pythonauthenticationsmstelethontelegram-api

How to ensure receiving an SMS verification code from Telegram using Telethon?


I'm currently working on a Python project where I need to authenticate a user via Telegram's API using the Telethon library. I intend to receive the authentication code via SMS, but I consistently receive a voice call instead. I'm looking for a way to force or ensure that Telegram sends the verification code through SMS.

Here's the relevant part of my code:

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!")

Despite setting force_sms=True, the verification code still not coming via a SMS. I've checked the Telegram settings to ensure that the phone number is correct and that SMS notifications are enabled.

Questions:

  1. How can I modify my Telethon script to ensure I receive the SMS instead of a voice call?
  2. Are there specific conditions under which Telegram decides to send a voice call instead of SMS, and can they be influenced programmatically? Thank you for any insights or suggestions you can provide!

Solution

  • Unofficial clients can no longer receive SMS. Telethon v1 still has the force_sms parameter to not break those that were using it, but it doesn't do anything.

    Users that signed for an API ID/hash over a year ago (start of 2023) would have received this message via Telegram letting them know:

    Telegram API Update. Hello —,. Thank you for contributing to the open Telegram ecosystem by developing your app, —.

    Please note that due to recent updates to Telegram's handling of SMS and the integration of new SMS providers like Firebase, we are changing the way login codes are handled in third-party apps based on the Telegram API.

    Starting on 18.02.2023, users logging into third-party apps will only be able to receive login codes via Telegram. It will no longer be possible to request an SMS to log into your app – just like when logging into Telegram's own desktop and web clients.

    Exactly like with the Telegram Desktop and Web apps, if a user doesn’t have a Telegram account yet, they will need to create one first using an official mobile Telegram app.

    We kindly ask you to update your app’s login and signup interfaces to reflect these changes before they go live on 18.02.2023 at 13:00 UTC.

    This change will not significantly affect users since, according to our research, the vast majority of third-party app users also use official Telegram apps. In the coming months, we expect to offer new tools for third-party developers that will help streamline the login process.