telegrampy-telegram-bot-api

Disable preview for a specific link in Telegram bot API


Does anyone know if a bot sets the disable_web_page_preview: false in sendMessage method and sends a link as part of a message, can I somehow programmatically disable the preview for a this (one) link?

My task is to ensure that the link that the bot sends as part of the message is displayed without preview. But if an admin decides to manually edit a post and add a link, the added link should have a preview working. After updating Telegram, if disable_web_page_preview: false is specified, all links do not have a preview. Regardless of whether they were sent by a bot or added later by the channel administrator when editing the message. You cannot manually enable link previews through the application; you can only disable link previews. From my point of view, the latest Telegram update in this part was done poorly.

Tried disable_web_page_preview: true - all links do not have a preview. If false, the first link in the message sent by the bot has a preview. I don't need a preview for this specific link, and need preview for other links.


Solution

  • Use link_preview_options parameter of sendMessage and set the proper LinkPreviewOptions.

    Example of GET url in php:

    $telegramUrl = "https://api.telegram.org/bot" . TELEGRAM_BOT_TOKEN .
                   "/sendMessage?chat_id=" . $chatid .
                   "&link_preview_options=" . urlencode('{"url":"https://example.org"}') .
                   "&parse_mode=HTML&text=" . urlencode($text);