4

I want to send a message from Telegram Bot to user with parse_mode 'HTML' . I use node.js with telegram.bot.api. But I've got an error

I've tried to write a code without parse_mode='HTML'. And it is working. But if I only add parse_mode=''(html or markdown) node.js show me: "error: [polling_error] {}"

Wrong code:

   const chatId=msg.chat.id;
   if (msg.text=='test'){
       bot.sendMessage(chatId,'<b>TEST</b>', parse_mode='HTML');
       return;
      }
})

Working code

    const chatId=msg.chat.id;
    if (msg.text=='test'){
        bot.sendMessage(chatId,'<b>TEST</b>');
        return;
       }
})

I cant find any solution and info about parse_mode='HTML' and "error: [polling_error] {}".

Paul Helios
  • 43
  • 1
  • 1
  • 3

2 Answers2

16

Try sending the parse mode as on object;

bot.sendMessage(chatId, '<b>TEST</b>', {parse_mode: 'HTML'});

Git issue

More info (Git)

0stone0
  • 34,288
  • 4
  • 39
  • 64
0

I have to change:

const fileMessage = await telegramClient.sendFile(chatId, {
  parseMode: "HTML", // ❌
  // ...
});

to

const fileMessage = await telegramClient.sendFile(chatId, {
  parseMode: "html", // ✅
  // ...
});

because of this validation in gram.js:

https://github.com/gram-js/gramjs/blob/b0f60d23d3111f2478326b86bd78aabeae0bb95a/gramjs/Utils.ts#LL1101C35-L1101C35

export function sanitizeParseMode(
    mode: string | ParseInterface
): ParseInterface {
    if (mode === "md" || mode === "markdown") {
        return MarkdownParser;
    }
    if (mode == "html") {
        return HTMLParser;
    }
    if (typeof mode == "object") {
        if ("parse" in mode && "unparse" in mode) {
            return mode;
        }
    }
    throw new Error(`Invalid parse mode type ${mode}`);
}
nezort11
  • 326
  • 1
  • 4
  • 11