我想创建一个机器人的电报和网络应用程序,我可以编辑和存储说机器人的命令。当我在heroku中部署时,问题就出现了,因为我不能同时运行机器人和web应用程序。我想我的网钩出了点问题。这是我的代码。
app.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!!")
home.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>
Procfile
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
有什么想法吗?
发布于 2021-10-27 18:46:57
注释掉app.run(host='0.0.0.0',port=int(os.environ.get('PORT',5000))行)
您将不会有/home处理程序,但是webhook可能会在Heroku提供的端口上侦听
发布于 2021-10-28 19:22:32
你不能在同一个端口上有两个进程(见错误),也不能在同一个Heroku Dyno上使用两个不同的端口。
您可以使用Flask应用程序作为REST前端,而不是Telegram Webhook,您可以使用polling
方法(不需要绑定端口)
https://stackoverflow.com/questions/69742625
复制相似问题