Deploying Telegram bot with webhooks on Heroku using python-telegram-bot library

I’m working on deploying a Telegram bot to Heroku using the python-telegram-bot library with webhook functionality. I’ve been trying to create a basic message repeater bot but I’m running into issues with the webhook configuration.

The bot should respond to user messages by sending back the same text, but it’s not working as expected. I think there might be something wrong with how I’m setting up the webhook or handling the requests.

Here’s my current code:

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__)

global telegram_bot
telegram_bot = telegram.Bot(token=BOT_TOKEN)

@server.route('/' + BOT_TOKEN, methods=['POST'])
def handle_webhook():
    if request.method == "POST":
        # parse the incoming message as Telegram update object
        incoming_update = telegram.Update.de_json(request.get_json(force=True))
        
        user_chat_id = incoming_update.message.chat.id
        
        # encode message text for proper UTF-8 handling
        user_message = incoming_update.message.text.encode('utf-8')
        
        # send the same message back to user
        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 settings
    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 identify what might be causing the webhook to fail? Any guidance would be really helpful!

I’ve hit this exact issue deploying telegram bots on Heroku. Your problem is mixing Flask with python-telegram-bot’s built-in webhook stuff - they’re fighting over the same port. You can’t call both bot_updater.start_webhook() and server.run() together. Just use Flask for webhooks. Ditch the start_webhook() and server.run() calls from your main block and let Heroku start the server. Also, that user_message.encode('utf-8') line is breaking things - the text’s already a string, so pass user_message directly to sendMessage(). Wasted hours on this same encoding mess. Make sure your Procfile has web: python your_bot_file.py and set the webhook URL through Telegram’s API after you deploy.

there’s another issue - you’re not starting the flask server properly. remove all that updater code and just add server.run(host='0.0.0.0', port=server_port) at the end. also, that utf-8 encoding is definitely wrong - telegram already gives you proper strings. make sure your config imports work too, missing bot_token will fail silently.

Your webhook setup is the problem. You’re calling bot_updater.bot.setWebhook() then immediately trying bot_updater.idle() - these conflict because the updater wants to manage its own webhook lifecycle. Just ditch the entire updater block and run Flask directly. Also, make sure you’re actually calling server.run() with the right parameters. Your code has it at the end but bot_updater.idle() blocks execution so it never gets there. I had the same headache until I figured out Heroku needs Flask to bind properly to the assigned port. Set your webhook URL once after deployment, not in your startup code.