Home > front end >  Problem on running telethon bot on heroku
Problem on running telethon bot on heroku

Time:11-12

I wrote a small autoresponder bot for myself and deployed using GitHub to Heroku. When I run it, it works only one minute then shuts down.

Here my code:

import time 
import telethon 
from telethon import TelegramClient, events 
 
api_id = x
api_hash = 'x' 
phone_number = 998x 
password = 'a' 
session_file = "TelegramClient('@alixam12')" 
a = 1
 
message = "Salom"
 
if a == 1: 

    client = TelegramClient(session_file, api_id, api_hash, sequential_updates=True) 
    

   
    @client.on(events.NewMessage(incoming=True)) 
    async def handle_new_message(event): 
        if event.is_private: 
            from_ = await event.client.get_entity(event.from_id) 
            if not from_.bot: 
                print(time.asctime(), '-', event.message) 
                time.sleep(0.001) 
                await event.respond(message)
                
        print(time.asctime(), '-', 'Bot ishlamoqda...') 
        
    with client:
        client.run_until_disconnected() 
        print(time.asctime(), '-', 'stopped')

And here is my Procfile:

web: python3 bot.py

I want to this bot runs on Heroku without stopping.

CodePudding user response:

web processes must listen for HTTP requests on the port they are assigned. If they don't bind to that port quickly enough, Heroku declares that they have crashed.

Your application does not listen for HTTP requests. It should therefore not be declared as a web process. It is common for such processes to be called "workers":

worker: python3 bot.py

If you prefer, you can use bot instead of worker. Really, any name except web or release is okay.

Commit your modified Procfile and redeploy.

  • Related