我想為電報創建一個機器人和一個網路應用程式,我可以從中編輯和存盤所述機器人的命令。當我在 heroku 中部署時會出現問題,因為我無法同時運行機器人和 Web 應用程式。我想我在使用 webhook 時遇到了問題。這是我的代碼。
應用程式
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
任何的想法?
uj5u.com熱心網友回復:
注釋掉 app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 5000)))
您將沒有 /home 處理程式,但 webhook 可能會監聽 Heroku 提供的埠
uj5u.com熱心網友回復:
同一個埠上不能有 2 個行程(參見錯誤),并且不能在同一個 Heroku Dyno 上使用 2 個不同的埠。
您可以使用 Flask APP 作為 REST 前端,而不是 Telegram Webhook,您可以使用該polling方法(不需要系結埠)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/343849.html
