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!