Telegram bot sendMessage problem. Parse_mode='HTML' is not working
Asked Answered
E

2

5

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] {}".

Enuresis answered 28/5, 2019 at 14:19 Comment(1)
possible duplicate : https://mcmap.net/q/539813/-using-html-in-telegram-botGladiate
E
17

Try sending the parse mode as on object;

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

Git issue

More info (Git)

Embody answered 29/5, 2019 at 11:25 Comment(0)
N
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}`);
}
Nodical answered 9/6, 2023 at 11:52 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.