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

Telethon正确获取自身信息的方法及报错解决

修复Telethon中AttributeError: 'coroutine' object has no attribute 'stringify'错误

错误原因

client.get_me()是Telethon提供的异步协程方法,直接调用不会立即执行并返回用户对象,而是返回一个coroutine对象,自然没有.stringify()方法,因此触发属性错误。

修复方案

方案1:使用async/await语法(推荐)

这是现代Python异步编程的标准写法,代码结构更清晰:

from telethon import TelegramClient
import asyncio

api_id = my_id  # 替换为你的API ID
api_hash = 'my_hash'  # 替换为你的API Hash

async def main():
    client = TelegramClient('Test2Session', api_id, api_hash)
    await client.start()
    
    # 等待协程执行完成,获取用户对象
    me = await client.get_me()
    print(me.stringify())

if __name__ == '__main__':
    asyncio.run(main())

方案2:使用run_until_complete(兼容旧版Python)

如果你的Python版本不支持asyncio.run(),可以用这种写法:

from telethon import TelegramClient

api_id = my_id  # 替换为你的API ID
api_hash = 'my_hash'  # 替换为你的API Hash

client = TelegramClient('Test2Session', api_id, api_hash)
with client:
    # 让事件循环等待协程完成
    me = client.loop.run_until_complete(client.get_me())
    print(me.stringify())

关键说明

两种方案的核心都是等待get_me()协程执行完毕,拿到真实的用户对象后再调用.stringify()方法。另外要确保api_id和api_hash是你在Telegram开发者平台申请的有效值。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.03 05:33:14