@client.on(events.NewMessage 不工作:(

@client.on(events.NewMessage not working :(

我正在尝试编写一个在另一个机器人上发送垃圾邮件的机器人,我尝试阅读文档,但它不起作用:/这是我与用户配对时收到的消息:


在聊天过程中保持安静!

DATI DELL'UTENTE

随信附上留言

按钮 1

按钮 2


我需要用我在输入中键入的一条消息来响应此文本,一两秒后再输入一条消息,但代码只是停止并且甚至没有响应。这是代码:

import asyncio 
from telethon.sync import TelegramClient
from telethon import TelegramClient, events, sync
from telethon.errors.rpcerrorlist import PeerFloodError
from telethon.tl.types import InputPeerUser
import time

api_id = xxx
api_hash = 'xxx'
messaggio = input("inserisci messaggio: ")
comando = input("inserisci comando: ")
client = TelegramClient('session_name', api_id, api_hash)
client.start()

time.sleep(5)


@client.on(events.NewMessage(from_users=<botname>, pattern=' Sei stato inserito in chat con un utente!.+'))
async def handler(event):
    await event.respond("messaggio")
    time.sleep(5)
    await event.respond("comando")
    time.sleep(10)

这里有一些问题。 首先,不推荐使用 time.sleep()asyncio。然后,客户端不是 运行ning 所以你需要 运行 它。

你可以这样做:

import asyncio 
from telethon.sync import TelegramClient
from telethon import TelegramClient, events, sync
from telethon.errors.rpcerrorlist import PeerFloodError
from telethon.tl.types import InputPeerUser
import time

api_id = 123456
api_hash = "gwudshojia24153124321"
messaggio = input("inserisci messaggio: ")
comando = input("inserisci comando: ")
client = TelegramClient('session_name', api_id, api_hash)
client.start()

@client.on(events.NewMessage(from_users=<bot_name>, pattern=' Sei stato inserito in chat con un utente!.+'))
async def handler(event):
    await event.respond(messaggio)
    await asyncio.sleep(5)
    await event.respond(comando)
    await asyncio.sleep(10)

client.run_until_disconnected()