I’m trying to deploy a simple message bot using the telegram python library on Heroku platform but running into issues with webhook configuration. I followed several tutorials and documentation but my bot doesn’t respond to messages properly.
The main problem is that the webhook setup isn’t working as expected. When users send messages to my bot, nothing happens and no responses are sent back.
Here’s my current implementation:
import telegram
from os import environ
from telegram.ext import Updater
from flask import Flask, request
from config import API_TOKEN, WEBHOOK_URL
server = Flask(__name__)
global telegram_bot
telegram_bot = telegram.Bot(token=API_TOKEN)
@server.route('/' + API_TOKEN, methods=['POST'])
def handle_message():
if request.method == "POST":
# parse incoming message data
incoming_update = telegram.Update.de_json(request.get_json(force=True))
user_chat_id = incoming_update.message.chat.id
# get message text and handle encoding
message_text = incoming_update.message.text.encode('utf-8')
# send back the same message
telegram_bot.sendMessage(chat_id=user_chat_id, text=message_text)
return 'success'
if __name__ == "__main__":
server_port = int(environ.get('PORT', '5000'))
bot_updater = Updater(API_TOKEN)
# configure webhook settings
bot_updater.start_webhook(listen="0.0.0.0", port=server_port, url_path=API_TOKEN)
bot_updater.bot.setWebhook(WEBHOOK_URL + API_TOKEN)
bot_updater.idle()
server.run(environ.get('PORT'))
Can anyone help me identify what might be wrong with this setup? Any guidance would be really helpful!
Had the same issue with my first Telegram bot on Heroku. You’re running both Flask and the telegram updater’s webhook at the same time - that’s what’s causing the conflict. Pick one: either use Flask for webhooks OR use telegram updater’s built-in webhook, not both. Flask works better on Heroku in my experience. Drop the bot_updater.start_webhook() and server.run() lines and let Heroku handle Flask directly. Also, message_text.encode('utf-8') is unnecessary and might break things. Telegram wants strings, not bytes, so ditch that encoding step. Double-check that your webhook URL in Heroku config vars matches exactly what you’ve got in your route decorator.
Your Procfile config and webhook setup are causing this. I hit the same issue last year with my first bot deployment. You need Flask as the only web process, but you’re not starting it right for Heroku. Delete all the updater webhook code and the server.run() call. Make your Procfile say web: python app.py and change your main block to just server.run(host='0.0.0.0', port=int(environ.get('PORT', 5000))). Don’t set the webhook URL every time the app starts - do it once after deployment with a one-time script or hit Telegram’s API directly. And wrap incoming_update.message in error handling since it’s None for some update types like callbacks.
Your main problem is running Flask and the Updater’s webhook simultaneously - they’re conflicting with each other. Also, your Flask app isn’t being served right. Don’t use server.run() on Heroku. You need a WSGI server like gunicorn instead. Make sure your Procfile says something like web: gunicorn app:server. Drop that encoding line too - sendMessage just wants a regular string. Debugging webhooks on Heroku is a pain, so add some logging to see if your webhook endpoint actually gets hit when messages come in.
you’re mixing flask and telegram updater - that won’t work. drop the updater stuff and stick with just flask. also, test if your webhook url is reachable by hitting it in your browser. you’ll prob need to handle cases where the message is None or empty.
Check your Heroku logs first - that’s where you’ll see what’s actually happening when messages come in. Your webhook URL might not even be getting hit. Also, that server.run(environ.get('PORT')) at the end is wrong. It should be server.run(host='0.0.0.0', port=server_port), but honestly just remove it since Heroku handles Flask differently.
Your code has the classic webhook setup headache everyone hits. Skip the Flask/Heroku deployment mess - automate this instead.
I’ve built dozens of Telegram bots and manual deployment always becomes a debugging nightmare. You’re juggling WSGI servers, environment variables, webhook URLs, then when production breaks you’re guessing what went wrong.
Visual automation platforms work way better - they handle all the server stuff. Connect Telegram as a trigger, add your bot logic, and it manages webhooks and scaling automatically. No Heroku configs or Flask routing.
I built a customer support bot this way in 10 minutes. Message comes in, processes text, sends response back. Zero server management, zero webhook debugging. Need to update logic? Just modify the visual flow instead of redeploying code.
Your approach will work eventually, but you’ll spend more time on infrastructure than bot features. Automation lets you focus on what the bot does, not how to deploy it.