服务端 API/消息/消息管理/获取会话历史消息
# 获取会话历史消息

获取会话（包括单聊、群组）的历史消息（聊天记录）。

**注意事项**：接口级别权限默认只能获取单聊（p2p）消息，如果需要获取群组（group）消息，应用还必须拥有 **==获取群组中所有消息==** 权限
**注意事项**：- 需要开启[机器人能力](https://open.larksuite.com/document/uAjLw4CM/ugTN1YjL4UTN24CO1UjN/trouble-shooting/how-to-enable-bot-ability)  
- 获取消息时，机器人必须在群组中
- 对于**普通对话群**中的话题消息，通过 "chat" 容器类型仅能获取到话题的根消息，可通过指定容器类型为 "thread" 获取话题回复中的所有消息

## 请求

基本 | &nbsp;
---|---
HTTP URL | https://open.larksuite.com/open-apis/im/v1/messages
HTTP Method | GET
接口频率限制 | [1000 次/分钟、50 次/秒](https://open.larksuite.com/document/ukTMukTMukTM/uUzN04SN3QjL1cDN)
支持的应用类型 | Custom App、Store App
权限要求<br>**调用该 API 所需的权限。开启其中任意一项权限即可调用**<br>开启任一权限即可 | 获取与发送单聊、群组消息(im:message)<br>获取单聊、群组消息(im:message:readonly)<br>获取单聊、群组的历史消息(im:message.history:readonly)

### 请求头

名称 | 类型 | 必填 | 描述
---|---|---|---
Authorization | string | 是 | `tenant_access_token`<br>**值格式**："Bearer `access_token`"<br>**示例值**："Bearer t-7f1bcd13fc57d46bac21793a18e560"<br>[了解更多：如何选择与获取 access token](https://open.larksuite.com/document/uAjLw4CM/ugTN1YjL4UTN24CO1UjN/trouble-shooting/how-to-choose-which-type-of-token-to-use)

### 查询参数

名称 | 类型 | 必填 | 描述
---|---|---|---
container_id_type | string | 是 | 容器类型 <br>**可选值有**：<br>- `chat`：包含单聊（p2p）和群聊（group）<br>- `thread`: 话题<br>**示例值**：chat
container_id | string | 是 | 容器的ID，可填写：<br>- chat_id，群聊或单聊的 ID，参见[群ID 说明](https://open.larksuite.com/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/chat-id-description) <br>- thread_id，话题 ID，参见[话题介绍](https://open.larksuite.com/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/thread-introduction)<br>**示例值**：oc_234jsi43d3ssi993d43545f
start_time | string | 否 | 历史信息的起始时间（秒级时间戳）<br>**注意：**<br>- thread 容器类型暂不支持获取指定时间范围内的消息<br>**示例值**：1608594809
end_time | string | 否 | 历史信息的结束时间（秒级时间戳）<br>**注意：**<br>- thread 容器类型暂不支持获取指定时间范围内的消息<br>**示例值**：1609296809
sort_type | string | 否 | 消息排序方式<br>**示例值**：ByCreateTimeAsc<br>**可选值有**：<br>- ByCreateTimeAsc：按消息创建时间升序排列<br>- ByCreateTimeDesc：按消息创建时间降序排列<br>**默认值**：`ByCreateTimeAsc`
page_size | int | 否 | 分页大小<br>**示例值**：20<br>**默认值**：`20`<br>**数据校验规则**：<br>- 取值范围：`1` ～ `50`
page_token | string | 否 | 分页标记，第一次请求不填，表示从头开始遍历；分页查询结果还有更多项时会同时返回新的 page_token，下次遍历可采用该 page_token 获取查询结果<br>**示例值**：GxmvlNRvP0NdQZpa7yIqf_Lv_QuBwTQ8tXkX7w-irAghVD_TvuYd1aoJ1LQph86O-XImC4X9j9FhUPhXQDvtrQ==

使用page_token请求时，排序方式（sort_type）与第一次请求一致，不支持中途改换排序方式

## 响应

### 响应体

名称 | 类型 | 描述
---|---|---
code | int | 错误码，非 0 表示失败
msg | string | 错误描述
data | \- | \-
has_more | boolean | 是否还有更多项
page_token | string | 分页标记，当 has_more 为 true 时，会同时返回新的 page_token，否则不返回 page_token
items | message\[\] | message[]
message_id | string | 消息id，说明参见：[消息ID说明](https://open.larksuite.com/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/intro#ac79c1c2)
root_id | string | 根消息id，用于回复消息场景，说明参见：[消息ID说明](https://open.larksuite.com/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/intro#ac79c1c2)
parent_id | string | 父消息的id，用于回复消息场景，说明参见：[消息ID说明](https://open.larksuite.com/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/intro#ac79c1c2)
thread_id | string | 消息所属的话题 ID
msg_type | string | 消息类型 包括：text、post、image、file、audio、media、sticker、interactive、share_chat、share_user等，类型定义请参考[接收消息内容](https://open.larksuite.com/document/uAjLw4CM/ukTMukTMukTM/im-v1/message/events/message_content)
create_time | string | 消息生成的时间戳（毫秒）
update_time | string | 消息更新的时间戳（毫秒）
deleted | boolean | 消息是否被撤回或删除
updated | boolean | 消息是否被更新
chat_id | string | 所属的群
sender | sender | 发送者，可以是用户或应用
id | string | 该字段标识发送者的id
id_type | string | 该字段标识发送者的id类型<br>**可选值有：**<br>- `open_id`<br>- `app_id`
sender_type | string | 该字段标识发送者的类型<br>**可选值有：**<br>- `user`: 用户<br>- `app`: 应用<br>- `anonymous`: 匿名<br>- `unknown`: 未知
tenant_key | string | 为租户在Lark上的唯一标识，用来换取对应的tenant_access_token，也可以用作租户在应用里面的唯一标识
body | message_body | 消息内容
content | string | 消息内容，json结构序列化后的字符串。不同msg_type对应不同内容。消息类型 包括：text、post、image、file、audio、media、sticker、interactive、share_chat、share_user等，类型定义请参考：[接收消息内容](https://open.larksuite.com/document/uAjLw4CM/ukTMukTMukTM/im-v1/message/events/message_content)<br>**注意**：<br>- 卡片消息内容与在卡片搭建工具中获取的卡片 JSON 不一致，暂不支持返回原始卡片 JSON
mentions | mention\[\] | 被@的用户或机器人的id列表
key | string | 被@的用户或机器人的序号。例如，第3个被@到的成员，值为“@_user_3”
id | string | 被@的用户或者机器人的open_id
id_type | string | 被@的用户或机器人 id 类型，目前仅支持 `open_id` ([什么是 Open ID？](https://open.larksuite.com/document/home/user-identity-introduction/open-id))
name | string | 被@的用户或机器人的姓名
tenant_key | string | 为租户在Lark上的唯一标识，用来换取对应的tenant_access_token，也可以用作租户在应用里面的唯一标识
upper_message_id | string | 合并转发消息中，上一层级的消息id message_id，说明参见：[消息ID说明](https://open.larksuite.com/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/intro#ac79c1c2)

### 响应体示例
```json
{
    "code": 0,
    "msg": "success",
    "data": {
        "has_more": false,
        "page_token": "GxmvlNRvP0NdQZpa7yIqf_Lv_QuBwTQ8tXkX7w-irAghVD_TvuYd1aoJ1LQph86O-XImC4X9j9FhUPhXQDvtrQ==",
        "items": [
            {
                "message_id": "om_dc13264520392913993dd051dba21dcf",
                "root_id": "om_40eb06e7b84dc71c03e009ad3c754195",
                "parent_id": "om_d4be107c616aed9c1da8ed8068570a9f",
                "thread_id": "omt_d4be107c616a",
                "msg_type": "interactive",
                "create_time": "1615380573411",
                "update_time": "1615380573411",
                "deleted": false,
                "updated": false,
                "chat_id": "oc_5ad11d72b830411d72b836c20",
                "sender": {
                    "id": "cli_9f427eec54ae901b",
                    "id_type": "app_id",
                    "sender_type": "app",
                    "tenant_key": "736588c9260f175e"
                },
                "body": {
                    "content": "{\"text\":\"test content\"}"
                },
                "mentions": [
                    {
                        "key": "@_user_1",
                        "id": "ou_155184d1e73cbfb8973e5a9e698e74f2",
                        "id_type": "open_id",
                        "name": "Tom",
                        "tenant_key": "736588c9260f175e"
                    }
                ],
                "upper_message_id": "om_40eb06e7b84dc71c03e009ad3c754195"
            }
        ]
    }
}
```

### 错误码

HTTP状态码 | 错误码 | 描述 | 排查建议
---|---|---|---
400 | 230001 | Your request contains an invalid request parameter. | 参数错误，请根据接口返回的错误信息并参考文档检查输入参数。
400 | 230002 | The bot can not be outside the group. | 机器人不在对应群组中。
400 | 230006 | Bot ability is not activated. | [机器人能力](https://open.larksuite.com/document/uAjLw4CM/ugTN1YjL4UTN24CO1UjN/trouble-shooting/how-to-enable-bot-ability)未启用 。可在[开发者后台](https://open.larksuite.com/app) -> 应用能力 -> 添加应用能力页面添加机器人功能，发布新版本后生效。
400 | 230027 | Lack of necessary permissions. | 请根据本文档中的权限要求部分补充所需权限，如获取群组中所有消息权限。此外，暂不支持在外部群中进行本操作。
400 | 230073 | The thread is invisible to the operator. | 该话题对于操作者不可见。 若群聊关闭了“新成员可查看历史消息”且此话题为操作者进入群聊前创建的，则该话题需要操作者被动订阅才可见，如其他用户在话题中@操作者。
400 | 230110 | Action unavailable as the message has been deleted. | 消息已删除。

其他未列出的错误码请参见[服务端通用错误码](https://open.larksuite.com/document/ukTMukTMukTM/ugjM14COyUjL4ITN)

