Telethon,TelegramClient.Connect如何以编程方式设置身份验证代码

发布于 2025-01-24 11:59:16 字数 560 浏览 0 评论 0 原文

我正在使用Telethon在电报上自动化一些任务。 我正在尝试创建一个API,第三方用户可以在其中提供电话号码并通过API输入代码。我的电话号码零件可以正常工作,因为我允许用户通过网络服务输入他们的电话号码,这将被写入文件,然后我打开该文件并在python中获取电话号码{number},然后我然后使用以下内容连接到客户端。

client = TelegramClient(f'{number}', API_ID, API_KEY)
try:
    await client.connect()
except Exception as e:
    print('Failed to connect', e, file=sys.stderr)
    return

运行代码后,用户将输入验证代码(不在Python应用中),该代码将写入文件。

在python中,返回以下

Please enter the code you received:

我可以打开包含{code}的验证代码的文件 但是我如何使用{code}回复'请输入您收到的代码:'

谢谢

I am using telethon to automate some tasks on Telegram.
I am trying to create an API where third party users can provide phone number and enter code through an api. I have got the phone number part working, As I allow users to input their phone number through a webservice, this gets wrote to a file, then I am opening that file and fetching the phone number in python which is {number}, I then connect to the client using below.

client = TelegramClient(f'{number}', API_ID, API_KEY)
try:
    await client.connect()
except Exception as e:
    print('Failed to connect', e, file=sys.stderr)
    return

Once the code is run the user enters the verification code (not in the python app) which gets wrote to a file.

And in python the following is returned

Please enter the code you received:

I can open the file which contains the verification code which as {code}
but how do I use {code} to reply to 'Please enter the code you received:'

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故事灯 2025-01-31 11:59:16

我认为您在电报上获得了代码,而不是在文件中获取代码

I think that you get the code on telegram and not in the file

鸠书 2025-01-31 11:59:16

这是可能的,但这将非常复杂,因为代码已发送到另一个设备。您可以编写自定义电报客户端,以将此代码发送到您的程序,但是它太复杂了,在99.9%的情况下,您不需要它。

编辑:

如果您已经有了此代码,请在 code 变量中说,您可以尝试使用方法 sign_in()而不是 connect()

try:
    await client.sign_in(number, code)
except Exception as e:
    print('Failed to connect', e, file=sys.stderr)
    return

It is possible, but this will be very complex as the code is sent to another device. You can write custom Telegram client that will send this code to your program, but it is too complex and in 99.9% of cases you won't need it.

Edit:

If you already have this code, let's say in code variable, you can try to use method sign_in() instead of connect()

try:
    await client.sign_in(number, code)
except Exception as e:
    print('Failed to connect', e, file=sys.stderr)
    return

Reference for sign_in() in docs

西瑶 2025-01-31 11:59:16

您可以尝试一下:

phone = "+123456789"

client = TelegramClient(phone, api_id, api_hash)
client.connect()
client.sign_in(phone)  # send code                                                 
try:
   code = input('enter code: ') # here instead of using input('enter code: ') you can just pass in the code they gave.
   client.sign_in(phone=phone, code=code)
   print(f'Successfully signed in as: {client.get_me().first_name} {client.get_me().last_name}')
except Exception as e:
   print(f'Sign-in failed: {e}')

You can try this:

phone = "+123456789"

client = TelegramClient(phone, api_id, api_hash)
client.connect()
client.sign_in(phone)  # send code                                                 
try:
   code = input('enter code: ') # here instead of using input('enter code: ') you can just pass in the code they gave.
   client.sign_in(phone=phone, code=code)
   print(f'Successfully signed in as: {client.get_me().first_name} {client.get_me().last_name}')
except Exception as e:
   print(f'Sign-in failed: {e}')

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文