我正在尝试在 Google App Engine 中制作一个简单的电报机器人。
当我在 GAE 中打开控制台并输入以下内容时,webhook 设置得很好
curl -F "url=https://example.appspot.com:8443/<token>" -F "certificate=@certificate.pem"
https://api.telegram.org/bot<token>/setWebhook
但是当我在 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)
app.yaml 文件如下:
runtime: python27
api_version: 1
threadsafe: yes
- url: .*
script: main.app
libraries:
- name: webapp2
version: "2.5.2"
- name: ssl
version: latest
编辑: 我更改了 app.yaml 文件如下,但我仍然无法设置 webhook。它现在给了我“502 bad gateway nginx”错误
runtime: python
env: flex
entrypoint: gunicorn -b :8443 main:app
threadsafe: true
runtime_config:
python_version: 2