You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Telethon库报错:'TelegramClient'无'get_input_peer'属性

解决Telethon报错:'TelegramClient' object has no attribute 'get_input_peer'

问题根源

最新版Telethon(v2.x及以上)已移除get_input_peer方法,该方法属于旧版(v1.x)API,新版本采用更简洁的方式处理peer参数。

解决方案

1. 替换get_input_peer调用

将代码中await client.get_input_peer('me')替换为以下任一可行方式:

  • 直接传入字符串'me'(Telethon会自动解析为当前登录用户)
  • 导入types模块,使用types.InputPeerSelf()明确指定当前用户

2. 优化登录流程(推荐)

client.start()方法已内置完整登录验证逻辑,会自动提示输入验证码,无需单独调用client.sign_in(),可移除冗余的登录代码。

修改后的完整代码

import asyncio
from telethon import TelegramClient, functions
from telethon.tl.types import InputPeerSelf  # 使用InputPeerSelf时需导入

api_id = ''
api_hash = ''
phone_number = ''

client = TelegramClient('session_name', api_id, api_hash,
                        device_model="iPhone",
                        system_version="14.8.1",
                        app_version="8.4",
                        lang_code="en",
                        system_lang_code="en-US")


async def main():
    # start()自动处理登录及验证码请求
    await client.start(phone_number)

    await client.get_dialogs()

    while True:
        try:
            # 方式1:直接使用'me'
            await client(functions.messages.SaveDraftRequest(
                'me',
                'бе',
                reply_to_msg_id=None
            ))
            
            # 方式2:使用InputPeerSelf()(需先导入对应类型)
            # await client(functions.messages.SaveDraftRequest(
            #     InputPeerSelf(),
            #     'бе',
            #     reply_to_msg_id=None
            # ))
            
            await asyncio.sleep(15)
        except Exception as e:
            print(f'Ошибка: {e}')
            await client.start(phone_number)
            continue

if __name__ == '__main__':
    asyncio.run(main())  # 替代旧循环写法,适配Python3.7+规范

额外说明

  • 处理其他对话(如用户名、群组)时,直接传入对应字符串(如'username'、群组链接)即可,Telethon会自动解析为正确的peer对象
  • 若仍有异常,可尝试安装指定稳定版本:pip install telethon==2.25.0

内容的提问来源于stack exchange,提问作者DMG DMG

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 07:30:19