【发布时间】: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