小编Ale*_*kin的帖子

通过 pyTelegramBotAPI 和 pythonanywhere.com 上的 Flask 使用 Telegram bot webhook

问题是关于使用 pyTelegramBotAPI 模块在 Telegram 机器人中使用 webhook。我正在使用 pythonanywhere.com 来托管机器人。

下面的代码工作正常:

from flask import Flask, request
import telebot

secret = "A_SECRET_NUMBER"
bot = telebot.TeleBot ('YOUR_AUTHORIZATION_TOKEN')
bot.set_webhook("https://YOUR_PYTHONANYWHERE_USERNAME.pythonanywhere.c..
}".format(secret), max_connections=1)

app = Flask(__name__)
@app.route('/{}'.format(secret), methods=["POST"])
def telegram_webhook():
   update = request.get_json()
   if "message" in update:
   text = update["message"]["text"]
   chat_id = update["message"]["chat"]["id"]
   bot.sendMessage(chat_id, "From the web: you said '{}'".format(text))
return "OK"
Run Code Online (Sandbox Code Playgroud)

但是,当我使用示例中所示的消息处理程序时,我没有收到机器人的答复:

# Process webhook calls
@app.route(WEBHOOK_URL_PATH, methods=['POST'])
def webhook():
   if flask.request.headers.get('content-type') == 'application/json':
   json_string = flask.request.get_data().decode('utf-8')
   update = telebot.types.Update.de_json(json_string)
   bot.process_new_updates([update])
   return ''
else:
   flask.abort(403) …
Run Code Online (Sandbox Code Playgroud)

flask pythonanywhere telegram telegram-bot py-telegram-bot-api

4
推荐指数
1
解决办法
5469
查看次数