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")
我该怎么做?
我在doc
中找到
解决方案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()
我目前有代码可以向我的朋友发送消息
但是我怎么知道他有没有回复
这是我当前的代码
请看注释行
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")
我该怎么做?
我在doc
中找到解决方案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()