简体   繁体   English

python 电视马拉松等待回复

[英]python telethon wait for reply

I currently have code to send message to my friend我目前有代码可以向我的朋友发送消息

but how can I know if he reply但我怎么知道他是否回复

this is my current code这是我当前的代码

please see the commented line请参阅注释行

from telethon import TelegramClient

api_id = '1234567'
api_hash = 'MYHASH'
client = TelegramClient('session', api_id, api_hash)
client.start(phone_number)
destination_user_username='friend'
entity=client.get_entity(destination_user_username)
client.send_message(entity=entity,message="hello")
#if he reply hi
client.send_message(entity=entity,message="have a nice day")

how can I do this?我怎样才能做到这一点?

I found in the doc我在文档中找到

solution 1解决方案1

for message in client.iter_messages(chat):
    print(message)

solution 2解决方案2

api_id = '1234567'
api_hash = 'MYHASH'
with TelegramClient('session') as client:
    destination_user_username='friend'
    entity=client.get_entity(destination_user_username)
    client.send_message(entity=entity,message="hello")

    from telethon import events
    @client.on(events.NewMessage(pattern='hi'))
    async def handler(event):
        # here received messgae, do something with event
        print(dir(event)) # check all possible methods/operations/attributes
        # reply once and then disconnect
        await event.reply("have a nice day")
        await client.disconnect()

    client.run_until_disconnected()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM