如何确保使用 Telethon 收到来自 Telegram 的短信验证码?

问题描述 投票:0回答:1

我目前正在开发一个 Python 项目,我需要使用 Telethon 库通过 Telegram 的 API 对用户进行身份验证。我打算通过短信接收验证码,但我总是收到语音电话。我正在寻找一种方法来强制或确保 Telegram 通过短信发送验证码。

这是我的代码的相关部分:

from telethon.sync import TelegramClient
from telethon.errors import SessionPasswordNeededError, FloodWaitError

api_id = 'your_api_id'  # Replace with your actual API ID
api_hash = 'your_api_hash'  # Replace with your actual API hash
phone_number = '+1234567890'  # Replace with the phone number you're using

client = TelegramClient('anon', api_id, api_hash)

async def send_code_via_sms():
    await client.connect()
    if not await client.is_user_authorized():
        try:
            # Explicitly request the code via SMS
            result = await client.send_code_request(phone=phone_number, force_sms=True)
            print("Code request sent via SMS. Please check your messages.")
            # Proceed with handling the code
            code = input("Enter the code: ")
            await client.sign_in(phone=phone_number, code=code, phone_code_hash=result.phone_code_hash)
            print("Signed in successfully!")
        except FloodWaitError as e:
            print(f"Need to wait for {e.seconds} seconds before trying again.")
        except Exception as e:
            print(f"Failed to sign in: {e}")
    else:
        print("Already authorized!")

尽管设置了force_sms=True,验证码仍然没有通过短信发送。我已检查 Telegram 设置以确保电话号码正确并且已启用短信通知。

问题:

  1. 如何修改我的 Telethon 脚本以确保我收到短信而不是语音通话?
  2. Telegram 是否在特定条件下决定发送语音通话而不是短信?它们是否会受到编程方式的影响? 感谢您提供的任何见解或建议!
python authentication sms telethon telegram-api
1个回答
0
投票

非官方客户端无法再接收短信。 Telethon v1 仍然具有

force_sms
参数,以免破坏正在使用它的人,但它没有做任何事情。

一年多前(2023 年初)签署 API ID/哈希的用户将通过 Telegram 收到此消息,让他们知道:

电报 API 更新。你好 -,。感谢您对开放做出的贡献 通过开发您的应用程序来构建 Telegram 生态系统,—.

请注意,由于最近更新了 Telegram 的短信处理方式 以及 Firebase 等新 SMS 提供商的集成,我们正在 更改第三方应用程序中处理登录代码的方式 Telegram API。

自2023年2月18日起,用户登录第三方应用程序仅 能够通过 Telegram 接收登录代码。它将不再是 可以请求短信登录您的应用程序 – 就像以前一样 登录 Telegram 自己的桌面和 Web 客户端。

与 Telegram 桌面和 Web 应用程序完全相同,如果用户不这样做 还没有 Telegram 帐户,他们需要先使用以下命令创建一个帐户 官方移动 Telegram 应用程序。

我们恳请您将应用程序的登录和注册界面更新为 在 UTC 时间 2023 年 2 月 18 日 13:00 上线之前反映这些更改。

这一变化不会对用户产生重大影响,因为根据 根据我们的研究,绝大多数第三方应用程序用户也使用 官方 Telegram 应用程序。在接下来的几个月中,我们预计将提供新的 面向第三方开发人员的工具,有助于简化登录 过程。

© www.soinside.com 2019 - 2024. All rights reserved.