LonamiWebs / Telethon

Pure Python 3 MTProto API Telegram client library, for bots too!
https://docs.telethon.dev
MIT License
9.74k stars 1.39k forks source link

send_code_request(force_sms=True) sending 2 sms #3006

Closed ysomad closed 2 years ago

ysomad commented 3 years ago

Checklist

I'm trying to implement telegram auth with fastapi and getting 2 sms with force_sms = True

Code that causes the issue

async def get_telegram_client(session: str = None) -> TelegramClient:
    return TelegramClient(
        StringSession(session),
        api_id=settings.TELEGRAM_API_ID,
        api_hash=settings.TELEGRAM_API_HASH
    )

@router.post('/code')
async def send_authorizarion_code(payload: TelegramSendCode):
    client = await get_telegram_client()
    await handle_send_code_request(client, payload)
    session = client.session.save()
    return {'phone': payload.phone, 'session': session}

async def handle_send_code_request(
        client: TelegramClient,
        payload: TelegramSendCode) -> Union[None, JSONResponse]:
    await client.connect()
    try:
        await client.send_code_request(
            payload.phone, force_sms=payload.force_sms)
    except FloodWaitError as e:
        return JSONResponse(status_code=420, content={
            'phone': e.request.phone_number,
            'cooldown_seconds': e.seconds
        })
    except PhoneNumberInvalidError:
        return JSONResponse(status_code=406, content={'phone': payload.phone})
ysomad commented 3 years ago

Requested 2 codes and there is the result image