- 后端
- 即时通讯
- API设计
【免费下载链接】aiogram
aiogram is a modern and fully asynchronous framework for Telegram Bot API written in Python using asyncio
导读
getUserProfileAudios是 Telegram Bot API 中用于获取指定用户个人资料页展示音频列表的方法,在 aiogram 中以Bot.get_user_profile_audios异步方法、GetUserProfileAudios方法对象以及User.get_profile_audios快捷方式三种形式提供给开发者。本文以 aiogram 官方 API 文档为骨架,结合 get_user_profile_audios.py 等源码实现,系统讲解该方法的参数语义、返回值结构、三种调用方式与底层执行链路,帮助你准确、高效地在机器人中读取用户的个人资料音频。
方法概览:返回什么、何时使用
getUserProfileAudios方法用于获取目标用户个人资料中展示的音频列表,返回一个 UserProfileAudios 对象。该方法在 aiogram 中的官方定义为:
Use this method to get a list of profile audios for a user. Returns a
UserProfileAudiosobject.
对应 Telegram Bot API 文档中的getUserProfileAudios接口(来源见 get_user_profile_audios.py)。在 aiogram 的 API 文档 中,它被描述为:
Returns: UserProfileAudios适用场景包括但不限于:展示用户个人资料页的音频(如语音介绍、音乐收藏),或配合Audio的file_id进一步复用音频文件(转发、设为个人资料音频等)。调用该接口不需要额外权限,但必须以真实存在的用户user_id为参数。
参数详解:user_id、offset 与 limit
从 get_user_profile_audios.py 的字段定义可以完整看到该方法全部三个参数:
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
user_id | int | 是 | 目标用户的唯一标识符(Unique identifier of the target user) |
offset | int \| None | 否 | 要返回的第一条音频的序号,默认返回全部音频(Sequential number of the first audio to be returned. By default, all audios are returned) |
limit | int \| None | 否 | 限制要获取的音频数量,取值范围 1–100,默认 100(Limits the number of audios to be retrieved. Values between 1-100 are accepted. Defaults to 100) |
需要注意几个由源码确认的细节:
limit的合法区间为1–100,超出该范围的取值会被 Telegram 服务端拒绝,因此在使用时应先校验或直接依赖默认值 100。offset与limit共同构成分页机制:当用户音频总数超过 100 条时,可以借助offset顺序翻页,例如第一页offset=0, limit=100,第二页offset=100, limit=100。- 从代码结构看(三个字段均为普通 Pydantic 字段,没有额外校验器),参数的有效性校验交由 Telegram 服务端完成,客户端只负责序列化传输。
在Bot方法封装 bot.py 中,该方法还额外暴露了一个request_timeout: int | None = None参数,用于控制本次请求的 API 超时时间(单位为秒),其余三个参数原样透传:
async def get_user_profile_audios( self, user_id: int, offset: int | None = None, limit: int | None = None, request_timeout: int | None = None, ) -> UserProfileAudios: call = GetUserProfileAudios( user_id=user_id, offset=offset, limit=limit, ) return await self(call, request_timeout=request_timeout)返回值结构:UserProfileAudios 对象
该方法的返回类型由 get_user_profile_audios.py 中的__returning__ = UserProfileAudios指定,其结构定义在 user_profile_audios.py:
class UserProfileAudios(TelegramObject): total_count: int """Total number of profile audios for the target user""" audios: list[Audio] """Requested profile audios"""字段含义:
total_count:目标用户的个人资料音频总数(注意:这并不一定等于本次返回的audios列表长度,因为受offset/limit分页限制);audios:本次请求实际返回的 Audio 对象列表,其中每个元素都包含file_id、file_unique_id、duration等音频元数据字段(file_id可用于后续下载或复用该文件)。
在类型层面,bot.get_user_profile_audios(...)的返回类型被精确标注为UserProfileAudios,配合 Pydantic 模型,你在 IDE 中即可获得字段级补全与类型检查。
三种调用方式
官方文档 get_user_profile_audios.rst 为该方法列出了三种完全等价的调用途径,下面逐一展开并给出可直接运行的示例。
方式一:作为 Bot 方法调用(推荐)
最直接的方式是通过Bot实例的异步方法调用:
result: UserProfileAudios = await bot.get_user_profile_audios(...)bot是 Bot 实例(如Bot(token="..."))。完整示例:
from aiogram import Bot bot = Bot(token="YOUR_BOT_TOKEN") # 获取用户 42 的默认 100 条个人资料音频 result = await bot.get_user_profile_audios(user_id=42) # 分页获取:跳过前 100 条,取接下来的 100 条 page2 = await bot.get_user_profile_audios(user_id=42, offset=100, limit=100) # 只取前 5 条 first5 = await bot.get_user_profile_audios(user_id=42, limit=5) print(f"总音频数: {result.total_count}") for audio in result.audios: print(audio.file_id, audio.duration)方式二:作为方法对象(Method Object)调用
aiogram 的所有 Telegram API 方法都可以被建模为可调用的方法对象。文档给出的导入方式有两种:
- 完整导入:
from aiogram.methods.get_user_profile_audios import GetUserProfileAudios - 别名导入:
from aiogram.methods import GetUserProfileAudios
GetUserProfileAudios类继承自TelegramMethod[UserProfileAudios](见 get_user_profile_audios.py),并通过__api_method__ = "getUserProfileAudios"声明对应的 API 方法名。调用时需要绑定一个具体的 Bot 实例:
from aiogram.methods import GetUserProfileAudios # 通过 bot(method) 显式调用 result: UserProfileAudios = await bot(GetUserProfileAudios(user_id=42, limit=10))这种对象化设计带来两个额外能力:
- 先构造后执行:你可以把方法对象作为"参数"传给其他 Bot 实例,或在不同时机延迟执行;
- 挂载到 Bot 后直接 await:借助
as_(bot)将方法对象绑定到特定 Bot(底层实现见 context_controller.py 的as_方法),之后直接await即可:
from aiogram.methods import GetUserProfileAudios call = GetUserProfileAudios(user_id=42).as_(bot) # 绑定 bot result: UserProfileAudios = await call # 直接执行如果方法对象未绑定任何 Bot 就直接await,base.py 会抛出RuntimeError,提示你显式通过await bot(method)调用或先用method.as_(bot)挂载。
方式三:从已接收对象调用快捷方法
当你的代码中已经持有某个User对象(例如从Message.from_user、ChatMember等场景获得),可以直接调用User上的快捷方法get_profile_audios(文档中的:meth:aiogram.types.user.User.get_profile_audios``)。其实现位于 user.py:
def get_profile_audios( self, offset: int | None = None, limit: int | None = None, **kwargs: Any, ) -> GetUserProfileAudios: from aiogram.methods import GetUserProfileAudios return GetUserProfileAudios( user_id=self.id, # 自动填充 user_id offset=offset, limit=limit, **kwargs, ).as_(self._bot)注意两点:
user_id会被自动填充为当前User对象的self.id,无需手动传入;- 如果该
User对象是从带 Bot 上下文的更新中解析而来(如Message.from_user),self._bot会自动绑定;若为空,需要自行补上.as_(bot)后执行:
from aiogram import Bot, types bot = Bot(token="YOUR_BOT_TOKEN") # 从消息中拿到用户 @dp.message() async def show_profile_audios(message: types.Message): user = message.from_user result = await bot(user.get_profile_audios(limit=10)) # user_id 自动填充 # 或者:await user.get_profile_audios().as_(bot) await message.answer(f"你的个人资料音频共 {result.total_count} 条")底层执行链路:方法对象如何变成 HTTP 请求
理解三种调用方式后,可以透过源码看它们是如何殊途同归的。
Bot.get_user_profile_audios内部构造GetUserProfileAudios(...)方法对象,再执行await self(call, ...)(见 bot.py);Bot.__call__把方法对象交给session处理(bot.py);- 会话层
make_request依据方法对象的__api_method__ = "getUserProfileAudios"拼接请求 URL,并把 Pydantic 字段序列化为 JSON(详见 client/session/base.py 的prepare_value与make_request实现)。
因此,无论采用哪种调用姿势,最终都会落到"构造GetUserProfileAudios方法对象 →Bot执行 → 会话发送 HTTP 请求 → 反序列化为UserProfileAudios"这条统一链路上,这也是 aiogram 将所有 Telegram API 统一建模为方法对象的通用设计。
测试验证:用 MockedBot 验证调用行为
仓库为该方法提供了针对性的单元测试 tests/test_api/test_methods/test_get_user_profile_audios.py,可以直接作为你集成时的参考模板:
from aiogram.methods import GetUserProfileAudios from aiogram.types import Audio, UserProfileAudios from tests.mocked_bot import MockedBot class TestGetUserProfileAudios: async def test_bot_method(self, bot: MockedBot): prepare_result = bot.add_result_for( GetUserProfileAudios, ok=True, result=UserProfileAudios( total_count=1, audios=[Audio(file_id="file_id", file_unique_id="file_unique_id", duration=120)], ), ) response: UserProfileAudios = await bot.get_user_profile_audios(user_id=42) bot.get_request() assert response == prepare_result.result该测试展示了两个实用要点:
- 通过
MockedBot.add_result_for(...)预置返回结果,其中UserProfileAudios至少需要total_count与audios(每个Audio至少含file_id、file_unique_id、duration三个必填字段,见 audio.py); await bot.get_user_profile_audios(user_id=42)的返回对象与预设结果逐字段相等,验证了方法签名、序列化与反序列化的正确性。
常见问题与使用建议
- 一次能拿到多少条?默认
limit=100,这是服务端允许的上限;音频超过 100 条时应使用offset分页遍历,直到累计audios数量达到total_count。 total_count与audios长度不一致?这是正常现象,total_count是用户全部个人资料音频的总数,而audios只是当前分页窗口内的子集。- 拿到
Audio.file_id之后能做什么?file_id可用于复用该文件(如发送、转发),也可用bot.get_file(file_id)进一步获取文件下载路径;但file_unique_id不可用于下载或复用。 - 关于权限与限制:从 get_user_profile_audios.py 的源码看,该方法不要求额外权限参数,仅需目标用户的
user_id。请注意 Telegram 对个人资料音频相关能力有平台级限制,实际可用性以 Telegram 官方 Bot API 文档及服务端行为为准。 - 选择哪种调用方式?常规业务中优先使用
bot.get_user_profile_audios(...);需要延迟执行或跨 Bot 复用请求时使用方法对象;在消息处理函数内直接使用user.get_profile_audios()最简洁。
小结
getUserProfileAudios在 aiogram 中是一个参数精简、返回结构清晰的标准 Telegram 方法:user_id定位目标用户,offset/limit控制分页,返回包含total_count与audios列表的UserProfileAudios对象。三种调用方式(Bot 方法、方法对象、User 快捷方法)均可在实战中按需选用,底层全部收敛到GetUserProfileAudios方法对象与统一的 Bot 请求链路。结合仓库提供的单元测试,你可以快速在自己的机器人项目中集成并验证该功能。
- 后端
- 即时通讯
- API设计
【免费下载链接】aiogram
aiogram is a modern and fully asynchronous framework for Telegram Bot API written in Python using asyncio
相关推荐
aiogram 实战:使用 getUserPersonalChatMessages 获取用户个人聊天中的最新消息
aiogram 实战:使用 getUserPersonalChatMessages 获取用户个人聊天中的最新消息 本篇文章以 aiogram 官方 API 参考
后端即时通讯API设计CANN/driver:设置设备IP地址接口文档
dcmi\_set\_device\_ip<a name="ZH CN_TOPIC_0000002517615309" </a 函数原型<a name="zh
驱动开发人工智能CANNaiogram 中 getMyShortDescription 方法实战:获取机器人简介(Short Description)
aiogram 中 getMyShortDescription 方法实战:获取机器人简介(Short Description) 本篇技术指南以 aiogram
后端即时通讯API设计
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考