在Heroku上运行Telethon bot时出现问题

t1qtbnec  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(118)

我为自己写了一个小的自动回复机器人,并使用GitHub部署到Heroku。当我运行它时,它只工作了一分钟,然后就关闭了。
这里我的代码:

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

这是我的Procfile

web: python3 bot.py

我要这个机器人不停地跑在Heroku身上。

wqnecbli

wqnecbli1#

Web进程必须在分配给它们的端口上侦听HTTP请求。如果它们没有足够快地绑定到该端口,Heroku会声明它们已经崩溃。
您的应用程序不会侦听HTTP请求。因此,不应将其声明为Web进程。此类进程通常被称为“worker”:

worker: python3 bot.py

如果您愿意,可以使用bot代替worker。实际上,除了webrelease之外的任何名称都可以。
提交修改后的Procfile并重新部署。

相关问题