简体   繁体   English

如何将 Telethon 脚本集成到我的事件循环中?

[英]How to intergrate telethon script into my event loop?

I have two bots, one is using pydle for IRC, like:我有两个机器人,一个是为 IRC 使用pydle ,比如:

async def start_ircbot ():
    try:
        client = MyOwnBot(NICK, 
                       realname=REALNAME,
                       sasl_username=SASL_USERNAME,
                       sasl_password=SASL_PASSWORD,
                       sasl_identity=SASL_IDENTITY,)

        loop = asyncio.get_event_loop()
        asyncio.ensure_future(client.connect(HOST, PORT, tls=True, tls_verify=False), loop=loop)
        loop.run_forever()
        loop.close()
    except Exception as e:
        print (e)

and another is using telethon for Telegram:另一个正在使用telethon进行 Telegram:

@client.on(events.NewMessage)
async def my_event_handler(event):
    ...

async def start_client ():
    print ("Telegram monitor started...")
    await client.start()
    await client.run_until_disconnected()

Both of them work without problem separately.他们两个单独工作没有问题。

Now, I want to integrate both of them, I tried to launch both of them in my main function like this,现在,我想整合它们,我尝试在我的main function 中像这样启动它们,

import Notifier

...

async def main():
    await asyncio.gather (Notifier.start_client (), start_ircbot ())

asyncio.run(main())

It starts without issue but my_event_handler seems never to get new messages.它开始时没有问题,但my_event_handler似乎永远不会收到新消息。 If I swap the order of functions:如果我交换功能的顺序:

await asyncio.gather (start_ircbot (), Notifier.start_client ())

The script will be stuck at launching, I suspect it has to be something within events loops and tried some different methods but without luck, could anyone shed light on this for me?该脚本将在启动时卡住,我怀疑它必须是事件循环中的某些东西并尝试了一些不同的方法,但没有运气,有人可以为我解释一下吗?

Newer Python versions are removing the loop parameter from most methods, so you should try to avoid using it.较新的 Python 版本正在从大多数方法中删除loop参数,因此您应该尽量避免使用它。 As long as you don't use asyncio.run (which creates a new loop) or you don't create a new loop yourself, both libraries should be using the default loop from the main thread (I can't speak for pydle , but Telethon does this).只要您不使用asyncio.run (创建一个新循环)或者您自己不创建一个新循环,两个库都应该使用主线程中的默认循环(我不能说pydle ,但 Telethon 会这样做)。

As long as the asyncio event loop is running, Telethon should have no trouble receiving updates.只要asyncio事件循环正在运行,Telethon 接收更新应该没有问题。 You can use client.loop to make sure it's using the same loop:您可以使用client.loop来确保它使用相同的循环:

tlclient = TelegramClient(...)
irclient = MyOwnBot(...)

@tlclient.on(events.NewMessage)
async def my_event_handler(event):
    ...

async def main():
    await tlclient.start()
    await irclient.connect(HOST, PORT, tls=True, tls_verify=False), loop=tlclient.tlclient)
    await tlclient.run_until_disconnected()  # similar to loop.run_forever() but stops when the client disconnects

client.loop.run_until_complete(main())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM