我正在Google Compute Engine上部署我的Telegram聊天机器人,并被webhook url的设置所困扰。
我有一个config.ini来存储相关信息,并且我试图使用项目的URL进行设置:
[TELEGRAM]
ACCESS_TOKEN = {token}
WEBHOOK_URL = https://api.telegram.org/bot{token}/setWebhook?url=https://{project-id}.appspot.com/hook
我在GCP中将防火墙设置为5000。我复制并粘贴该链接,它显示“ {“ ok”:true,“ result”:true,“ description”:“ Webhook已设置”}“。但是当我运行main.py(chatbot脚本)时,chatbot无法正常工作。 WEBHOOK_URL应该是什么?部署的步骤是什么-与我使用ngrok复制并粘贴链接以确认钩子然后运行main.py相同吗?
运行main.py时,它表明我在开发服务器中,并建议我使用生产WSGI。是否必须这样做?这是日志:
sys:1: TelegramDeprecationWarning: Old Handler API is deprecated - see https://git.io/fxJuV for details
* Serving Flask app "main" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
2020-05-19 17:49:57,685 - werkzeug - INFO - * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
2020-05-19 17:49:57,686 - werkzeug - INFO - * Restarting with stat
sys:1: TelegramDeprecationWarning: Old Handler API is deprecated - see https://git.io/fxJuV for details
2020-05-19 17:49:57,969 - werkzeug - WARNING - * Debugger is active!
2020-05-19 17:49:57,970 - werkzeug - INFO - * Debugger PIN: 286-205-082
我不认为这是我的main.py,但以防万一,我将相关部分放在这里:
import configparser
import requests
import logging
import telegram
from flask import Flask, request
from telegram.ext import Dispatcher, MessageHandler, Filters, Updater, CommandHandler
# Load data from config.ini file
config = configparser.ConfigParser()
config.read('config.ini')
# Enable logging
logging.basicConfig(format = '%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level = logging.INFO)
logger = logging.getLogger(__name__)
# Initial Flask app
app = Flask(__name__)
# Initial bot by Telegram access token
bot = telegram.Bot(token = (config['TELEGRAM']['ACCESS_TOKEN']))
@app.route('/hook', methods=['POST'])
def webhook_handler():
"""Set route /hook with POST method will trigger this method."""
if request.method == "POST":
update = telegram.Update.de_json(request.get_json(force = True), bot)
# Update dispatcher process that handler to process this message
dispatcher.process_update(update)
return 'ok', 200
def whatever_chatbot_function():
.
.
.
dispatcher = Dispatcher(bot, None)
dispatcher.add_handler(CommandHandler('start', start_handler))
.
.
.
if __name__ == "__main__":
# Running server
app.run(debug=True)
非常感谢!
#May be try this inside the
def whatever_chatbot_function():
updater = Updater(bot,use_context=True)
dp = updater.dispatcher
dp.add_handler(CommandHandler('start', start_handler))