2

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?

Jayvee Embog
  • 47
  • 2
  • 6
  • Can you provide some more context to your question? What is your "client"? Providing enough context that somebody could take what you've written and run at least parts of it on their own in order to try to offer a solution is typically going to help you get a answer a lot faster. – Tristen Mar 20 '21 at 06:46
  • i edit it please help me – Jayvee Embog Mar 20 '21 at 06:49
  • yes it is a package – Jayvee Embog Mar 20 '21 at 06:56
  • 1
    So what happened after you put [`python telethon telegram reply`](https://duckduckgo.com/?q=python+telethon+telegram+reply) into a search engine and looked at [the example code on GitHub that comes up near the top](https://github.com/ibrdrahim/telegram-auto-reply/blob/master/main.py)? I assume that you tried writing some code based on that, or maybe even just using the project as is (according to its README), but I don't see anything about that in your question. – Karl Knechtel Mar 20 '21 at 07:39
  • See https://stackoverflow.com/a/62246569/4759433 – Lonami Mar 25 '21 at 19:34

1 Answers1

4

I found in the doc

solution 1

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

solution 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()
BAKE ZQ
  • 765
  • 6
  • 22
  • thank you very much friend solution 2 works. but it not stop. i want is only wait for 1 reply – Jayvee Embog Mar 20 '21 at 07:15
  • How about the second solution, I didn't check the doc carefully, But it seems that you can get every information from the `event` object, like check the sender, check the messaged received, one way you need to care is that some method of event must be called with `await` – BAKE ZQ Mar 20 '21 at 07:17
  • 1
    A simple way is to add a `raise ValueError` at the end of the function, but there may be some rightful way to end the event loop with codes like `await client.disconnect()`. – BAKE ZQ Mar 20 '21 at 07:19
  • 1
    You should check the doc by yourself, I didn't use telephon before, I just quickly glanced at the doc. – BAKE ZQ Mar 20 '21 at 07:24
  • sorry its my first time too – Jayvee Embog Mar 20 '21 at 07:26
  • wow thank you so much. it works fine now. thank you again. i forget to add await before client.disconnect() that what cause error – Jayvee Embog Mar 20 '21 at 07:32