skip to Main Content

I deployed my bot to Heroku but it is not responding

I have a doubt regarding the Procfile, is this correct?:

web: gunicorn tlgrmbot.wsgi
worker: python bot.py

This is what I see in the logs:

2021-07-27T21:25:56.080317+00:00 heroku[web.1]: Starting process with command `gunicorn tlgrmbot.wsgi`
2021-07-27T21:26:00.590684+00:00 heroku[web.1]: State changed from starting to up
2021-07-27T21:26:00.092958+00:00 app[web.1]: [2021-07-27 21:26:00 +0000] [4] [INFO] Starting gunicorn 20.1.0
2021-07-27T21:26:00.093896+00:00 app[web.1]: [2021-07-27 21:26:00 +0000] [4] [INFO] Listening at: http://0.0.0.0:54428 (4)
2021-07-27T21:26:00.094088+00:00 app[web.1]: [2021-07-27 21:26:00 +0000] [4] [INFO] Using worker: sync
2021-07-27T21:26:00.106699+00:00 app[web.1]: [2021-07-27 21:26:00 +0000] [9] [INFO] Booting worker with pid: 9
2021-07-27T21:26:00.157630+00:00 app[web.1]: [2021-07-27 21:26:00 +0000] [10] [INFO] Booting worker with pid: 10
2021-07-27T21:25:59.000000+00:00 app[api]: Build succeeded
2021-07-27T21:26:06.266840+00:00 app[web.1]: 10.43.228.207 - - [27/Jul/2021:21:26:06 +0000] "POST / HTTP/1.1" 200 10697 "-" "-"
2021-07-27T21:26:06.267235+00:00 heroku[router]: at=info method=POST path="/" host=tlgrmbotgym.herokuapp.com request_id=a7b1fd84-93d2-4fdb-88cd-941dd581b4c1 fwd="91.108.6.98" dyno=web.1 connect=0ms service=37ms status=200 bytes=10924 protocol=https

and this is how I setup the webhook in bot.py

mode = os.environ.get("MODE", "polling")
    if mode == 'webhook':

        # enable webhook
        updater.start_webhook(listen="0.0.0.0",
                    port=PORT,
                    url_path=TOKEN)
        updater.bot.setWebhook('https://tlgrmbotgym.herokuapp.com/'+TOKEN)
    else:
        # enable polling
        updater.start_polling()

    updater.idle()

UPDATE

I updated my webhook:

mode = os.environ.get("MODE", "polling")
    if mode == 'webhook':

        # enable webhook
        updater.start_webhook(listen="0.0.0.0",
                    port=PORT,
                    url_path=TOKEN,
                    webhook_url= "https://tlgrmbotgym.herokuapp.com/"+TOKEN)
        
    else:
        # enable polling
        updater.start_polling()

    updater.idle()

and the bot is not responding, but I see that the logs have changed and I see new information:

2021-07-28T16:25:09.856073+00:00 heroku[web.1]: State changed from down to starting
2021-07-28T16:25:18.088650+00:00 heroku[web.1]: Starting process with command `gunicorn tlgrmbot.wsgi`
2021-07-28T16:25:21.429559+00:00 app[web.1]: [2021-07-28 16:25:21 +0000] [4] [INFO] Starting gunicorn 20.1.0
2021-07-28T16:25:21.430458+00:00 app[web.1]: [2021-07-28 16:25:21 +0000] [4] [INFO] Listening at: http://0.0.0.0:43093 (4)
2021-07-28T16:25:21.430624+00:00 app[web.1]: [2021-07-28 16:25:21 +0000] [4] [INFO] Using worker: sync
2021-07-28T16:25:21.437623+00:00 app[web.1]: [2021-07-28 16:25:21 +0000] [9] [INFO] Booting worker with pid: 9
2021-07-28T16:25:21.445747+00:00 app[web.1]: [2021-07-28 16:25:21 +0000] [10] [INFO] Booting worker with pid: 10
2021-07-28T16:25:22.355994+00:00 heroku[web.1]: State changed from starting to up
2021-07-28T16:25:25.573630+00:00 heroku[router]: at=info method=POST path="/" host=tlgrmbotgym.herokuapp.com request_id=cbab72e0-bded-4bb1-9e10-b5e820de9871 fwd="91.108.6.98" dyno=web.1 connect=1ms service=1629ms status=200 bytes=10924 protocol=https
2021-07-28T16:25:25.567572+00:00 app[web.1]: 10.41.182.161 - - [28/Jul/2021:16:25:25 +0000] "POST / HTTP/1.1" 200 10697 "-" "-"
2021-07-28T16:27:45.534385+00:00 heroku[router]: at=info method=POST path="/" host=tlgrmbotgym.herokuapp.com request_id=92bcee23-c40c-4111-9e2f-dab8d6a3faa8 fwd="91.108.6.98" dyno=web.1 connect=1ms service=17ms status=200 bytes=10924 protocol=https
2021-07-28T16:27:45.534074+00:00 app[web.1]: 10.45.182.145 - - [28/Jul/2021:16:27:45 +0000] "POST / HTTP/1.1" 200 10697 "-" "-"

logging code

import logging

logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                    level=logging.INFO)

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

this is what I see in the logs now:

021-07-28T18:45:28.807525+00:00 app[web.1]: Not Found: /{Token}
2021-07-28T18:45:28.811974+00:00 app[web.1]: 10.181.143.206 - - [28/Jul/2021:18:45:28 +0000] "POST /{Token} HTTP/1.1" 404 2230 "-" "-"
2021-07-28T18:46:28.977729+00:00 heroku[router]: at=info method=POST path="/{Token}" host=tlgrmbotgym.herokuapp.com request_id=ad50bc1b-5e0e-4e98-b599-315e425c56e7 fwd="91.108.6.98" dyno=web.1 connect=0ms service=87ms status=404 bytes=2463 protocol=https
2021-07-28T18:46:28.982557+00:00 app[web.1]: Not Found: /{Token}

Updated Logs:

2021-07-29T01:02:04.000000+00:00 app[api]: Build succeeded
2021-07-29T01:02:06.949623+00:00 app[worker.1]: 2021-07-29 01:02:06,949 - telegram.ext.dispatcher - DEBUG - Setting singleton dispatcher as <telegram.ext.dispatcher.Dispatcher object at 0x7faed70b0130>
2021-07-29T01:02:06.959355+00:00 app[worker.1]: 2021-07-29 01:02:06,959 - apscheduler.scheduler - INFO - Scheduler started
2021-07-29T01:02:06.960775+00:00 app[worker.1]: 2021-07-29 01:02:06,960 - apscheduler.scheduler - DEBUG - Looking for jobs to run
2021-07-29T01:02:06.961328+00:00 app[worker.1]: 2021-07-29 01:02:06,961 - apscheduler.scheduler - DEBUG - No jobs; waiting until a job is added
2021-07-29T01:02:06.961409+00:00 app[worker.1]: 2021-07-29 01:02:06,960 - telegram.bot - DEBUG - Entering: get_me
2021-07-29T01:02:07.248949+00:00 app[worker.1]: 2021-07-29 01:02:07,247 - telegram.bot - DEBUG - {'supports_inline_queries': False, 'username': 'CKBXFbot', 'can_join_groups': True, 'first_name': 'gym_bot', 'is_bot': True, 'can_read_all_group_messages': False, 'id': 1810662496}
2021-07-29T01:02:07.252111+00:00 app[worker.1]: 2021-07-29 01:02:07,248 - telegram.bot - DEBUG - Exiting: get_me
2021-07-29T01:02:07.252115+00:00 app[worker.1]: 2021-07-29 01:02:07,248 - telegram.ext.updater - DEBUG - Bot:1810662496:dispatcher - started
2021-07-29T01:02:07.252123+00:00 app[worker.1]: 2021-07-29 01:02:07,250 - telegram.ext.updater - DEBUG - Bot:1810662496:updater - started
2021-07-29T01:02:07.252123+00:00 app[worker.1]: 2021-07-29 01:02:07,250 - telegram.ext.updater - DEBUG - Updater thread started (webhook)
2021-07-29T01:02:07.252126+00:00 app[worker.1]: 2021-07-29 01:02:07,251 - telegram.ext.updater - DEBUG - Start network loop retry bootstrap set webhook
2021-07-29T01:02:07.252126+00:00 app[worker.1]: 2021-07-29 01:02:07,251 - telegram.ext.updater - DEBUG - Setting webhook
2021-07-29T01:02:07.252127+00:00 app[worker.1]: 2021-07-29 01:02:07,251 - telegram.bot - DEBUG - Entering: set_webhook
2021-07-29T01:02:07.256003+00:00 app[worker.1]: 2021-07-29 01:02:07,254 - telegram.ext.updater - DEBUG - Waiting for Dispatcher and Webhook to start
2021-07-29T01:02:07.257154+00:00 app[worker.1]: 2021-07-29 01:02:07,256 - telegram.ext.dispatcher - DEBUG - Dispatcher started
2021-07-29T01:02:07.344474+00:00 app[worker.1]: 2021-07-29 01:02:07,343 - telegram.bot - DEBUG - True
2021-07-29T01:02:07.344478+00:00 app[worker.1]: 2021-07-29 01:02:07,343 - telegram.bot - DEBUG - Exiting: set_webhook
2021-07-29T01:02:07.377616+00:00 app[worker.1]: 2021-07-29 01:02:07,376 - asyncio - DEBUG - Using selector: EpollSelector
2021-07-29T01:02:07.377620+00:00 app[worker.1]: 2021-07-29 01:02:07,376 - telegram.ext.utils.webhookhandler - DEBUG - Webhook Server started.
2021-07-29T01:03:04.550955+00:00 heroku[router]: at=info method=POST path="/{token}" host=tlgrmbotgym.herokuapp.com request_id=6faf2fcc-a84c-410c-8ad2-f0455dfe6121 fwd="91.108.6.98" dyno=web.1 connect=0ms service=30ms status=404 bytes=2463 protocol=https
2021-07-29T01:03:04.487802+00:00 app[web.1]: Not Found: /1810662496:AAFyVWyOr5K9CVM6XQcTWiVIG05qgSxmNDk
2021-07-29T01:03:04.488421+00:00 app[web.1]: 10.45.67.217 - - [29/Jul/2021:01:03:04 +0000] "POST /{token} HTTP/1.1" 404 2230 "-" "-"

looks like that what is not founding is the url "https://tlgrmbotgym.herokuapp.com/" + TOKEN which in the procfile is web and that somehow it has conflicts when the worker(bot.py) runs

2

Answers


  1. Chosen as BEST ANSWER

    This is how I solved the problem:

    Profile

    web: gunicorn tlgrmbot.wsgi
    worker: python manage.py bot
    

    I'm using polling at the moment, if I found a way to make it work with a webhook I will update my answer.


  2. If you’re on PTB 13.4+, you’ll have to change how you set the webhook. See https://t.me/pythontelegrambotchannel/100 and also the wiki page on webhooks.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search