skip to Main Content

i try learning API telegram and i have some issue with ‘Parameter’ ‘link_preview_options’

https://core.telegram.org/bots/api#sendmessage
https://core.telegram.org/bots/api#sendmessage

  function sendTelegramMessage(chatId, text) {
    var token = "my_token";
    var apiUrl = "https://api.telegram.org/bot" + token + "/sendMessage";

    var payload = {
      method: "post",
      payload: {
        chat_id: chatId,
        text: text,
        parse_mode: "HTML",
        link_preview_options: {is_disabled: true},
      },
    };

    var response = UrlFetchApp.fetch(apiUrl, payload);

  }

I get

Exception: Request failed for https://api.telegram.org returned code 400. Truncated server response: {"ok":false,"error_code":400,"description":"Bad Request: can't parse link preview options JSON object"} (use muteHttpExceptions option to examine full response)
    at sendTelegramMessage(NotifyTG:17:32)


17  var response = UrlFetchApp.fetch(apiUrl, payload);

this parameter should remove thumbnails in links from messages sent by the telegram bot

It works without ‘link_preview_options: {is_disabled: true},’

  function sendTelegramMessage(chatId, text) {
    var token = "my_token";
    var apiUrl = "https://api.telegram.org/bot" + token + "/sendMessage";

    var payload = {
      method: "post",
      payload: {
        chat_id: chatId,
        text: text,
        parse_mode: "HTML",
        // link_preview_options: {is_disabled: true},
      },
    };

    var response = UrlFetchApp.fetch(apiUrl, payload);

  }

2

Answers


  1. Chosen as BEST ANSWER

    Changed this part:

    var payload = {
      method: "post",
      payload: {
        chat_id: chatId,
        text: text,
        parse_mode: "HTML",
        // link_preview_options: {is_disabled: true},
      },
    };
    

    On this

    var payload = {
      method: "post",
      contentType: "application/json",
      headers: {
        Authorization: "Bot " + token
      },
      payload: JSON.stringify({
        chat_id: chatId,
        text: text,
        parse_mode: "HTML",
        disable_web_page_preview: true
      }),
      muteHttpExceptions: true
    };
    

    Now it works and I don't know why =/


  2. You may need to serialize the link_preview_options object:

    ...
        'link_preview_options': JSON.stringify({
          'is_disabled': true
        }),
    ...
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search