2017-01-16 4 views
4

저는 python-telegram-bot 래퍼를 사용하고 있습니다. Heoku adapting a pre-existing example에 대한 간단한 echo telegram 봇을 호스트하려했는데, 이는 Google App Engine과 webhook guide on the wiki을위한 것이었지만 아무 소용이 없었습니다.Heroku에서 python-telegram-bot 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')) 

답변