如何使用 Telethon... 登录多个帐户? [英] How can I login multiple accounts using telethon...?

查看:202
本文介绍了如何使用 Telethon... 登录多个帐户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个需要使用多个电报帐户登录的 python 脚本.我不想为每个帐户运行单独的脚本.我正在使用 TELETHON.我知道在 Telethon 中有类似 create_new_connection 的东西,但我不知道它如何帮助我.有没有一种方法可以让我只使用一个 python 脚本并使用多个帐户登录..?(如果可能,请包含要在答案中使用的代码片段)

I am trying to build a python script which requires login with multiple telegram accounts. I don't want to run a separate script for each account.I am using TELETHON. I know that there is something like create_new_connection in telethon, but I don't know how it can help me. Is there a way by which I can use just one python script and login using several accounts..? (If possible please include the code snippet to use in your answer)

推荐答案

创建客户端时,传递的第一个参数是会话文件名.不同的会话可以有不同的客户端登录,因此创建多个客户端就足够了,每个客户端都有不同的会话文件名,以便它们使用不同的帐户:

When you create a client, the first parameter you pass is the session file name. Different sessions can have different clients logged in, so it's enough to create multiple clients each with a different session file name for them to use different accounts:

user1 = TelegramClient('user1', api_id, api_hash)
user2 = TelegramClient('user2', api_id, api_hash)

当然,您也可以使用列表来轻松支持任意数量的客户端.您如何处理这些客户端取决于您,尽管您通常只会利用 asyncioPython 文档 - 并发运行任务:

Of course, you could also use a list to trivially support an arbitrary number of clients. What you do with these clients is up to you, although you will generally just take advantage of asyncio as explained in the Python documentation - Running Tasks Concurrently:

import asyncio

async def work(client):
    async with client:
        me = await client.get_me()
        print('Working with', me.first_name)

async def main():
    await asyncio.gather(
        work(TelegramClient('user1', api_id, api_hash)),
        work(TelegramClient('user1', api_id, api_hash)),
    )

asyncio.run(main())

当然,您可以为每个代码运行单独的代码、使用条件等.这在很大程度上取决于您的需求,而这些内容也会影响您的设计.

You could, of course, run separate code for each, use conditionals, etc. It depends a lot on your needs, and what these are will influence your design too.

这篇关于如何使用 Telethon... 登录多个帐户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆