我正在用Python构建一个Telegram机器人,该机器人可以按命令获取组织的Github存储库的星标并显示它们。
机器人运行并显示欢迎消息,但不响应任何命令,然后崩溃并给出错误消息,
错误R10(引导超时)-> Web进程在启动2018-11-17T17:13:40.232216 + 00:00的60秒内未能绑定到$ PORT
heroku [web.1]:使用SIGKILL停止进程
2018-11-17T17:13:40.309943 + 00:00 heroku [web.1]:进程退出 状态137
2018-11-17T17:13:40.370462 + 00:00 heroku [web.1]:状态 从开始更改为崩溃
2018-11-17T17:13:41.899621 + 00:00 heroku [router]:at =错误代码= H10 desc =“应用程序崩溃”,方法= GET path =“ /” host = gcijbossbot.herokuapp.com request_id = 4cf3c8f0-940b-4c73-aee7-842b1949e395 fwd =“ 115.97.36.250” dyno = connect = service = status = 503字节= protocol = https
2018-11-17T17:13:44.029680 + 00:00 heroku [router]:at =错误代码= H10 desc =“应用程序崩溃”方法=获取路径=“ / favicon.ico” host = gcijbossbot.herokuapp.com request_id = 94937fe2-56d2-4f4c-bad9-1fe679442db4 fwd =“ 115.97.36.250” dyno = connect = service = status = 503字节= protocol = https
我尝试从
切换Procfileweb: python Stars.py
到
worker: python Stars.py
但是该应用程序根本无法运行。
Stars.py代码:
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import requests
def start(bot, update):
update.message.reply_text('Ahoy {}! Welcome to JBossStarsBot. \n\nTo get started, use the /stars command to fetch the stars from the GitHub repos of JBoss'.format(update.message.from_user.first_name))
def stars(bot, update):
api = requests.get('https://api.github.com/orgs/JBossOutreach/repos')
json = api.json()
stars = ''
for i in range(len(json)):
stars = stars + '\n' + res[i]['name'] + ' : ' + str(res[i]['stargazers_count'])
update.message.reply_text('Here\'s the list of all the JBoss repositories on GitHub along with their respective star count. \n\n' + stars + '\n\nTo get the stars of a specific repository, enter the name of the repository.')
def repo_stars(bot, update):
api = requests.get('https://api.github.com/orgs/JBossOutreach')
json = api.json()
star = ''
for i in range(len(json)):
cur = res[i]['name']
if cur == update.message.text:
star = star + cur + ' : ' + str(res[i]['stargazers_count'])
if cur == '':
star = 'No such repository found.'
bot.send_message(update.message.chat_id, star)
def main():
updater = Updater(token)
dp = updater.dispatcher
dp.add_handler(CommandHandler('start', start))
dp.add_handler(CommandHandler('stars', stars))
dp.add_handler(MessageHandler(Filters.text, repo_stars))
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()
我还没有使用Django或Flask。只需使用python-telegram-bot和请求即可。
答案 0 :(得分:1)
我对Telegram不熟悉,但是看起来您实际上并没有在运行HTTP服务器。而是,您的代码为periodically polling for updates:
可以将更新程序作为轮询服务启动,或者在生产时使用Webhook接收更新。
在Heroku上,您需要运行实际的HTTP服务器,并将其绑定到PORT
环境变量提供的端口。看来start_webhook
可以做到这一点,例如像
import os
def main():
# ...
port = os.getenv('PORT', default=8000)
updater.start_webhook(port=port)