ARTICLE · INTELLIGENCE

战地情报 · 详情页

来自尧图项目组的一线实战观察与深度解析

aiogram 实战:使用 getMyDescription 读取机器人多语言简介与 BotDescription 详解

aiogram 实战:使用 getMyDescription 读取机器人多语言简介与 BotDescription 详解 后端即时通讯API设计【免费下载链接】aiogramaiogram is a modern and fully asynchronous framework for Telegram Bot API written in Python using asyncio项目地址https://gitcode.com/gh_mirrors/ai/aiogram点击查看免费下载本篇技术指南以 aiogram 仓库中 get_my_description.rst 为骨架系统讲解 Telegram Bot API 的getMyDescription方法在 aiogram 中的完整用法包括直接作为Bot方法调用、以GetMyDescription对象形式调用、language_code多语言参数语义以及返回值BotDescription的结构。读完本文你将掌握读取并配合setMyDescription设置机器人多语言简介的完整方案并能理解 aiogram 中所有 API 方法对象化的底层设计。一、方法概览getMyDescription 是做什么的getMyDescription用于获取当前机器人在指定用户语言下的描述description。该描述会显示在用户与机器人私聊的聊天窗口中当聊天为空时展示是机器人在默认欢迎场景中的自我介绍文本。在 aiogram 中该方法对应API 方法名getMyDescription即 Telegram Bot API 的原始调用名返回类型BotDescription源码位置aiogram/methods/get_my_description.py从源码可以看到其类型声明非常简洁class GetMyDescription(TelegramMethod[BotDescription]): Use this method to get the current bot description for the given user language. Returns :class:aiogram.types.bot_description.BotDescription on success. __returning__ BotDescription __api_method__ getMyDescription language_code: str | None None A two-letter ISO 639-1 language code or an empty string两个类属性定义了方法的核心元信息属性值含义__returning__BotDescription声明该方法成功时返回的对象类型驱动类型推断与响应解析__api_method__getMyDescription实际发送到 Telegram API 的 HTTP 方法名language_code 参数语义language_code是该方法唯一的请求参数类型为str | None默认值为None传入两位 ISO 639-1 语言代码如en、zh、uk表示获取该语言专属的简介传入空字符串表示获取默认简介即未设置专属语言简介时回退使用的通用描述不传None时Telegram 会依据用户语言自动选择最合适的描述。二、返回值 BotDescription 结构成功调用后返回一个BotDescription对象其定义位于 aiogram/types/bot_description.pyclass BotDescription(TelegramObject): This object represents the bots description. description: str The bots description该类型只有一个必填字段description: str机器人的描述文本。注意这是必填字段无默认值因为getMyDescription成功返回时必然携带描述内容。它继承自TelegramObjectPydantic 模型基类因此可以直接进行属性访问、序列化与校验。三、使用方法一作为 Bot 方法直接调用原文档给出的第一种也是最常用的调用方式是直接挂在Bot实例上以协程方式调用。aiogram 在 aiogram/client/bot.py#L4324-L4342 中为每个 API 方法都提供了同名异步封装async def get_my_description( self, language_code: str | None None, request_timeout: int | None None, ) - BotDescription: call GetMyDescription( language_codelanguage_code, ) return await self(call, request_timeoutrequest_timeout)因此实际使用中只需from aiogram import Bot bot Bot(tokenYOUR_BOT_TOKEN) # 获取默认描述 result: BotDescription await bot.get_my_description() print(result.description) # 获取指定语言的描述 result_en: BotDescription await bot.get_my_description(language_codeen) result_zh: BotDescription await bot.get_my_description(language_codezh) # 显式请求默认回退描述 result_default: BotDescription await bot.get_my_description(language_code)注意封装签名中除了language_code还额外暴露了request_timeout参数用于覆盖默认的请求超时时间单位为秒这在网络环境不稳定时很有用。四、使用方法二以 Method 对象形式调用原文档同时说明了方法即对象的调用范式。aiogram 中每个 API 方法都被建模为独立的 Pydantic 对象GetMyDescription也不例外。两种导入方式# 方式一从子模块直接导入 from aiogram.methods.get_my_description import GetMyDescription # 方式二从 methods 包导入官方推荐的别名路径 from aiogram.methods import GetMyDescription第二种方式之所以可行是因为 aiogram/methods/init.py 中统一导出了GetMyDescription并已注册到__all__列表。创建对象后绑定 Bot 实例调用对象创建与调用分离适合将请求参数与执行时刻解耦的场景from aiogram import Bot from aiogram.methods import GetMyDescription bot Bot(tokenYOUR_BOT_TOKEN) # 1) 构造方法对象此时尚未绑定 bot method GetMyDescription(language_codeen) # 2) 通过 await bot(...) 执行 —— 原文档 With specific bot 的写法 result: BotDescription await bot(method)直接 await 方法对象绑定后调用GetMyDescription继承自TelegramMethod定义见 aiogram/methods/base.py其底层实现了__await__、emit()与as_()机制# 方式三先绑定 bot再直接 await 对象本身 result: BotDescription await GetMyDescription(language_codeen).as_(bot) # 或分步写 method GetMyDescription(language_codeen) method.as_(bot) result await method这里的调用链是__await__→emit(bot)→bot(method)→bot.session(...)发送真实请求。若对象未绑定任何 Bot 实例就直接awaitaiogram/methods/base.py 中的__await__会抛出RuntimeError提示你显式调用await bot(method)或先使用method.as_(bot)绑定。as_方法来自 aiogram/client/context_controller.py 的BotContextController基类它通过私有属性_bot持有 Bot 引用并暴露只读的bot属性。对象化的内部原理纵深补充从 aiogram/methods/base.py 可以进一步看到方法对象的完整生命周期请求模型化TelegramMethod继承BaseModelPydantic并配置extraallow、populate_by_nameTrue允许灵活传入附加字段。UNSET 哨兵值清理模型初始化前会执行remove_unset校验器过滤掉值为UNSET_TYPE的字段避免哨兵值污染请求数据——这一点在parse_mode等可选默认值字段上尤为重要。响应泛型解析__returning__声明的返回类型会被用于将 Telegram 返回的 JSON 结果解析为BotDescription等具体类型请求失败时则组装为带ok、error_code、description、parameters字段的Response模型。五、配套方法设置与短简介本地化闭环getMyDescription通常与setMyDescription成对使用构成读取-设置闭环。仓库中相关的三个方法一并列出方法用途返回类型源码getMyDescription读取指定语言下的机器人描述BotDescriptionaiogram/methods/get_my_description.pysetMyDescription设置机器人描述0-512 字符boolaiogram/methods/set_my_description.pygetMyShortDescription读取机器人短简介个人主页展示BotShortDescriptionaiogram/methods/get_my_short_description.pysetMyShortDescription设置机器人短简介0-120 字符boolaiogram/client/bot.py设置多语言简介的完整示例from aiogram import Bot bot Bot(tokenYOUR_BOT_TOKEN) # 设置默认描述 await bot.set_my_description( descriptionHi! Im a Telegram bot built with aiogram. Send /start to begin., ) # 设置中文专属描述只会对语言设置为中文的用户生效 await bot.set_my_description( description你好我是使用 aiogram 构建的 Telegram 机器人发送 /start 开始使用。, language_codezh, ) # 设置英文专属描述 await bot.set_my_description( descriptionHello! Im a Telegram bot powered by aiogram., language_codeen, ) # 读取验证 default: BotDescription await bot.get_my_description() zh_desc: BotDescription await bot.get_my_description(language_codezh) print(zh_desc.description) # 删除某语言的专属描述传空字符串 await bot.set_my_description(description, language_codezh)几点来自源码 docstring 的语义要点见 aiogram/methods/set_my_description.pydescription长度为0-512 字符传入空字符串可删除指定语言的专属描述language_code为空时描述会应用于所有没有专属描述语言的用户短简介short_description长度限制为0-120 字符显示在机器人主页并随分享链接发送。六、测试验证如何确认调用行为仓库中为该方法提供了单元测试tests/test_api/test_methods/test_get_my_description.pyfrom aiogram.methods import GetMyDescription from aiogram.types import BotDescription from tests.mocked_bot import MockedBot class TestGetMyDescription: async def test_bot_method(self, bot: MockedBot): prepare_result bot.add_result_for( GetMyDescription, okTrue, resultBotDescription(descriptionTest) ) response: BotDescription await bot.get_my_description() bot.get_request() assert response prepare_result.result该测试揭示了三点可复用的结论MockedBot 机制通过add_result_for(GetMyDescription, okTrue, result...)预置响应无需真实网络请求即可测试方法行为这也是 aiogram 官方测试框架 tests/mocked_bot.py 的通用模式返回类型一致性bot.get_my_description()的返回值被解析为BotDescription与预置结果逐字段相等请求确认bot.get_request()用于校验请求确实被发出保证方法体执行了完整的调用链。七、方法索引与文档生态该方法的 API 文档已登记在 docs/api/methods/index.rst 的方法索引中与get_my_short_description、set_my_description等相邻并由 docs/api/methods/get_my_description.rst 通过automodule指令自动从源码 docstring 生成渲染内容文档与源码实现保持单一事实来源。仓库还维护了多语言翻译文档如 docs/locale/uk_UA/LC_MESSAGES/api/methods/get_my_description.po方便国际化社区维护。八、实战小结三种调用方式对比调用方式代码形态适用场景Bot 方法推荐await bot.get_my_description(language_codeen)大多数业务代码语义直观、支持request_timeout方法对象 bot 执行await bot(GetMyDescription(language_codeen))需要先构造/传递请求参数对象的场景方法对象绑定后 awaitawait GetMyDescription(language_codeen).as_(bot)装饰器/工具函数中把方法对象当作可等待对象传递getMyDescription虽然是一个参数极简的只读方法但在 aiogram 中完整展现了框架的核心设计哲学——所有 Telegram API 方法都是一等公民对象统一继承TelegramMethod、声明__api_method__与__returning__、支持直接await、并自动获得类型提示与响应解析能力。掌握了它也就掌握了 aiogram 全部 200 API 方法对象化的通用调用规律。赞分享后端即时通讯API设计【免费下载链接】aiogramaiogram is a modern and fully asynchronous framework for Telegram Bot API written in Python using asyncio项目地址https://gitcode.com/gh_mirrors/ai/aiogram点击查看免费下载相关推荐aiogram 删除机器人命令列表 deleteMyCommands作用域与语言参数的完整实战指南aiogram 删除机器人命令列表 deleteMyCommands作用域与语言参数的完整实战指南 deleteMyCommands 是 Telegram B后端即时通讯API设计React SaaS Template高级功能开发图片裁剪、表情输入等组件实现React SaaS Template高级功能开发图片裁剪、表情输入等组件实现 React SaaS Template是一个基于React和Materialaiogram 中 getMe / get_me 方法全解析验证 Bot 令牌与读取机器人基础信息aiogram 中 getMe / get_me 方法全解析验证 Bot 令牌与读取机器人基础信息 本篇技术指南围绕 aiogram 框架中的 getMe 后端即时通讯API设计上一篇rust-blog 精读2024 年的 Rust 学习指南从零基础到熟练的 19–30 小时实战路线图下一篇Lenovo Legion Toolkit轻量级控制工具完全指南与系统优化方案创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
RELATED READING

延伸阅读

更多一线实战笔记与深度复盘,助您持续精进