Telegram bot API setWebhook不支持Google App Engine

时间:2018-03-20 17:05:38

标签: python google-app-engine telegram-bot

我的app.yaml文件如下:

runtime: python
env: flex
entrypoint: gunicorn -b :8443 main:app
threadsafe: true

runtime_config:
  python_version: 2

所以,当我在GAE中运行这个python脚本时(当然,删除了之前的webhook),webhook没有设置好。我无法弄清楚我做错了什么。

import sys
import os
import time
from flask import Flask, request
import telegram

# CONFIG
TOKEN    = '<token>'
HOST     = 'example.appspot.com' # Same FQDN used when generating SSL Cert
PORT     = 8443
CERT     = "certificate.pem"
CERT_KEY = "key.pem"


bot = telegram.Bot(TOKEN)
app = Flask(__name__)


@app.route('/')
def hello():
    return 'Hello World!'


@app.route('/' + TOKEN, methods=['POST','GET'])
def webhook():
    update = telegram.Update.de_json( request.get_json(force = True), bot )
    chat_id = update.message.chat.id
    bot.sendMessage(chat_id = chat_id, text = 'Hello, there')

    return 'OK'


def setwebhook():
    bot.setWebhook(url = "https://%s:%s/%s" % (HOST, PORT, TOKEN), certificate = open(CERT, 'rb'))


if __name__ == '__main__':
    context = (CERT, CERT_KEY)
    setwebhook()
    time.sleep(5)
    app.run(host = '0.0.0.0', port = PORT, ssl_context = context, debug = True)

我认为SSL证书可能存在问题,但如果我只是在不运行python代码的情况下执行此操作,那么一切正常:

curl -F "url=https://example.appspot.com:8443/<token>"  -F "certificate=@certificate.pem" 
      https://api.telegram.org/bot<token>/setWebhook

0 个答案:

没有答案