1

我正在尝试使用 Colab 中的 pyrogram 从我的机器人发送消息,但我不能使用主线程,因为 pyrogram 会认为我正在异步函数中运行我的代码,并且所有函数都变成了协程。

所以我尝试从另一个线程运行它,但问题是它在bot.start() 一个单元运行时卡住了。

当所有单元完成运行时。它会运行良好并发送消息。

编码:

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

我查看了pyrogram 模块但我不知道为什么会发生这种情况

4

1 回答 1

0

我从未与 Google Colab 合作过,所以我不能告诉你这是否可行,但你可以尝试让你的代码像这样:

from pyrogram import Client

bot = Client(..)

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

bot.run(main())
于 2021-09-23T12:42:11.793 回答