Telethon的send_request_code和sign_in函数未发送

0 投票
1 回答
29 浏览
提问于 2025-04-14 15:38

我在我的Telegram机器人里有一个处理程序的代码。

这个机器人是用aiogram写的。

async def login_and_save_session(session_name, phone_number, call: types.CallbackQuery, state: FSMContext):
    try:
        await state.update_data(session_file_path=my_files.get_session_file_path(session_name))
        session_file_path = await get_session_file_path_for_session(state=state)
        client = TelegramClient(session_file_path, config.api_id, config.api_hash)
        await client.connect()
        if await client.is_user_authorized():

        if not await client.is_user_authorized():
            try:
                await client.sign_in(phone=str(phone_number))
                await handle_request_code(call, state)
                while await get_add_session_flag(state):
                    await asyncio.sleep(3) 
                code = await get_code_for_session(state)
                await client.sign_in(phone=str(phone_number), code=str(code))
            except SessionPasswordNeededError:
                await handle_request_2fa_password(call, state)
                while await get_add_session_flag(state):
                    await asyncio.sleep(3) 
                password = await get_2fa_password_for_session(state)
                await client.sign_in(password=str(password))
            except Exception as e:
                if client:
                    await client.disconnect()
                if session_file_path and os.path.exists(session_file_path):
                    my_files.delete_file(session_file_path)

从日志中我们可以看到,错误发生在尝试发送代码或进行授权的时候。

        if not await client.is_user_authorized():
            try:
                await client.sign_in(phone=str(phone_number))
                await handle_request_code(call, state)
                while await get_add_session_flag(state):
                    await asyncio.sleep(3) 
                code = await get_code_for_session(state)
                await client.sign_in(phone=str(phone_number), code=str(code))
            except SessionPasswordNeededError:
                await handle_request_2fa_password(call, state)
                while await get_add_session_flag(state):
                    await asyncio.sleep(3) 
                password = await get_2fa_password_for_session(state)
                await client.sign_in(password=str(password))
            except Exception as e:
                if client:
                    await client.disconnect()
                if session_file_path and os.path.exists(session_file_path):
                    my_files.delete_file(session_file_path)

我确定数据格式是正确的,但出现了错误 **bytes or str expected, not <class 'int'> **。

我简化了这个函数,它就能正常工作了。

async def send_code(phone_number):
    client = TelegramClient(StringSession(), api_id, api_hash)
    
    await client.connect()
    
    if not await client.is_user_authorized():
        await client.send_code_request(phone_number)
    else:
        
    await client.disconnect()

asyncio.run(send_code(phone_number))

但是我无法像上面提到的那样在机器人内部进行授权。

1 个回答

0

我自己纠正了这个错误。

我只需要在客户端更准确地指定参数就可以了……

就这样。

client = TelegramClient(session=session_file_path, api_id=config.api_id, api_hash=config.api_hash)

撰写回答