# 接收历史消息 (/zh-CN/api-reference/api-ref/signaling/receive)

> For AI agents: see the complete documentation index at [llms.txt](/llms.txt).

## 接口描述 [#接口描述]

`History` 接口获取指定频道中存储的历史消息。

## 接口方法 [#接口方法]

```shell
GET https://api.sd-rtn.com/rtm/v2/history/{:appId}/userId/{:userId}/channelType/{:channelType}/channel/{:channelName}?start={:startTime}&end={:endTime}&messageCount={:messageCount}
```

| 参数 | 类型 | 必填 | 描述 |
| --- | --- | --- | --- |
| `appId` | string | 是 | 你的声网项目的 App ID。 |
| `userId` | string | 是 | [receive-restful-html-0-2-3 details](#table-slot-receive-restful-html-0-2-3) |
| `channelType` | string | 是 | [receive-restful-html-0-3-3 details](#table-slot-receive-restful-html-0-3-3) |
| `channelName` | string | 是 | [receive-restful-html-0-4-3 details](#table-slot-receive-restful-html-0-4-3) |
| `start` | long | 否 | 历史消息开始的时间戳，可以为 0，不填默认为 0。 |
| `end` | long | 否 | 历史消息结束的时间戳，可以为 0，不填默认为 0。 |
| `messageCount` | int | 否 | 单次查询的最大消息数。如果单次查询的时间范围内消息数大于该值，则需多次调用该方法进行查询。取值范围为 (0,100]，不填默认为 100。 |

<a id="table-slot-receive-restful-html-0-2-3"></a>

**receive-restful-html-0-2-3 details**

此次操作的 User ID。

以下为支持的字符集范围（共 89 个字符）:

- 26 个小写英文字母 a-Z
- 26 个大写英文字母 A-Z
- 10 个数字 0-9
- 空格
- `!`, `#`, `$`, `%`, `&`, `(`, `)`, `+`, `-`, `:`, `;`, `<`, `=`, `.`, `>`, `?`, `@`, `[`, `]`, `^`, `_`, `{`, `}`, `|`, `~`, `,`

    :::warning[注意]
    字符长度限制为：0 < 字符长度 &le; 64。
    :::

<a id="table-slot-receive-restful-html-0-3-3"></a>

**receive-restful-html-0-3-3 details**

频道类型：

- `message`：消息频道
- `user`：用户频道

<a id="table-slot-receive-restful-html-0-4-3"></a>

**receive-restful-html-0-4-3 details**

频道名。

以下为支持的字符集范围（共 89 个字符）:

- 26 个小写英文字母 a-Z
- 26 个大写英文字母 A-Z
- 10 个数字 0-9
- 空格
- `!`, `#`, `$`, `%`, `&`, `(`, `)`, `+`, `-`, `:`, `;`, `<`, `=`, `.`, `>`, `?`, `@`, `[`, `]`, `^`, `_`, `{`, `}`, `|`, `~`, `,`

    :::warning[注意]
    字符长度限制为：0 < 字符长度 &le; 64。
    :::

## 基本用法 [#基本用法]

```shell tab="cURL" tabGroup="language"
curl -L 'https://api.sd-rtn.com/rtm/v2/history/{appId}/userId/Tony/channelType/message/channel/chatroom?start=17809876&end=1781000&messageCount=100' \
-H 'Accept: application/json'\
-H 'Authorization: agora token=007xxxxx'\
```

```javascript tab="Node.js" tabGroup="language"
const axios = require('axios');
 
let config = {
    method: 'get',
    maxBodyLength: Infinity,
    url: 'https://api.sd-rtn.com/rtm/v2/history/{:appId}/userId/Tony/channelType/message/channel/chatroom?start=17809876&end=1781000&messageCount=100',
    headers: {
        'Accept': 'application/json'
        'Authorization': 'agora token=007xxxxx'
        }
    };
 
axios.request(config)
.then((response) => {
  console.log(JSON.stringify(response.data));
})
.catch((error) => {
  console.log(error);
});
```

## 返回值 [#返回值]

执行后将返回以下数据：

```json
{
  "errorCode": 200,
  "error": false,
  "requestId": 1121_1234231,
  "operation": "getMessage",
  "reason": "xxxxxxxxxxxx",
  "timestamp":1234567,
  "data":{
    "messageList": HistoryMessage[];
    "count": number, 
    "newStart": number
  }
}
```

其中，`HistoryMessage` 结构如下：

```json
{
  "messageType": "STRING",
  "message": "xxxxxxxxxx",
  "customType": "xxx",
  "publisher": "xxxxx",
  "timestamp":1234567
}
```

| 参数 | 类型 | 描述 |
| --- | --- | --- |
| `errorCode` | number | 错误码。200 表示请求成功。如果不为 200，你可以参考[错误码](/zh-CN/realtime-media/rtm/reference/usage-limits-and-errors/response-code)了解出错的原因并进行排障。 |
| `error` | boolean | [receive-restful-html-1-2-2 details](#table-slot-receive-restful-html-1-2-2) |
| `requestId` | string | 当前请求的请求 ID。 |
| `operation` | string | 当前请求的具体操作。 |
| `reason` | string | 出错原因。 |
| `timestamp` | number | 发送请求的时间戳。 |
| `data` | object | [receive-restful-html-1-7-2 details](#table-slot-receive-restful-html-1-7-2) |

<a id="table-slot-receive-restful-html-1-2-2"></a>

**receive-restful-html-1-2-2 details**

当前请求是否出错。

- `false` 代表请求成功。
- `true` 代表请求失败。

<a id="table-slot-receive-restful-html-1-7-2"></a>

**receive-restful-html-1-7-2 details**

响应的具体 Payload 信息，包含如下字段：

- `messageList`：获取的历史消息列表，为一个 Array List，包含如下参数：
  - `messageType`：消息数据类型：
    - 如果 `messageType` 为 "STRING"，直接取 `message` 字段内容即可。
    - 如果 `messageType` 为 "BINARY"，消息内容为 `message` 字段 base64 编码后的结果。
  - `message`：具体的消息内容。
  - `customType`：自定义类型。
  - `publisher`：发布消息的用户。
  - `timestamp`：消息发布时的时间戳。
- `count`：共获得的历史消息数量。
- `newStart`：新的历史消息开始的时间戳。
