2
votes

I'm building a Telegram bot in Python which fetches stars of Github repos of an organisation on command and displays them.

The bot runs and shows the welcome message but doesn't respond to any command and then crashes giving the error,

Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch 2018-11-17T17:13:40.232216+00:00

heroku[web.1]: Stopping process with SIGKILL

2018-11-17T17:13:40.309943+00:00 heroku[web.1]: Process exited with status 137

2018-11-17T17:13:40.370462+00:00 heroku[web.1]: State changed from starting to crashed

2018-11-17T17:13:41.899621+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=gcijbossbot.herokuapp.com request_id=4cf3c8f0-940b-4c73-aee7-842b1949e395 fwd="115.97.36.250" dyno= connect= service= status=503 bytes= protocol=https

2018-11-17T17:13:44.029680+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=gcijbossbot.herokuapp.com request_id=94937fe2-56d2-4f4c-bad9-1fe679442db4 fwd="115.97.36.250" dyno= connect= service= status=503 bytes= protocol=https

I tried switching the Procfile from

web: python Stars.py 

to

worker: python Stars.py

but then the app doesn't work at all.

Stars.py code:

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()

I haven't used Django, or Flask. Simply python-telegram-bot and requests.

1
Does your app provide a web service? Does it listen for HTTP requests and respond with HTTP responses? That's what Heroku is for. If your code doesn't do that it's not a good fit.Chris
Yes. It's a telegram bot.Arihant Bansal
Okay, so what's in Stars.py? How do you set up your server to listen? We can't help with code we can't see.Chris
Questions on Stack Overflow must be self-contained. We're not going to go off-site and dig through your repository to find the problem. Please post the relevant parts here as a minimal reproducible example.Chris

1 Answers

2
votes

I'm not familiar with Telegram, but it looks like you aren't actually running an HTTP server. Instead, your code is periodically polling for updates:

The updater can be started as a polling service or, for production, use a webhook to receive updates.

On Heroku you need to run an actual HTTP server, binding to the port provided by the PORT environment variable. It looks like start_webhook can do this, e.g. with something like

import os

def main():
    # ...
    port = os.getenv('PORT', default=8000)
    updater.start_webhook(port=port)