python - heroku 中 webhook telegrambot 和烧瓶的问题
问题描述
我想创建一个用于电报的机器人和一个 Web 应用程序,我可以从中编辑和存储所述机器人的命令。当我在 heroku 中部署时会出现问题,因为我无法同时运行机器人和 Web 应用程序。我认为我在使用 webhook 时遇到了问题。这是我的代码。
应用程序.py
import os
import sys
import telegram
from telegram.ext import Updater, updater
from telegram.ext.commandhandler import CommandHandler
from resources.commands import *
from flask import Flask, render_template, request
token = "Telegram bot token"
heroku_app_name = "Heroku app name"
bot = telegram.Bot(token= token)
app=Flask(__name__)
updater = Updater(bot.token, use_context= True)
@app.route('/home')
def home():
return render_template('home.html')
@app.route('/')
def webhook():
updater.start_webhook(listen="0.0.0.0",
port=int(os.environ.get('PORT', 5000)),
url_path=token,
webhook_url="https://{}.herokuapp.com/{}".format(os.environ.get("HEROKU_APP_NAME"), token))
return "!", 200
if __name__ == '__main__':
webhook()
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 5000)))
dp = updater.dispatcher
dp.add_handler(CommandHandler("hello", hellothere))
def hellothere(update, context):
update.message.reply_text("Hello There!!")
主页.html
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<link rel="icon" href="data:,">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Bot</title>
</head>
<body>
<h1>Hello world!!</h1>
</body>
</html>
档案
web: python app.py
日志
2021-10-27T16:49:56.962690+00:00 heroku[web.1]: Starting process with command `python app.py`
2021-10-27T16:49:59.445699+00:00 app[web.1]: 2021-10-27 16:49:59,445 - apscheduler.scheduler - INFO - Scheduler started,
2021-10-27T16:49:59.800346+00:00 app[web.1]: * Serving Flask app 'app' (lazy loading)
2021-10-27T16:49:59.800372+00:00 app[web.1]: * Environment: production
2021-10-27T16:49:59.800373+00:00 app[web.1]: WARNING: This is a development server. Do not use it in a production deployment.
2021-10-27T16:49:59.800387+00:00 app[web.1]: Use a production WSGI server instead.
2021-10-27T16:49:59.800399+00:00 app[web.1]: * Debug mode: on
2021-10-27T16:49:59.817520+00:00 app[web.1]: Traceback (most recent call last):
2021-10-27T16:49:59.817521+00:00 app[web.1]: File "/app/app.py", line 401, in <module>
2021-10-27T16:49:59.817724+00:00 app[web.1]: app.run(debug=True, host='0.0.0.0', port=int(os.environ.get('PORT', 5000)))
2021-10-27T16:49:59.817727+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/flask/app.py", line 920, in run
2021-10-27T16:49:59.817951+00:00 app[web.1]: run_simple(t.cast(str, host), port, self, **options)
2021-10-27T16:49:59.817960+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/werkzeug/serving.py", line 984, in run_simple
2021-10-27T16:49:59.818196+00:00 app[web.1]: s.bind(server_address)
2021-10-27T16:49:59.818260+00:00 app[web.1]: OSError: [Errno 98] Address already in use
2021-10-27T16:50:00.329639+00:00 heroku[web.1]: State changed from starting to up
2021-10-27T16:50:11.000000+00:00 app[api]: Build succeeded
任何想法?
解决方案
你不能在同一个端口上有 2 个进程(见错误),你不能在同一个 Heroku Dyno 上使用 2 个不同的端口。
您可以使用 Flask APP 作为 REST 前端,而不是 Telegram Webhook,您可以使用该polling
方法(不需要绑定端口)
推荐阅读
- python - 如何解决“flask.cli.NoAppException:无法导入文件名”?
- c++ - std::isspace 是 C++ 中 std::isgraph 的对立面吗(语言环境版本)
- javascript - Redux、useSelector 和 map:改变状态后,map() 不会改变内容
- excel - 如何将代码应用于以下所有行
- python - 如何在 Python 中处理异常处理
- c# - 如何将 WPF 支持添加到 .NET 5 Winforms 项目
- spotipy - 使用 spotipy 获得可舞性和效价
- scala - Spark SQL - 查找乘客去过的最多国家
- firebase - 尝试从我的集合中获取数组数据时遇到问题
- mysql - Munin MySQL插件的未知图形错误