1

I'm trying to send message from my bot using pyrogram in Colab, But I can't use main Thread because pyrogram will think I'm running my code in async function and all functions became coroutines.

so I tried to run it from another thread but the problem is it stucks in bot.start() ,when a cell is running.

when all cells finished running. it will run fine and send the message.

The code:

from pyrogram import Client
import threading

bot=Client("Me", bot_token=BOT_TOKEN, api_id=APP_ID, api_hash=API_HASH,no_updates=True)

def main():
  bot.start()
  bot.send_message(chat_id="...",text="how")
  bot.stop()

t=threading.Thread(target=main)
t.start()

I looked at pyrogram module But I couldn't find out why this happening

Darkido A
  • 21
  • 3
  • I found out only pyrogram has this problem I tried [Telethon](https://github.com/LonamiWebs/Telethon) and it worked fine – Darkido A Sep 23 '21 at 13:54

1 Answers1

0

I never worked with Google Colab so I can't tell you if this would work, but you could try and make your code like this:

from pyrogram import Client

bot = Client(..)

async def main():
    async with bot:
        bot.send_message(..)

bot.run(main())
ColinShark
  • 1,047
  • 2
  • 10
  • 18
  • it will error "This event loop is already running" same as ```asyncio.run()``` it can be solved with ```nest_asyncio``` but It stucks in loop sometimes and cause session crash so I'm trying to find another way (which we don't need to use ```nest_asyncio```) – Darkido A Sep 23 '21 at 12:56