Search code examples
pythonjsontelegramwebhooksslack

How to forward a photo from Telegram group chat to Slack (using Webhooks and Telegram Bot)?


I'm trying to build a Telegram bot which forwards messages from a Telegram group chat to a Slack channel using Webhooks. at the moment, I was able to forward all text messages and photo captions (caption only).

If a file (photo, video, etc.) is sent in the connected group, is there a way to get it's URL and forward it to the post request to Slack? When asked, Slack support told me I can send a URL but I couldn't find it the the JSON update sent by the Telegram bot when a file was sent.

Thanks for the help


Solution

  • after searching and asking experts,

    after receiving file_id in the webhook update from the Telegram bot, in order to get the image url as Slack requires, one should GET request the path from Telegram api using this URL:

    "https://api.telegram.org/bot/<BOT_TOKEN>/getFile?file_id=<FILE_ID>"
    

    and accessing 'file_path' under json_dict['result']['file_path']

    The next step is to use the path and add it to this URL:

    "https://api.telegram.org/file/bot/<BOT_TOKEN>/<FILE_PATH>"
    

    then send a POST request to the slack webhook URL using the pattern mentioned in the link: https://api.slack.com/messaging/webhooks

    more info can be found at TelegramAPI manual website: https://core.telegram.org/bots/api#getfile