'Pyrogram telegram API (not bot api)

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


Solution 1:[1]

use asyncio.sleep() inside async functions instead of time.sleep()

Solution 2:[2]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sergey Efimov
Solution 2 ColinShark