0

i am using Pyrogram to work with telegram API. I have succeed to join channel. I have a task to add message handler and receive messages in channel. But the message handler is not invoked when message arrives (i am the owner of channel)

The code:

import asyncio
from pyrogram import Client
import time
from pyrogram.handlers import MessageHandler, RawUpdateHandler

api_id = "xx"
api_hash = "xx"

def my_handler(client, message):
    message.forward("me")
    print('sent msg')

async def main():
    async with Client("my_account", api_id, api_hash) as app:
        a = await app.get_chat('test2k3')

        msg_handler = MessageHandler(my_handler)
        app.add_handler(msg_handler)

        await app.join_chat(str(a.id))
        print(f'joined chat ' + str(a.id))

        while True:
            time.sleep(2.4)

asyncio.get_event_loop().run_until_complete(main())
1
  • 1
    (Advice) use asyncio.sleep() inside async functions instead of time.sleep() Commented Apr 4, 2022 at 8:37

2 Answers 2

0

Sleeping while the client runs halts it until the sleep is over. Pyrogram itself will already keep itself alive until Ctrl and C is pressed. Remove your while True sleep loop.

Sign up to request clarification or add additional context in comments.

1 Comment

that's the problem! if he gets rid of "sleep" the process just exits!!!
0

as recommended by sudden_appearance use asyncio.sleep() inside async functions instead of time.sleep()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.