在 heroku 上托管 python 电报机器人
hosting python telegram bot on heroku
我有一个 .py
文件,其中包含一些代码 运行 a telegram bot
它在我的本地机器上工作正常但是当我将我的代码推送到 heroku
它时不起作用,我的意思是文件不是 运行 接收和发送消息,但是当我在 heroku
和 运行 中手动输入 bash 和 .py
文件时它工作正常。
我在 Procfile
中应用了一些更改,但我不知道如何自动将 heroku
告诉 运行 .py
文件。
我还尝试将我的 bot
代码包装在一个烧瓶应用程序中,它再次在本地机器上完美运行,但在 heroku
上,烧瓶请求成功处理,但代码中的机器人不再工作。
这是代码:
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
代码不起作用。
那么 "flask wrapped" bot
中的问题是什么?我如何才能简单地告诉 heroku
到 运行 一个 python 文件并让它成为?
也许你要输入Procfile
这样一个字符串:
web: python web.py
其中 web.py
是脚本的名称。
同时检查 requirements.txt
包含所有依赖项的文件。看这篇文章on Heroku Dev Center.
部署您的代码后 运行 您的应用,如果出现错误 - 检查您的 Heroku 日志。您可以通过单击右上角的按钮 More
-> View logs
.
在 Heroku 仪表板中查看日志
我有一个 .py
文件,其中包含一些代码 运行 a telegram bot
它在我的本地机器上工作正常但是当我将我的代码推送到 heroku
它时不起作用,我的意思是文件不是 运行 接收和发送消息,但是当我在 heroku
和 运行 中手动输入 bash 和 .py
文件时它工作正常。
我在 Procfile
中应用了一些更改,但我不知道如何自动将 heroku
告诉 运行 .py
文件。
我还尝试将我的 bot
代码包装在一个烧瓶应用程序中,它再次在本地机器上完美运行,但在 heroku
上,烧瓶请求成功处理,但代码中的机器人不再工作。
这是代码:
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
代码不起作用。
那么 "flask wrapped" bot
中的问题是什么?我如何才能简单地告诉 heroku
到 运行 一个 python 文件并让它成为?
也许你要输入Procfile
这样一个字符串:
web: python web.py
其中 web.py
是脚本的名称。
同时检查 requirements.txt
包含所有依赖项的文件。看这篇文章on Heroku Dev Center.
部署您的代码后 运行 您的应用,如果出现错误 - 检查您的 Heroku 日志。您可以通过单击右上角的按钮 More
-> View logs
.