Telethon问题:RuntimeWarning:从未等待协程'send_message'

时间:2020-06-12 16:05:21

标签: python-3.x telethon

请帮助我解决我的代码中的错误:

from telethon import TelegramClient, sync, events

#Ids
api_id = 99999
api_hash = 'secret_word'

client = TelegramClient('secret_chat', api_id, api_hash)

@client.on(events.NewMessage(chats=('user1')))
async def normal_handler(event):
    s_user_id=event.message.to_dict()['from_id']
    if s_user_id != 99999999:
        client.send_message('user3', 'Test')



@client.on(events.NewMessage(chats=('user2')))
async def normal_handler(event):
    s_user_id=event.message.to_dict()['from_id']
    if s_user_id != 99999999:
        client.send_message('user3', 'Test')

client.start()

client.run_until_disconnected()

当user1或user2给我发送消息时,我的程序出现错误:

RuntimeWarning:从未等待协程'send_message'
client.send_message('user3','Test')

1 个答案:

答案 0 :(得分:0)

使用下一个代码固定:

from telethon import TelegramClient, sync, events

#Ids
api_id = 99999
api_hash = 'secret_word'

client = TelegramClient('secret_chat', api_id, api_hash)

@client.on(events.NewMessage(chats=('user1')))
async def normal_handler(event):
    s_user_id=event.message.to_dict()['from_id']

    await client.send_message('user3', 'Test')

    if s_user_id != 99999999:
        client.send_message('user3', 'Test')



@client.on(events.NewMessage(chats=('user2')))
async def normal_handler(event):

    s_user_id=event.message.to_dict()['from_id']

    await client.send_message('user3', 'Test')

    if s_user_id != 99999999:
        client.send_message('user3', 'Test')

client.start()

client.run_until_disconnected()

但是现在,client.send_message甚至可以工作s_user_id = 99999999。 代码以某种方式忽略了s_user_id!= 99999999: