messages
feishu.im.messages
¶
IMNamespace
¶
Bases: Namespace
即时消息(IM)接口命名空间。
封装飞书消息相关的服务端接口,包括发送、回复、编辑、撤回、转发、查询消息以及读取已读用户列表等能力。
通过 IMNamespace.chats 可进一步访问群组相关接口。
通常无需直接实例化,应通过客户端的 client.im 访问。
飞书文档
源代码位于: feishu/im/messages.py
| Python | |
|---|---|
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 | |
chats
property
¶
chats: ChatsNamespace
群组接口命名空间。
惰性创建并返回 feishu.im.chats.ChatsNamespace,用于创建、查询、更新、解散群组以及管理群成员。
返回:
| 类型 | 描述 |
|---|---|
ChatsNamespace
|
群组接口命名空间实例。 |
飞书文档
示例:
pins
property
¶
pins: PinsNamespace
消息 Pin 接口命名空间。
惰性创建并返回 feishu.im.pins.PinsNamespace,用于将消息 Pin 到会话、取消 Pin、列举 Pin 消息。
返回:
| 类型 | 描述 |
|---|---|
PinsNamespace
|
消息 Pin 接口命名空间实例。 |
飞书文档
示例:
reactions
property
¶
reactions: ReactionsNamespace
消息表情回复接口命名空间。
惰性创建并返回 feishu.im.reactions.ReactionsNamespace,用于添加、删除与列举消息表情回复。
返回:
| 类型 | 描述 |
|---|---|
ReactionsNamespace
|
消息表情回复接口命名空间实例。 |
飞书文档
示例:
forward
async
¶
forward(receive_id: str, message_id: str, *, receive_id_type: str | None = None, uuid: str | None = None) -> NestedDict
转发消息。
将一条已有消息转发给指定接收者。接收者在前,与 feishu.im.messages.IMNamespace.send / feishu.im.messages.IMNamespace.merge_forward 一致。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
str
|
接收者 ID,可为 chat ID、open ID、union ID、邮箱或 user ID。 |
必需 |
|
str
|
要转发的消息 ID。 |
必需 |
|
str | None
|
接收者 ID 类型。为空时根据 |
None
|
|
str | None
|
消息唯一标识,用于消息去重;为空时自动生成。 |
None
|
返回:
| 类型 | 描述 |
|---|---|
NestedDict
|
转发后生成的消息数据。 |
引发:
| 类型 | 描述 |
|---|---|
ValueError
|
未显式传入 |
FeishuError
|
请求失败或返回错误码时抛出。 |
飞书文档
示例:
| Python Console Session | |
|---|---|
源代码位于: feishu/im/messages.py
get
async
¶
get(message_id: str) -> NestedDict
获取指定消息的内容。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
str
|
消息 ID。 |
必需 |
返回:
| 类型 | 描述 |
|---|---|
NestedDict
|
消息数据,包含 |
引发:
| 类型 | 描述 |
|---|---|
FeishuError
|
请求失败或返回错误码时抛出。 |
飞书文档
示例:
| Python Console Session | |
|---|---|
源代码位于: feishu/im/messages.py
get_resource
async
¶
get_resource(message_id: str, file_key: str, *, resource_type: str = 'image') -> bytes
获取消息中的资源文件(图片或附件)。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
str
|
消息 ID(以 om_ 开头)。 |
必需 |
|
str
|
资源文件 Key,可从消息体中的 image_key / file_key 字段获取。 |
必需 |
|
str
|
资源类型,”image”(默认)或 “file”。 发送给飞书接口的 type 查询参数。 |
'image'
|
返回:
| 类型 | 描述 |
|---|---|
bytes
|
资源文件的原始字节内容。 |
引发:
| 类型 | 描述 |
|---|---|
FeishuError
|
请求失败或返回错误码时抛出。 |
飞书文档
示例:
| Python Console Session | |
|---|---|
源代码位于: feishu/im/messages.py
list
async
¶
list(container_id: str, *, container_id_type: str = 'chat', sort_type: str = 'ByCreateTimeDesc', start_time: str | None = None, end_time: str | None = None, page_size: int = 50, max_items: int | None = None) -> _NestedDictList
获取会话历史消息。
自动翻页并将各页结果拼接为单个列表返回。page_size 会被限制在飞书接口上限(50)以内。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
str
|
容器 ID,例如群 chat ID。 |
必需 |
|
str
|
容器类型,默认为 |
'chat'
|
|
str
|
排序方式,默认为 |
'ByCreateTimeDesc'
|
|
str | None
|
起始时间,秒级时间戳字符串;为空表示不限制。 |
None
|
|
str | None
|
结束时间,秒级时间戳字符串;为空表示不限制。 |
None
|
|
int
|
每页条数,默认为 50,超过上限时按上限截断。 |
50
|
|
int | None
|
最多返回的条数;为空表示返回全部。 |
None
|
返回:
| 类型 | 描述 |
|---|---|
_NestedDictList
|
消息数据列表。 |
引发:
| 类型 | 描述 |
|---|---|
FeishuError
|
请求失败或返回错误码时抛出。 |
飞书文档
示例:
| Python Console Session | |
|---|---|
源代码位于: feishu/im/messages.py
list_reply_chain
async
¶
list_reply_chain(message_id: str, *, max_items: int | None = None, max_chars: int | None = None, oldest_first: bool = True) -> _NestedDictList
获取一条消息的回复链(自身及其全部父级消息)。
从 message_id 指向的消息出发,沿 parent_id 逐级向上抓取父消息,直至以下任一条件满足:
不再存在 parent_id、已收集的消息数量达到 max_items、或累计的消息体长度达到 max_chars。
抓取过程中若某条父消息缺失(飞书接口返回业务错误),视为回复链终点并优雅停止。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
str
|
链尾消息 ID(以 |
必需 |
|
int | None
|
最多返回的消息条数;为空表示不限制。 |
None
|
|
int | None
|
累计消息体长度上限,达到后停止向上回溯;为空表示不限制。 |
None
|
|
bool
|
为 |
True
|
返回:
| 类型 | 描述 |
|---|---|
_NestedDictList
|
回复链上的消息数据列表。 |
引发:
| 类型 | 描述 |
|---|---|
FeishuError
|
请求失败或返回错误码时抛出。 |
飞书文档
示例:
| Python Console Session | |
|---|---|
源代码位于: feishu/im/messages.py
merge_forward
async
¶
merge_forward(receive_id: str, message_id_list: _StringList, *, receive_id_type: str | None = None, uuid: str | None = None) -> NestedDict
合并转发消息。
将多条已有消息合并为一条转发给指定接收者。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
str
|
接收者 ID,可为 chat ID、open ID、union ID、邮箱或 user ID。 |
必需 |
|
_StringList
|
要合并转发的消息 ID 列表。 |
必需 |
|
str | None
|
接收者 ID 类型。为空时根据 |
None
|
|
str | None
|
消息唯一标识,用于消息去重;为空时不发送该字段。 |
None
|
返回:
| 类型 | 描述 |
|---|---|
NestedDict
|
合并转发后生成的消息数据。 |
引发:
| 类型 | 描述 |
|---|---|
ValueError
|
未显式传入 |
FeishuError
|
请求失败或返回错误码时抛出。 |
飞书文档
示例:
| Python Console Session | |
|---|---|
源代码位于: feishu/im/messages.py
patch
async
¶
patch(message_id: str, card: dict[str, Any] | str) -> NestedDict
更新应用发送的消息卡片。
仅更新卡片内容,请求体不含 msg_type,适用于交互式卡片的局部刷新。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
str
|
要更新的卡片消息 ID。 |
必需 |
|
dict[str, Any] | str
|
新的卡片内容,可为卡片字典或已序列化的 JSON 字符串。 |
必需 |
返回:
| 类型 | 描述 |
|---|---|
NestedDict
|
更新后的消息数据。 |
引发:
| 类型 | 描述 |
|---|---|
FeishuError
|
请求失败或返回错误码时抛出。 |
飞书文档
示例:
| Python Console Session | |
|---|---|
源代码位于: feishu/im/messages.py
push_follow_up
async
¶
push_follow_up(message_id: str, follow_ups: str | dict[str, Any]) -> NestedDict
为消息添加跟随气泡。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
str
|
消息 ID(以 om_ 开头)。 |
必需 |
|
str | dict[str, Any]
|
跟随气泡内容,可为字符串或字典。 - 传入字符串时,自动封装为 {“follow_ups”: [{“content”: follow_ups}]}。 - 传入字典时直接作为请求体发送(调用方需确保包含 “follow_ups” 键)。 |
必需 |
返回:
| 类型 | 描述 |
|---|---|
NestedDict
|
接口返回的数据(通常为空字典)。 |
引发:
| 类型 | 描述 |
|---|---|
FeishuError
|
请求失败或返回错误码时抛出。 |
飞书文档
示例:
| Python Console Session | |
|---|---|
源代码位于: feishu/im/messages.py
read_users
async
¶
read_users(message_id: str, *, user_id_type: str = 'open_id', max_items: int | None = None) -> _NestedDictList
查询消息已读用户列表。
自动翻页并将各页结果拼接为单个列表返回。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
str
|
消息 ID。 |
必需 |
|
str
|
返回的用户 ID 类型,默认为 |
'open_id'
|
|
int | None
|
最多返回的条数;为空表示返回全部。 |
None
|
返回:
| 类型 | 描述 |
|---|---|
_NestedDictList
|
已读用户数据列表。 |
引发:
| 类型 | 描述 |
|---|---|
FeishuError
|
请求失败或返回错误码时抛出。 |
飞书文档
示例:
| Python Console Session | |
|---|---|
源代码位于: feishu/im/messages.py
recall
async
¶
recall(message_id: str) -> NestedDict
撤回消息。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
str
|
要撤回的消息 ID。 |
必需 |
返回:
| 类型 | 描述 |
|---|---|
NestedDict
|
接口返回的数据(通常为空)。 |
引发:
| 类型 | 描述 |
|---|---|
FeishuError
|
请求失败或返回错误码时抛出。 |
飞书文档
示例:
源代码位于: feishu/im/messages.py
reply
async
¶
reply(message_id: str, content: dict[str, Any] | str, *, msg_type: str = 'text', reply_in_thread: bool | None = None, uuid: str | None = None) -> NestedDict
回复消息。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
str
|
要回复的消息 ID(以 |
必需 |
|
dict[str, Any] | str
|
消息内容,可为字典或已序列化的 JSON 字符串。 |
必需 |
|
str
|
消息类型,默认为 |
'text'
|
|
bool | None
|
是否以话题形式回复。为 |
None
|
|
str | None
|
消息唯一标识,用于消息去重;为空时自动生成。 |
None
|
返回:
| 类型 | 描述 |
|---|---|
NestedDict
|
包含 |
引发:
| 类型 | 描述 |
|---|---|
FeishuError
|
请求失败或返回错误码时抛出。 |
飞书文档
示例:
| Python Console Session | |
|---|---|
源代码位于: feishu/im/messages.py
send
async
¶
send(receive_id: str, content: dict[str, Any] | str, *, msg_type: str | None = None, receive_id_type: str | None = None, uuid: str | None = None) -> NestedDict
发送消息。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
str
|
接收者 ID,可为 chat ID、open ID、union ID、邮箱或 user ID。 |
必需 |
|
dict[str, Any] | str
|
消息内容。可为纯文本字符串(自动按 |
必需 |
|
str | None
|
消息类型。为空时根据 |
None
|
|
str | None
|
接收者 ID 类型。为空时通过
feishu.im.messages.infer_receive_id_type 根据 |
None
|
|
str | None
|
消息唯一标识,用于消息去重;为空时自动生成。 |
None
|
返回:
| 类型 | 描述 |
|---|---|
NestedDict
|
包含 |
引发:
| 类型 | 描述 |
|---|---|
ValueError
|
未显式传入 |
FeishuError
|
请求失败或返回错误码时抛出。 |
飞书文档
示例:
| Python Console Session | |
|---|---|
源代码位于: feishu/im/messages.py
update
async
¶
update(message_id: str, content: dict[str, Any] | str, *, msg_type: str = 'text') -> NestedDict
编辑消息。
用新内容整体替换已发送消息的内容,适用于文本(text)与富文本(post)消息。
如需局部更新卡片,请使用 feishu.im.messages.IMNamespace.patch。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
str
|
要编辑的消息 ID。 |
必需 |
|
dict[str, Any] | str
|
新的消息内容,可为字典或已序列化的 JSON 字符串。 |
必需 |
|
str
|
消息类型,默认为 |
'text'
|
返回:
| 类型 | 描述 |
|---|---|
NestedDict
|
更新后的消息数据。 |
引发:
| 类型 | 描述 |
|---|---|
FeishuError
|
请求失败或返回错误码时抛出。 |
飞书文档
示例:
| Python Console Session | |
|---|---|
源代码位于: feishu/im/messages.py
upload_file
async
¶
上传文件。
以 multipart/form-data 方式上传文件,返回的 file_key 可用于
feishu.im.messages.IMNamespace.send 以 file 类型发送文件消息(msg_type 会自动推断)。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
bytes
|
文件的原始字节内容。 |
必需 |
|
str
|
文件名称(含扩展名)。 |
必需 |
|
str
|
文件类型,默认为 |
'stream'
|
返回:
| 类型 | 描述 |
|---|---|
NestedDict
|
上传结果数据,含 |
引发:
| 类型 | 描述 |
|---|---|
FeishuError
|
请求失败或返回错误码时抛出。 |
飞书文档
示例:
| Python Console Session | |
|---|---|
源代码位于: feishu/im/messages.py
upload_image
async
¶
upload_image(image: bytes, *, image_type: str = 'message') -> NestedDict
上传图片。
以 multipart/form-data 方式上传图片,返回的 image_key 可用于
feishu.im.messages.IMNamespace.send 以 image 类型发送图片消息(msg_type 会自动推断)或填充消息卡片。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
bytes
|
图片的原始字节内容。 |
必需 |
|
str
|
图片用途,默认为 |
'message'
|
返回:
| 类型 | 描述 |
|---|---|
NestedDict
|
上传结果数据,含 |
引发:
| 类型 | 描述 |
|---|---|
FeishuError
|
请求失败或返回错误码时抛出。 |
飞书文档
示例:
源代码位于: feishu/im/messages.py
infer_receive_id_type
¶
infer_receive_id_type(receive_id: str) -> str
根据接收者 ID 的形态推断其 ID 类型。
根据 ID 的前缀或形态推断 receive_id_type,便于在调用消息接口时省略该参数:
| 前缀 / 形态 | 推断结果 |
|---|---|
oc_ 开头 |
chat_id |
ou_ 开头 |
open_id |
on_ 开头 |
union_id |
| 合法邮箱地址 | email |
仅依据可靠的前缀与邮箱形态进行推断。user_id 没有固定前缀或长度特征,无法可靠识别,
因此当 receive_id 不匹配上述任一规则时直接抛出 ValueError,请显式传入 receive_id_type。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
str
|
接收者 ID,可以是 chat ID、open ID、union ID 或邮箱。 |
必需 |
返回:
| 类型 | 描述 |
|---|---|
str
|
推断出的 ID 类型字符串。 |
引发:
| 类型 | 描述 |
|---|---|
ValueError
|
无法从 |
飞书文档
示例:
源代码位于: feishu/im/messages.py
infer_msg_type
¶
根据消息内容的形态推断 msg_type。
仅对高置信度的内容形态做推断,无法判定时回退为 text;调用方始终可显式传入
msg_type 覆盖推断结果。
参数:
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
|
dict[str, Any] | str
|
消息内容,字典或已序列化的 JSON 字符串。 |
必需 |
返回:
| 类型 | 描述 |
|---|---|
str
|
推断出的 |
示例: