2017-01-16 168 views
4

我正在使用python-telegram-bot包裝器,並且我一直試圖在Heroku adapting a pre-existing example上託管一個簡單的回顯電報機器人,這個電子郵件應用於Google App Engine以及webhook guide on the wiki,但無濟於事。如何在Heroku上設置python-telegram-bot webhook?

我似乎沒有能夠讓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')) 

回答