在heroku上托管python电报机器人
我有一个在heroku上托管python电报机器人,python,heroku,telegram-bot,Python,Heroku,Telegram Bot,我有一个.py文件,其中包含一些运行电报bot的代码,它在本地机器上运行良好,但当我将代码推入heroku时,它不工作,我的意思是,该文件不是为了接收和发送消息而运行的,但当我在heroku中输入bash并手动运行.py文件时,它工作正常。 我在Procfile中应用了一些更改,但我不知道如何告诉heroku自动运行.py文件。 我还尝试将我的bot代码包装到flask应用程序中,它在本地机器上运行得非常好,但在heroku上,flask请求处理成功,但代码中的bot无法再次运行。 代码如下:
.py
文件,其中包含一些运行电报bot
的代码,它在本地机器上运行良好,但当我将代码推入heroku
时,它不工作,我的意思是,该文件不是为了接收和发送消息而运行的,但当我在heroku
中输入bash并手动运行.py
文件时,它工作正常。我在
Procfile
中应用了一些更改,但我不知道如何告诉heroku
自动运行.py
文件。我还尝试将我的
bot
代码包装到flask应用程序中,它在本地机器上运行得非常好,但在heroku
上,flask请求处理成功,但代码中的bot无法再次运行。代码如下:
from flask import Flask
import time
import os
import telepot
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine, Column, Integer, PickleType, String
from sqlalchemy.orm import sessionmaker
from flask.ext.heroku import Heroku
app = Flask(__name__)
app.config['SECRET_KEY'] = "random string"
heroku = Heroku(app)
@app.route('/') # the requests handled successfully!
def hello_world():
return 'Hello World!'
# the bot code deleted to simplify
uri = os.environ.get('DATABASE_URL')
engine = create_engine(uri)
Base = declarative_base()
if __name__ == '__main__':
Base.metadata.create_all(engine)
global session
Session = sessionmaker(bind=engine)
session = Session()
TOKEN = 'token'
bot = MyBot(TOKEN) # the bot stuff here
bot.message_loop() # but the bot stuff just doesn't work
# i also removed incoming 2 lines and let it be the default but has no effect
port = int(os.environ.get("PORT", 5000))
app.run(host='0.0.0.0', port=port)
while 1:
time.sleep(10)
engine.dispose()
我从heroku查看了日志,没有错误,但是bot的代码不起作用。那么,“烧瓶包装”机器人的问题是什么呢?我怎么能简单地告诉heroku运行python文件,让它自然运行呢?也许你必须在
Procfile
中输入这样一个字符串:
web: python web.py
其中web.py
是脚本的名称
同时检查包含所有依赖项的requirements.txt
文件。看看这篇文章
部署代码后,运行应用程序,如果出现错误,请检查Heroku日志。您可以通过单击右角的按钮查看Heroku dashboard上的日志More
->查看日志