Search code examples
telegramtelegram-botnode-telegram-bot-api

node-telegram-bot-api disable links preview


I've tried the following options, but I still see the link preview

await BOT.sendMessage(message.chat.id, uiMessage, menuOptions, {disable_web_page_preview: true })
await BOT.sendMessage(message.chat.id, uiMessage, menuOptions, { parse_mode: 'Markdown'})
await BOT.sendMessage(message.chat.id, uiMessage, menuOptions, { parse_mode: 'Markdown', disable_web_page_preview: true })
await BOT.sendMessage(message.chat.id, uiMessage, menuOptions, [{ parse_mode: 'Markdown'}, {disable_web_page_preview: true }])

Solution

  • The disable_web_page_preview is part of the options parameter.

    Looking at the sendMessage documentation:

    chatId      Number|String   Unique identifier for the target chat or username of the target channel (in the format @channelusername)  
    text        String          Text of the message to be sent  
    [options]   Object          Additional Telegram query options
    

    So you should extend menuOptions to also include disable_web_page_preview:

    await BOT.sendMessage(message.chat.id, uiMessage, {disable_web_page_preview: true })
    

    A litte snippet to test this:

    const TelegramBot = require('node-telegram-bot-api');
    
    const token = 'xxxxxxxx:AAEjLUL8Lx67lYEA3B2O7lSVhlCio3SVR9k';
    const chatId = 12345678;
    
    const bot = new TelegramBot(token, { polling: false });
    
    bot.sendMessage(chatId, 'Link Test');
    bot.sendMessage(chatId, '[Foo Bar](https://github.com/FFmpeg/FFmpeg)', { parse_mode: 'MarkDown', disable_web_page_preview: false });
    bot.sendMessage(chatId, '[Foo Bar](https://github.com/FFmpeg/FFmpeg)', { parse_mode: 'MarkDown', disable_web_page_preview: true });
    

    Shows as: enter image description here