Flask 为什么烧瓶没有';无法使用gunicorn和电报机器人api?

Flask 为什么烧瓶没有';无法使用gunicorn和电报机器人api?,flask,telegram,gunicorn,telegram-bot,Flask,Telegram,Gunicorn,Telegram Bot,我正在使用电报机器人api(telebot)、flask和gunicorn。 当我使用命令python app.py时,一切正常,但当我使用pythonwsgi.pyflask时,http://127.0.0.1:5000/和bot不应答,如果我使用的是gunicorn--bind 0.0.0.0:8443 wsgi:appwebhook正在设置,但电报bot不应答。我试图将app.run从app.py添加到wsgi.py,但没有成功 app.py import logging import t

我正在使用电报机器人api(telebot)、flask和gunicorn。 当我使用命令
python app.py
时,一切正常,但当我使用python
wsgi.py
flask时,
http://127.0.0.1:5000/
和bot不应答,如果我使用的是
gunicorn--bind 0.0.0.0:8443 wsgi:app
webhook正在设置,但电报bot不应答。我试图将app.run从app.py添加到wsgi.py,但没有成功

app.py

import logging
import time
import flask
import telebot

API_TOKEN = '111111111:token_telegram'

WEBHOOK_HOST = 'droplet ip'
WEBHOOK_PORT = 8443  # 443, 80, 88 or 8443 (port need to be 'open')
WEBHOOK_LISTEN = '0.0.0.0'  # In some VPS you may need to put here the IP addr

WEBHOOK_SSL_CERT = 'webhook_cert.pem'  # Path to the ssl certificate
WEBHOOK_SSL_PRIV = 'webhook_pkey.pem'  # Path to the ssl private key

WEBHOOK_URL_BASE = "https://%s:%s" % (WEBHOOK_HOST, WEBHOOK_PORT)
WEBHOOK_URL_PATH = "/%s/" % (API_TOKEN)

logger = telebot.logger
telebot.logger.setLevel(logging.DEBUG)

bot = telebot.TeleBot(API_TOKEN)

app = flask.Flask(__name__)


# Empty webserver index, return nothing, just http 200
@app.route('/', methods=['GET', 'HEAD'])
def index():
    return ''


# 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)


# Handle all other messages
@bot.message_handler(func=lambda message: True, content_types=['text'])
def echo_message(message):
    bot.reply_to(message, message.text)


# Remove webhook, it fails sometimes the set if there is a previous webhook
bot.remove_webhook()
#
time.sleep(1)

# Set webhook
bot.set_webhook(url=WEBHOOK_URL_BASE + WEBHOOK_URL_PATH,
                certificate=open(WEBHOOK_SSL_CERT, 'r'))

if __name__ == "__main__":
    # Start flask server
    app.run(host=WEBHOOK_LISTEN,
            port=WEBHOOK_PORT,
            ssl_context=(WEBHOOK_SSL_CERT, WEBHOOK_SSL_PRIV),
            debug=True)
wsgi.py

from app import app  

if __name__ == "__main__":
    app.run()

根据您的情况更改端口号和调试参数。

向全世界展示python Web服务器是一种不好的做法。它不安全。 良好做法-使用反向代理,例如
nginx

链条 因此,链条应该是:

api.telegram.org->您的\u域->您的\u nginx->您的\u Web服务器->您的\u应用程序

SSL 您的ssl证书应该在
nginx
级别进行检查。成功后,只需将请求传递到Web服务器(flask或其他)。这也是一个关于
如何在一台主机/端口上使用大量bot的技巧(
:)

如何配置nginx反向代理-您可以在startoverflow或google中找到

电报网钩
telebot
对于使用webhook来说非常复杂。 尝试使用
aigram
示例。这很简单:

来自aigram导入Bot、调度器、执行器
从aigram.types导入消息
网络钩子https://your.domain'
WEBHOOK_PATH='/PATH/to/api'
bot=bot('bot:TOKEN')
dp=调度程序(bot)
@dp.message_handler()
异步def回显(消息:消息):
返回message.answer(message.text)
启动时异步定义(dp:Dispatcher):
wait bot.set_webhook(f“{webhook_HOST}{webhook_PATH}”)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
executor.start\u webhook(dispatcher=dp,on\u startup=on\u startup,
webhook_path=webhook_path,host='localhost',port=3000)

Webhook只能在端口80、88、443或8443Try上设置,不带端口和调试参数。我以前在没有主机=0.0.0.0的情况下遇到过问题。永远不要使用0.0.0.0!不安全!仅本地主机+反向代理。
app.run(host=0.0.0.0, port=5000, debug=True)