aiogram BusinessOpeningHours 类型详解:企业账号营业时间的数据结构与实战解析

发布时间:2026/10/13 9:45:42

aiogram BusinessOpeningHours 类型详解:企业账号营业时间的数据结构与实战解析 后端即时通讯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 官方 API 文档docs/api/types/business_opening_hours.rst及其配套类型 BusinessOpeningHoursInterval 为核心系统讲解 Telegram Bot API 中企业账号Business Account营业时间的数据模型两个字段的含义与取值边界、基于周一 0 点起的分钟序号的时间换算规则、该类型在getChat返回的ChatFullInfo中的实际获取方式并结合仓库源码给出可直接运行的解析与展示代码。读完本文你将能够从任意一次getChat响应中正确提取、换算并以人类可读格式输出企业账号的每周营业时间。一、类型定位营业时间在企业账号信息中的地位Telegram 企业账号Business Account可以配置多项公开资料包括简介BusinessIntro、地理位置BusinessLocation以及本文的主角——营业时间BusinessOpeningHours。在 aiogram 的 ChatFullInfo 类型中三者并列出现business_intro: BusinessIntro | None None *Optional*. For private chats with business accounts, the intro of the business business_location: BusinessLocation | None None *Optional*. For private chats with business accounts, the location of the business business_opening_hours: BusinessOpeningHours | None None *Optional*. For private chats with business accounts, the opening hours of the businessbusiness_opening_hours是可选字段只有当目标私聊属于配置了营业时间的企业账号时getChat才会返回该对象普通用户、群组或未配置营业时间的企业账号该字段为None。因此在使用时务必要做空值判断。二、BusinessOpeningHours 字段详解aiogram 的BusinessOpeningHours类定义在 aiogram/types/business_opening_hours.py 中继承自所有 Telegram 对象的公共基类TelegramObjectaiogram/types/base.py。它仅包含两个字段字段类型必填说明time_zone_namestr是定义营业时间所用时区的唯一名称IANA 时区标识符opening_hourslist[BusinessOpeningHoursInterval]是描述营业时间的时间区间列表源码中的完整定义与 docstringclass BusinessOpeningHours(TelegramObject): Describes the opening hours of a business. Source: https://core.telegram.org/bots/api#businessopeninghours time_zone_name: str Unique name of the time zone for which the opening hours are defined opening_hours: list[BusinessOpeningHoursInterval] List of time intervals describing business opening hours1. time_zone_name时区标识是换算的前提time_zone_name使用 IANA 时区数据库的标准名称例如Europe/Moscow、Asia/Shanghai、America/New_York。它表示区间中所有分钟序号都是相对该时区的本地时间计算的而不是 UTC。解读营业时间时必须结合该时区将分钟序号还原为当地时间否则会得出错误的星期与时刻。若业务上需要做跨时区展示如把莫斯科时间转换为用户本地时间应使用zoneinfo或pytz配合该字段做转换。2. opening_hours区间列表的语义opening_hours是一个BusinessOpeningHoursInterval对象的列表描述一周内企业营业的全部时间段。多个区间之间可能存在间隙对应休息时段也可能存在跨周区间。列表中的每个元素都遵循统一的分钟序号编码规则见下节。三、BusinessOpeningHoursInterval以分钟序号表达周内时刻BusinessOpeningHoursInterval定义在 aiogram/types/business_opening_hours_interval.py 中同样继承TelegramObject只包含两个整数字段class BusinessOpeningHoursInterval(TelegramObject): Describes an interval of time during which a business is open. Source: https://core.telegram.org/bots/api#businessopeninghoursinterval opening_minute: int The minutes sequence number in a week, starting on Monday, marking the start of the time interval during which the business is open; 0 - 7 * 24 * 60 closing_minute: int The minutes sequence number in a week, starting on Monday, marking the end of the time interval during which the business is open; 0 - 8 * 24 * 601. 编码规则周一 0 点起算的分钟序号这是整个数据模型最核心的约定一周以周一为起点从周一 00:00 开始按分钟累计编号。一周共有7 × 24 × 60 10080分钟opening_minute合法范围为0到7 * 24 * 60即0–10080标记营业区间的起始时刻closing_minute合法范围为0到8 * 24 * 60即0–11520标记营业区间的结束时刻允许超出 10080用于表达营业时段跨越到下周周一的凌晨。分钟序号与星期 时刻的换算公式如下minute_of_week weekday_index(Monday0, ..., Sunday6) * 1440 hour * 60 minute几个典型换算示例实际时刻time_zone_name 指定的时区内weekday 索引分钟序号周一 09:0000 × 1440 9 × 60 540周一 17:0000 × 1440 17 × 60 1020周五 18:0044 × 1440 18 × 60 6840周日 23:5966 × 1440 23 × 60 59 10019下周一 00:00跨周关门点—100802. 一个完整区间示例例如周一至周五 09:00–18:00 营业Telegram API 会将其表示为如下 JSON{ time_zone_name: Europe/Moscow, opening_hours: [ { opening_minute: 540, closing_minute: 1020 }, { opening_minute: 1980, closing_minute: 2460 }, { opening_minute: 3420, closing_minute: 3900 }, { opening_minute: 4860, closing_minute: 5340 }, { opening_minute: 6300, closing_minute: 6780 } ] }其中6300 4 × 1440 540周五 09:006780 4 × 1440 1020周五 18:00其余区间依次对应周二、周三、周四。四、数据从哪来getChat 与 ChatFullInfo 的调用链营业时间只能通过getChat方法获得。aiogram 中对应的方法类是 GetChat__api_method__ getChat其返回值类型为ChatFullInfoclass GetChat(TelegramMethod[ChatFullInfo]): Use this method to get up-to-date information about the chat. Returns a :class:aiogram.types.chat_full_info.ChatFullInfo object on success. __returning__ ChatFullInfo __api_method__ getChat chat_id: ChatIdUnion Unique identifier for the target chat or username of the target supergroup or channel in the format :code:username在异步调用链上bot.get_chat(chat_id...)会向 Telegram Bot API 发出getChat请求返回的 JSON 经 pydantic 校验后构建为ChatFullInfo实例其business_opening_hours字段即被解析为BusinessOpeningHours对象。仓库测试 tests/test_api/test_methods/test_get_chat.py 验证了这一调用约定class TestGetChat: async def test_bot_method(self, bot: MockedBot): prepare_result bot.add_result_for( GetChat, okTrue, resultChatFullInfo( id-42, typechannel, titlechat, accent_color_id0, max_reaction_count0, accepted_gift_typesAcceptedGiftTypes(...), ), ) response: ChatFullInfo await bot.get_chat(chat_id-42) bot.get_request() assert response prepare_result.result需要特别说明的是版本差异旧版 aiogram 中Chat类型aiogram/types/chat.py#L122-L128也曾包含business_opening_hours字段但自 Telegram Bot API 7.3 起已被标记为deprecated注释中注明Returned only in getChat并指向 API 变更日志。当前版本应统一从ChatFullInfogetChat的返回类型中读取该字段。五、实战从 getChat 响应中解析并展示营业时间以下代码展示了完整的端到端用法调用getChat、判空、将分钟序号还原为星期几 时刻格式。基于仓库中 BusinessOpeningHours 与 BusinessOpeningHoursInterval 的类型定义可以直接访问hours.time_zone_name与interval.opening_minute/interval.closing_minuteimport asyncio from datetime import datetime, timedelta from aiogram import Bot, Dispatcher WEEKDAYS [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday] def fmt_minute(minute: int) - str: Convert week-minute sequence number (Monday 00:00 based) to Weekday HH:MM. day_idx, rem divmod(minute, 1440) hour, m divmod(rem, 60) return f{WEEKDAYS[day_idx % 7]} {hour:02d}:{m:02d} async def show_opening_hours(bot: Bot, chat_id: int) - None: chat: ChatFullInfo await bot.get_chat(chat_idchat_id) hours chat.business_opening_hours if hours is None: print(fChat {chat_id} has no business opening hours configured.) return print(fTime zone: {hours.time_zone_name}) for interval in hours.opening_hours: print(f {fmt_minute(interval.opening_minute)} - {fmt_minute(interval.closing_minute)}) async def main() - None: bot Bot(tokenBOT_TOKEN) dp Dispatcher() await show_opening_hours(bot, chat_id123456789) # a business account chat id await bot.session.close() if __name__ __main__: asyncio.run(main())关于跨周区间的处理由于closing_minute允许取到11520即下一周周一的 00:00 之前对于类似周日至周一凌晨 02:00 关门的排班closing_minute会大于10080。上面的fmt_minute通过day_idx % 7取模可将超出的序号正确折叠回一周内的星期名称避免出现第 8 天这类非法输出。在实际业务中还应结合time_zone_name做时区确认确保换算的是企业所在时区的本地时间。直接构造对象的场景除了从getChat响应中解析开发者在编写测试或构建演示数据时也可以直接用关键字参数构造BusinessOpeningHours其__init__签名由 butcher 自动生成仅用于类型检查与 IDE 提示如下BusinessOpeningHours( time_zone_nameEurope/Moscow, opening_hours[ BusinessOpeningHoursInterval(opening_minute540, closing_minute1020), BusinessOpeningHoursInterval(opening_minute6300, closing_minute6780), ], )由于 TelegramObject 基于 pydantic 并配置了extraallow、validate_assignmentTrue与frozenTrue字段赋值会经过校验且实例不可变反序列化时未知的额外字段会被宽容保留这保证了与 Telegram API 新字段的前向兼容。六、模块导出与文档索引BusinessOpeningHours与BusinessOpeningHoursInterval已从 aiogram/types/init.py 中公开导出因此可以直接使用from aiogram.types import BusinessOpeningHours, BusinessOpeningHoursInterval两份 API 文档页面均以automodule指令渲染源码 docstringbusiness_opening_hours.rst、business_opening_hours_interval.rst并已登记在 docs/api/types/index.rst 的类型索引中读者可在官方文档站点的 Types 目录下直接查阅这两类的最新字段说明。七、要点小结BusinessOpeningHours由time_zone_nameIANA 时区标识与opening_hours区间列表两个必填字段构成仅出现在企业账号的私聊信息中区间时刻一律使用周一 00:00 起算的分钟序号编码opening_minute范围0–10080closing_minute范围0–11520换算公式为weekday_index × 1440 hour × 60 minute该对象只能通过getChat返回ChatFullInfo获取旧版Chat中的同名字段已废弃读取前务必判空解读时务必结合time_zone_name对应的时区并注意跨周区间的分钟序号会超过10080。相关源码与测试BusinessOpeningHours 源码、BusinessOpeningHoursInterval 源码、ChatFullInfo 字段定义、GetChat 方法、get_chat 测试用例。赞分享后端即时通讯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 实战指南使用 setBusinessAccountProfilePhoto 管理企业账号头像aiogram 实战指南使用 setBusinessAccountProfilePhoto 管理企业账号头像 导读 本文围绕 aiogram 中 setBus后端即时通讯API设计aiogram 中 setBusinessAccountUsername 方法详解修改托管企业账号用户名的完整实战指南aiogram 中 setBusinessAccountUsername 方法详解修改托管企业账号用户名的完整实战指南 导读 setBusinessAccou后端即时通讯API设计aiogram deleteStory 方法详解删除企业账号 Story 的三种调用方式与底层实现aiogram deleteStory 方法详解删除企业账号 Story 的三种调用方式与底层实现 deleteStory 是 Telegram Bot AP后端即时通讯API设计创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
延伸阅读

更多相关文章

2026/10/13 9:40:42

Spring Boot配置与YAML日志系统实战指南

1. Spring Boot配置体系解析Spring Boot的配置体系是其"约定优于配置"核心理念的重要体现。相比传统Spring框架需要大量XML配置,Spring Boot通过自动配置机制大幅简化了配置工作。在实际项目中,我们主要使用properties和YAML两种配置文件格式&…

2026/10/13 9:40:42

K8s集群Keepalived VIP+NodePort访问不通:根因与解决

如果有一天你发现,K8s集群里用Keepalived漂移出来的VIPNodePort死活访问不通,而直接用节点IP加NodePort却一切正常,别急着怀疑防火墙。我最近就在一套3节点的集群上踩中了这个问题,排查链路很长,根因却特别简单&#x…

2026/10/13 11:01:05

C++数组指针字符串:从内存视角彻底搞懂核心难点

我知道很多人第一次翻开郑莉这本《C语言程序设计》,到第六章"数组、指针和字符串"就开始心里打鼓。说实话,我当年也是这样:前面的流程控制、函数调用,照着抄总能写出来,可数组和指针一上来,什么&…

2026/10/13 11:01:05

智能PDU:从车间排插到智慧工厂电力神经中枢

1. 先聊聊为什么车间里要装“智能排插”前阵子去一个做汽车零部件的工厂看产线改造,走到车间角落的配电柜旁边,正好看到一位老师傅拿着红外测温枪对着一个满负载的插线板在测温度。那个插线板上插着三台设备的电源,线缆摸上去已经有些发烫了&…

2026/10/13 11:01:05

OpenClaw跨平台安装指南:让《Captain Claw》在现代系统重生

前几天整理旧硬盘,翻出一个十年前的游戏存档文件夹,里面还躺着那只猫船长的记录。我试着双击当年的老游戏程序,Win11下直接黑屏闪退,连个报错都不给。正想放弃,朋友提了一句:“有没有试过OpenClaw&#xff…

2026/10/13 11:01:05

2026计算机职业规划:产品思维与新兴技术赛道

## 1. 计算机专业职业规划的现状与痛点2026年的计算机行业与十年前相比已经发生翻天覆地的变化。当我在某985高校计算机系担任企业导师时,发现超过70%的应届生仍在使用2015年的职业规划模板——这就像用Windows XP的操作手册来指导Windows 12的安装。最典型的认知偏…

2026/10/13 11:01:05

Java+SSM+Flask全栈实现在线奶茶店系统

1. 项目概述:在线奶茶店系统的全栈实现这个基于JavaSSMFlask的网上奶茶店系统,是我为连锁奶茶品牌开发的一套完整的线上销售解决方案。系统采用前后端分离架构,后端使用JavaSSM框架处理核心业务逻辑,Flask则负责轻量级的API服务和…

2026/10/13 10:56:04

三天搞定论文AI率:排查、深度改写与复测全攻略

如果你刷到了这篇文章,大概率正经历同一件事:导师把论文回传过来,批注栏里只有一句话——“AI率太高,自己回去改”,外加一张检测报告截图。2026年了,这已经不是什么新鲜事。随着生成式工具在学术写作里的使…

2026/10/13 5:19:27

Python调用Gemini Structured Outputs实现工单路由门禁

客服工单最怕的不是模型“答错一句话”,而是它给出一段看起来合理的说明,程序却从中猜错优先级。通俗做法是:要求模型只交 JSON(JavaScript Object Notation,轻量数据格式),再让代码验证它。Gem…

2026/10/12 12:01:26

Spring Boot超市进销存系统毕设实战:从需求拆解到答辩通关

最近带的一个学生项目组里,有A同学跑来问我:选什么毕设题目最稳妥,既能让评审老师觉得工作量够,又不会在答辩时被问到语无伦次。我第一反应就是推荐基于Spring Boot的超市仓库管理系统——也就是超市进销存系统。这个题目乍一看平…

2026/10/12 12:54:07

Flutter StatefulWidget 生命周期核心解析

很多刚开始接触 Flutter 的朋友,在看完一堆“Hello World”和基础组件之后,大概率都会撞上同一堵墙:StatefulWidget 里那堆 initState、build、dispose 方法,到底什么时候被调用?为什么顺序是那样?在里面到…

2026/10/13 0:04:41

基于Triton部署YOLO目标检测:从训练脚本到线上服务

简介:这份资源面向希望将目标检测模型落地到生产环境的深度学习开发者与算法工程师,聚焦于用 Triton Inference Server 部署 YOLOv9 的完整实战方案,解决从训练权重到线上推理服务之间的工程化难题。压缩包共 16 个文件,约 886KB&…

2026/10/13 0:04:41

手把手教你云端部署Qwen-Image-2.1:从GPU选型到在线文生图服务

Qwen-Image-2.1 这个开源的图像生成模型,最近在内容创作和 AI 应用圈子里热度相当高。它属于通义 Qwen 系列里的视觉生成分支,让我眼前一亮的是它对中文长文本的理解能力——以前用那些老方案生成带中文文字的图,十个里九个是“乱码印刷体”&…

还想了解更多?直接咨询顾问

免费诊断 + 免费方案 + 透明报价。

全国咨询热线400-8866-253
免费获取方案
☎咨询二维码 ☎ ↑