簡體   English   中英

python 電視馬拉松等待回復

[英]python telethon wait for reply

我目前有代碼可以向我的朋友發送消息

但我怎么知道他是否回復

這是我當前的代碼

請參閱注釋行

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

我怎樣才能做到這一點?

我在文檔中找到

解決方案1

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

解決方案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