【问题标题】:How to set up python-telegram-bot webhook on Heroku?如何在 Heroku 上设置 python-telegram-bot webhook?
【发布时间】:2017-05-31 14:25:31
【问题描述】:

我正在使用 python-telegram-bot 包装器,我一直在尝试在 Heroku adapting a pre-existing example 上托管一个简单的 echo telegram bot,它适用于 Google App Engine 和 webhook guide on the wiki,但是无济于事。

我似乎无法让 webhook 工作,也无法让机器人正确回显消息。

我似乎无法弄清楚出了什么问题,因此非常感谢任何能帮助我指出正确方向的人!

我的尝试详述如下。

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

【问题讨论】:

    标签: telegram-bot python-telegram-bot


    【解决方案1】:

    您会在 wiki 中找到最简单的示例。

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

    总之,不要尝试使用flask。使用内置的网络服务器。

    【讨论】:

      猜你喜欢
      • 2017-07-22
      • 2017-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-12
      • 1970-01-01
      • 2017-10-17
      • 2020-09-22
      相关资源
      最近更新 更多