即时通讯 REST API
概览
请求的 Base URL 可以在云服务控制台 > 设置 > 应用 Keys > 服务器地址查看。
对于 POST 和 PUT 请求,请求的主体必须是 JSON 格式,而且 HTTP Header 的 Content-Type 需要设置为 application/json
。
请求的鉴权是通过 HTTP Header 里面包含的键值对来进行的,详见数据存储 REST API 使用详解中《请求格式》一节的说明。
_Conversation
表包含一些内置的关键字段定义了对话的属性、成员等,单聊、群聊、聊天室、服务号均在此表中,详见即时通讯总览的《对话》一节。
不过为了避免出现数据不一致问题,我们不推荐调用数据存储相关的 API 直接操作 _Conversation
表中的数据。
当前的 API 版本为 1.2
:
- 单聊、群聊相关 API 以
rtm/conversations
标示 - 聊天室相关 API 以
rtm/chatrooms
标示,在_Conversation
表内用字段tr
为 true 标示。 - 服务号相关 API 以
rtm/service-conversations
标示,在_Conversation
表内用字段sys
为 true 标示。
除此之外,与 client 相关的请求以 rtm/clients
标示。
最后,一些全局性质的 API 直接以 rtm/{function}
标示,如 rtm/all-conversations
可查询所有类型的对话。
单聊、群聊
创建对话
在 _Conversation
表默认 ACL 权限下本接口要求使用 master key。
curl -X POST \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{masterkey}},master" \
-H "Content-Type: application/json" \
-d '{"name":"My First Conversation", "m": ["BillGates", "SteveJobs"], "unique": true}' \
https://{{host}}/1.2/rtm/conversations
上面的例子会创建一个最简单的对话,包括两个 client ID 为 BillGates 和 SteveJobs 的初始成员。对话创建成功会返回 objectId,即即时通讯中的对话 ID,客户端就可以通过这个 ID 发送消息了。新创建的对话可以在 _Conversation
表内找到。
对话的字段可参考即时通讯总览的《对话》一节。
传入 "unique": true
参数可以保证对话的唯一性。
返回
{
"unique":true,
"updatedAt":"2020-05-26T06:42:31.492Z",
"name":"My First Conversation",
"objectId":"5eccba570d3a42c5fd4e25c3",
"m":["BillGates","SteveJobs"],
"createdAt":"2020-05-26T06:42:31.482Z",
"uniqueId":"6c7b0e5afcae9aa1139a0afa25833dec"
}
需要注意,群聊与单聊的唯一区 别是 client 数量,API 层面是一致的。
查询对话
在 _Conversation
表默认 ACL 权限下本接口要求使用 master key。
curl -X GET \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{masterkey}},master" \
-G \
--data-urlencode 'where={"name": "first conversation"}' \
--data-urlencode 'skip=1' \
--data-urlencode 'limit=20' \
https://{{host}}/1.2/rtm/conversations
参数 | 约束 | 说明 |
---|---|---|
skip | 可选 | |
limit | 可选 | 与 skip 联合使用实现分页 |
where | 可选 | 参见数据存储 REST API 使用详解的《查询》一节 |
返回
{"results": [
{"name":"test conv1",
"m":["tom", "jerry"],
"createdAt":"2018-01-17T04:15:33.386Z",
"updatedAt":"2018-01-17T04:15:33.386Z",
"objectId":"5a5ecde6c3422b738c8779d7"}
]}
更新对话
在 _Conversation
表默认 ACL 权限下本接口要求使用 master key。
curl -X PUT \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{masterkey}},master" \
-H "Content-Type: application/json" \
-d '{"name":"Updated Conversation"}' \
https://{{host}}/1.2/rtm/conversations/{conv_id}
_Conversation
表除 m 字段均可通过这个接口更新。
返回
{"updatedAt":"2018-01-16T03:40:37.683Z", "objectId":"5a5d7433c3422b31ed845e76"}
删除对话
在 _Conversation
表默认 ACL 权限下本接口要求使用 master key。
curl -X DELETE \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{masterkey}},master" \
https://{{host}}/1.2/rtm/conversations/{conv_id}
返回
{}
增加成员
在 _Conversation
表默认 ACL 权限下本接口要求使用 master key。
curl -X POST \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{masterkey}},master" \
-H "Content-Type: application/json" \
-d '{"client_ids": ["Tom", "Jerry"]}' \
https://{{host}}/1.2/rtm/conversations/{conv_id}/members
返回
{"updatedAt":"2018-01-16T03:40:37.683Z", "objectId":"5a5d7433c3422b31ed845e76"}
移除成员
在 _Conversation
表默认 ACL 权限下本接口要求使用 master key。
curl -X DELETE \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{masterkey}},master" \
-H "Content-Type: application/json" \
-d '{"client_ids": ["Tom", "Jerry"]}' \
https://{{host}}/1.2/rtm/conversations/{conv_id}/members
返回
{"updatedAt":"2018-01-16T03:40:37.683Z", "objectId":"5a5d7433c3422b31ed845e76"}
查询成员
在 _Conversation
表默认 ACL 权限下本接口要求使用 master key。
curl -X GET \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{masterkey}},master" \
https://{{host}}/1.2/rtm/conversations/{conv_id}/members
返回
{"result": ["client1", "client2"]}
增加静音用户
在 _Conversation
表默认 ACL 权限下本接口要求使用 master key。
curl -X POST \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{masterkey}},master" \
-H "Content-Type: application/json" \
-d '{"client_ids": ["Tom", "Jerry"]}' \
https://{{host}}/1.2/rtm/conversations/{conv_id}/mutes
参数 | 说明 |
---|---|
client_ids | 要静音的 Client ID ,数组 |
返回
{"updatedAt":"2018-01-16T03:40:37.683Z", "objectId":"5a5d7433c3422b31ed845e76"}
移除静音用户
在 _Conversation
表默认 ACL 权限下本接口要求使用 master key。
curl -X DELETE \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{masterkey}},master" \
-H "Content-Type: application/json" \
-d '{"client_ids": ["Tom", "Jerry"]}' \
https://{{host}}/1.2/rtm/conversations/{conv_id}/mutes
返回
{"updatedAt":"2018-01-16T03:40:37.683Z", "objectId":"5a5d7433c3422b31ed845e76"}
查询静音用户
在 _Conversation
表默认 ACL 权限下本接口要求使用 master key。
curl -X GET \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{masterkey}},master" \
https://{{host}}/1.2/rtm/conversations/{conv_id}/mutes
返回
{"result": ["client1", "client2"]}
单聊、群聊-发消息
该接口要求使用 master key。
curl -X POST \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{masterkey}},master" \
-H "Content-Type: application/json" \
-d '{"from_client": "", "message": ""}' \
https://{{host}}/1.2/rtm/conversations/{conv_id}/messages
注意,由于这个接口的管理性质,当你通过这个接口发送消息时,我们不会检查 from_client 是否有权限给这个对话发送消息,而是统统放行,请谨慎使用这个接口。 如果你在应用中使用了我们内部定义的富媒体消息格式,在发送消息时 message 字段需要遵守相应的格式要求。
参数 | 约束 | 说明 |
---|---|---|
from_client | 必填 | 消息的发件人 client Id |
message | 必填 | 消息内容(这里的消息内容的本质是字符串,但是我们对字符串内部的格式没有做限定,理论上开发者可以随意发送任意格式,只要大小不超过 5 KB 限制即可。) |
transient | 可选 | 是否为暂态消息,默认 false |
no_sync | 可选 | 默认情况下消息会被同步给在线的 from_client 用户的客户端,设置为 true 禁用此功能。 |
push_data | 可选 | 以消息附件方式设置本条消息的离线推送通知内容。如果目标接收者使用的是 iOS 设备并且当前不在线,我们会按照该参数填写的内容来发离线推送。请参看即时通讯开发指南第二篇的《离线推送通知》一节的说明。 |
priority | 可选 | 定义消息优先级,可选值为 high 、normal、low,分别对应高、中、低三种优先级。该参数大小写不敏感,默认为中优先级 normal。本参数仅对暂态消息或聊天室的消息有效,高优先级下在服务端与用户设备的连接拥塞时依然排队。 |
mention_all | 可选 | 布尔类型,用于提醒对话内所有成员注意本消息。 |
mention_client_ids | 可选 | 数组类型,表示需要提醒注意本消息的对话内成员 client_id 列表,最多能包含 20 个 client Id。 |
返回说明:
默认情况下发送消息 API 使用异步的方式,调用后返回消息 id 和接收消息的服务器时间戳,例如 {"msg-id":"qNkRkFWOeSqP65S9fDyHJw", "timestamp":1495431811151}
。
频率限制:
此接口受频率限制,详见后文接口请求频率限制一节。
查询历史消息
该接口要求使用 master key。 为了保证获取聊天记录的安全性,可以开启签名认证,具体可以参考即时通讯开发指南第三篇的《安全与签名》一节。
curl -X GET \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{masterkey}},master" \
-H "Content-Type: application/json" \
https://{{host}}/1.2/rtm/conversations/{conv_id}/messages
参数 | 约束 | 说明 |
---|---|---|
msgid | 可选 | 起始的消息 id,使用时必须加上对应消息的时间戳 timestamp 参数,作为查询的起点 |
timestamp | 可选 | 查询起始的时间戳。默认是当前时间,单位是毫秒 |
till_msgid | 可选 | 查询终止的消息 id。使用时必须加上消息的时间戳 till_timestamp 参数,作为查询的终点 |
till_timestamp | 可选 | 查询终止的时间戳,默认为 0,单位是毫秒 |
include_start | 可选 | 是否包含由 timestamp 与 msgid 确定的起始消息。布尔值,默认为 false |
include_stop | 可选 | 是否包含由 till_timestamp 与 till_msgid 确定的终止消息。布尔值,默认为 false |
reversed | 可选 | 以 默认排序(默认按时间降序)相反的方向返回结果,这时 till_timestamp 默认为当前时间戳,timestamp 默认为 0。布尔值,默认为 false |
limit | 可选 | 返回条数限制,可选,默认 100 条,最大 1000 条 |
client_id | 可选 | 查看者 id(签名参数) |
nonce | 可选 | 签名随机字符串(签名参数) |
signature_ts | 可选 | 签名时间戳(签名参数),单位是毫秒 |
signature | 可选 | 签名(签名参数) |
本接口时间参数较多,这里举一示例供大家参考。比如某对话内有三条消息,id 分别为 id1、id2、id3,发消息的时间分别是 t1、t2、t3(t1 < t2 < t3),下面列举出不同参数组合的查询结果(空白表示使用默认值):
timestamp | msgid | till_timestamp | till_msgid | include_start | include_stop | reversed | 结果 |
---|---|---|---|---|---|---|---|
t3 | id3 | t1 | id1 | id2 | |||
t3 | id3 | t1 | id1 | true | id3 id2 | ||
t3 | id3 | t1 | id1 | true | id2 id1 | ||
t1 | id1 | t3 | id3 | true | id2 | ||
t1 | id1 | t3 | id3 | true | true | id1 id2 | |
t1 | id1 | t3 | id3 | true | true | id2 id3 |
返回数据格式,JSON 数组,默认按消息记录从新到旧排序,设置请求参数 reversed
后以相反的方向排序。
返回:
[
{
"timestamp": 1408008498571,
"conv-id": "219946ef32e40c515d33ae6975a5c593",
"data": "今天天气不错!",
"from": "u111872755_9d0461adf9c267ae263b3742c60fa",
"msg-id": "vdkGm4dtRNmhQ5gqUTFBiA",
"is-conv": true,
"is-room": false,
"to": "5541c02ce4b0f83f4d44414e",
"bin": false,
"from-ip": "202.117.15.217"
},
...
]
如需查询某个用户发出的消息,可以调用 GET /rtm/clients/{client_id}/messages
这个接口。
如需查询整个应用的历史消息,可以调用 GET /rtm/messages
这个接口。
单聊、群聊-修改消息
该接口要求使用 master key。
curl -X PUT \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{masterkey}},master" \
-H "Content-Type: application/json" \
-d '{"from_client": "", "message": "", "timestamp": 123}' \
https://{{host}}/1.2/rtm/conversations/{conv_id}/messages/{message_id}
参数 | 约束 | 说明 |
---|---|---|
from_client | 必填 | 消息的发件人 client ID |
message | 必填 | 消息体 |
timestamp | 必填 | 消息的时间戳 |
成功则返回状态码 200 OK
。
频率限制:
此接口受频率限制,详见后文接口请求频率限制一节。
单聊、群聊-撤回消息
该接口要求使用 master key。需要相应 SDK 的支持,具体可参考之前的修改消息接口。
curl -X PUT \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{masterkey}},master" \
-H "Content-Type: application/json" \
-d '{"from_client": "", "timestamp": 123}' \
https://{{host}}/1.2/rtm/conversations/{conv_id}/messages/{message_id}/recall
参数 | 约束 | 说明 |
---|---|---|
from_client | 必填 | 消息的发件人 client ID |
timestamp | 必填 | 消息的时间戳 |
成功则返回状态码 200 OK
。
频率限制:
此接口受频率限制,详见后文接口请求频率限制一节。
删除消息
该接口要求使用 master key。
curl -X DELETE \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{masterkey}},master" \
-G \
--data-urlencode 'from_client=some-client-id' \
--data-urlencode 'timestamp=123' \
https://{{host}}/1.2/rtm/conversations/{conv_id}/messages/{message_id}
注意,该接口仅删除服务端的消息,对客户端无影响。
参数 | 约束 | 说明 |
---|---|---|
from_client | 必填 | 消息的发件人 client ID |
timestamp | 必填 | 消息的时间戳 |
返回:
{}
增加临时性禁言用户
该接口要求使用 master key。
curl -X POST \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{masterkey}},master" \
-H "Content-Type: application/json" \
-d '{"client_id": "some-client-id", "ttl": 50}' \
https://{{host}}/1.2/rtm/conversations/{conv_id}/temporary-silenceds
参数 | 说明 |
---|---|
client_id | 要禁言的 Client ID ,字符串 |
ttl | 禁言的时间,秒数,最长 24 小时 |
返回
{}
移除临时性禁言用户
该接口要求使用 master key。
curl -X DELETE \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{masterkey}},master" \
-G \
--data-urlencode 'client_id=some-client-id' \
https://{{host}}/1.2/rtm/conversations/{conv_id}/temporary-silenceds
返回
{}
对话权限
该功能介绍可参考即时通讯开发指南第三篇中的《权限管理与黑名单》一节。
增加权限
该接口要求使用 master key。 每个对话最多允许添加 500 个永久性禁言用户。
curl -X POST \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{masterkey}},master" \
-H "Content-Type: application/json" \
-d '{"clientId": "client", "role": "role"}' \
https://{{host}}/1.2/rtm/conversations/{conv_id}/member-infos
参数 | 说明 |
---|---|
clientId | 用户 ID,字符串 |
role | 角色,可选值 Member、Manager、Owner |
返回
{"updatedAt":"2018-01-16T03:40:37.683Z", "objectId":"5a5d7433c3422b31ed845e76"}
删除权限
该接口要求使用 master key。
curl -X DELETE \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{masterkey}},master" \
https://{{host}}/1.2/rtm/conversations/{conv_id}/member-infos/{info-id}
参数 | 说明 |
---|---|
info-id | 该记录对应的 objectId |
返回
{}
更新权限
该接口要求使用 master key。
curl -X PUT \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{masterkey}},master" \
-H "Content-Type: application/json" \
-d '{"clientId": "client", "role": "role"}' \
https://{{host}}/1.2/rtm/conversations/{conv_id}/member-infos/{info-id}
参数 | 说明 |
---|---|
clientId | 用户 ID,字符串 |
role | 角色,可选值 Member、Manager、Owner。可选 |
info-id | 该记录对应的 objectId |
返回
{"updatedAt":"2018-01-16T03:40:37.683Z", "objectId":"5a5d7433c3422b31ed845e76"}
查询权限
该接口要求使用 master key。
curl -X GET \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{masterkey}},master" \
-G \
--data-urlencode 'skip=1' \
--data-urlencode 'limit=20' \
https://{{host}}/1.2/rtm/conversations/{conv_id}/member-infos
参数 | 说明 |
---|---|
skip | |
limit | 与 skip 联合使用实现分页 |
role | 本次查询只希望包含该角色 |
返回
{"results": [{"clientId":"client1", "objectId":"5a5d7433c3422b31ed845e76", "role": "Manager"}]}
增加永久性禁言用户
该接口要求使用 master key。 每个对话最多允许添加 500 个永久性禁言用户。
curl -X POST \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{masterkey}},master" \
-H "Content-Type: application/json" \
-d '{"client_ids": ["client1", "client2"]}' \
https://{{host}}/1.2/rtm/conversations/{conv_id}/permanent-silenceds
参数 | 说明 |
---|---|
client_ids | 要禁言的 Client ID 列表,数组 |
返回
{}
移除永久性禁言用户
该接口要求使用 master key。
curl -X DELETE \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{masterkey}},master" \
-d '{"client_ids": ["client1", "client2"]}' \
https://{{host}}/1.2/rtm/conversations/{conv_id}/permanent-silenceds
返回
{}
查询永久性禁言列表
该接口要求使用 master key。
curl -X GET \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{masterkey}},master" \
-G \
--data-urlencode 'limit=20' \
https://{{host}}/1.2/rtm/conversations/{conv_id}/permanent-silenceds
参数 | 约束 | 说明 |
---|---|---|
limit | 可选 | 与 next 联合使用实现分页,默认 10 |
next | 可选 | 第一次查询时返回,后面的查询带着这个参数,实现分页查询 |
返回
{"client_ids": ["client1", "client2"]}
黑名单
该功能介绍可参考即时通讯开发指南第三篇中的《权限管理与黑名单》一节。
增加对话黑名单
该接口要求使用 master key。
加入黑名单的 client 不允许再加入该对话,如果之前在该对话中将被移除。每个对话最多允许添加 500 个黑名单。
curl -X POST \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{masterkey}},master" \
-H "Content-Type: application/json" \
-d '{"client_ids": ["client1", "client2"]}' \
https://{{host}}/1.2/rtm/conversations/{conv_id}/blacklists
参数 | 说明 |
---|---|
client_ids | 要拉黑的 Client ID 列表,数组 |
返回
{}
移除对话黑名单
该接口要求使用 master key。
curl -X DELETE \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{masterkey}},master" \
-d '{"client_ids": ["client1", "client2"]}' \
https://{{host}}/1.2/rtm/conversations/{conv_id}/blacklists
返回
{}
查询对话黑名单
该接口要求使用 master key。
curl -X GET \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{masterkey}},master" \
-d '{"client_ids": ["client1", "client2"]}' \
https://{{host}}/1.2/rtm/conversations/{conv_id}/blacklists
参数 | 约束 | 说明 |
---|---|---|
limit | 可选 | 与 next 联合使用实现分页,默认 10 |
next | 可选 | 第一次查询时返回,后面的查询带着这个参数,实现分页查询 |
返回
{"client_ids": ["client1", "client2"]}
聊天室
创建聊天室
在 _Conversation
表默认 ACL 权限下本接口要求使用 master key。
curl -X POST \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{masterkey}},master" \
-H "Content-Type: application/json" \
-d '{"name":"My First Chatroom"}' \
https://{{host}}/1.2/rtm/chatrooms
对话的字段可参考即时通讯总览的《对话》一节。
返回
{"objectId":"5a5d7432c3422b31ed845e75", "createdAt":"2018-01-16T03:40:32.814Z"}
查询聊天室
在 _Conversation
表默认 ACL 权限下本接口要求使用 master key。
curl -X GET \
-H "X-LC-Id: {{appid}}" \
-H "X-LC-Key: {{masterkey}},master" \
-G \
--data-urlencode 'where={"name": "chatroom"}' \
--data-urlencode 'skip=1' \
--data-urlencode 'limit=20' \
https://{{host}}/1.2/rtm/chatrooms
参数 | 约束 | 说明 |
---|---|---|
skip | 可选 | |
limit | 可选 | 与 skip 联合使用实现分页 |
where | 可选 | 请参考数据存储 REST API 使用详解 |