Deploying Telegram bot with webhook configuration on Heroku platform

I’ve been struggling to get my Telegram bot working properly on Heroku using webhooks. The bot is supposed to send back whatever message users type to it, but it’s not responding at all.

I’m using the python-telegram-bot library and Flask to handle the webhook requests. I followed some tutorials online but something is still not working correctly. The webhook doesn’t seem to be receiving messages from Telegram properly.

Here’s my current code setup:

import telegram
from os import environ
from telegram.ext import Updater
from flask import Flask, request
from config import BOT_TOKEN, HEROKU_URL

server = Flask(__name__)

telegram_bot = telegram.Bot(token=BOT_TOKEN)


@server.route('/' + BOT_TOKEN, methods=['POST'])
def handle_webhook():
    if request.method == "POST":
        # get the update from Telegram
        incoming_update = telegram.Update.de_json(request.get_json(force=True))
        
        user_chat_id = incoming_update.message.chat.id
        
        # get the message text
        user_message = incoming_update.message.text.encode('utf-8')
        
        # send the same message back
        telegram_bot.sendMessage(chat_id=user_chat_id, text=user_message)
    
    return 'success'


if __name__ == "__main__":
    server_port = int(environ.get('PORT', '5000'))
    bot_updater = Updater(BOT_TOKEN)
    
    # configure webhook
    bot_updater.start_webhook(listen="0.0.0.0", port=server_port, url_path=BOT_TOKEN)
    bot_updater.bot.setWebhook(HEROKU_URL + BOT_TOKEN)
    bot_updater.idle()
    server.run(environ.get('PORT'))

Can anyone help me figure out what might be wrong with this setup? Any suggestions would be really helpful!

I hit the same issues when I first deployed my Telegram bot to Heroku. Your main problem is mixing Flask with the Updater’s webhook functionality - they’re conflicting with each other. I fixed this by using only Flask to handle the webhook. Ditch the Updater webhook setup in your main block and just use Flask’s server. Also, drop that encode(‘utf-8’) call on the message text. It’s unnecessary and might break things since sendMessage wants a string, not bytes. Double-check if your webhook is actually set up right. Call getWebhookInfo on your bot to see if Telegram is properly configured to send updates to your Heroku app. Make sure your HEROKU_URL variable has the https:// prefix and ends with a slash before you add the token.

yo, u’ve got a conflict with running both Flask and Updater! just remove the server.run() line at the end. also, fixing that user_message.encode('utf-8') should help. telegram needs normal strings, not bytes. try that and see what happens!

You’re running Flask and the Updater’s webhook at the same time - that’s your problem. I hit the same issue when I deployed my first Telegram bot on Heroku. Ditch the Updater initialization completely and just use Flask to handle everything. Your webhook route should be the only thing processing messages from Telegram. Also, you’re encoding the message text to bytes with encode(‘utf-8’), but sendMessage wants a regular string. Just use user_message = incoming_update.message.text without any encoding. Add some basic error handling around message processing to catch None values or missing attributes. Test your webhook URL manually first to make sure Heroku’s getting POST requests before you debug the Telegram side.