How to setup python-telegram-bot webhook on Heroku?

I use the python-telegram-bot shell, and I tried to place a simple echo-telegram bot on Heroku by adapting a pre-existing example designed for the Google App Engine, as well as a wiki web guide , but to no avail.

It seems I can not get the webhook to work, and the echo message is correct for the bot.

I can’t understand what happened, so any help to point me in the right direction would be greatly appreciated!

My attempt is described in detail below.

import telegram from os import environ from telegram.ext import Updater from flask import Flask, request from credentials import TOKEN, APP_URL app = Flask(__name__) global bot bot = telegram.Bot(token=TOKEN) @app.route('/' + TOKEN, methods=['POST']) def webhook_handler(): if request.method == "POST": # retrieve the message in JSON and then transform it to Telegram object update = telegram.Update.de_json(request.get_json(force=True)) chat_id = update.message.chat.id # Telegram understands UTF-8, so encode text for unicode compatibility text = update.message.text.encode('utf-8') # repeat the same message back (echo) bot.sendMessage(chat_id=chat_id, text=text) return 'ok' if __name__ == "__main__": PORT = int(environ.get('PORT', '5000')) updater = Updater(TOKEN) # add handlers updater.start_webhook(listen="0.0.0.0", port=PORT, url_path=TOKEN) updater.bot.setWebhook(APP_URL + TOKEN) updater.idle() app.run(environ.get('PORT')) 
+6
source share
1 answer

in the wiki you will find the simplest example.

https://github.com/python-telegram-bot/python-telegram-bot/wiki/Webhooks#heroku

In short, do not try to use a flask. Use the built-in web server.

+2
source

Source: https://habr.com/ru/post/1014130/


All Articles