这是本文档旧的修订版!
发送消息
聊天相关 API,可以发送文本消息,发送图片消息,发送语音消息,发送视频消息,发送透传消息和发送扩展消息。
流程说明
消息类型 | 说明 |
---|---|
发送文本/透传消息 | 直接编辑内容发送 |
发送图片/语音/视频消息 | 需要先上传这三类文件,从接口返回值中获取到相应的参数,按照 API 要求编辑到消息体中然后的发送 |
发送文本消息
给一个或者多个用户,或者一个或者多个群组发送消息,并且通过可选的 from 字段让接收方看到发送方是不同的人。同时,支持扩展字段,通过 ext 属性,APP 可以发送自己专属的消息结构。
注意:在调用程序中,请求体如果超过 5kb 会导致413错误,需要拆成几个更小的请求体重试,同时用户消息+扩展字段的长度在4k字节以内。详见接口限流说明。
HTTP Request
//{org_name}/{app_name}/messages |
---|
Request Headers
参数 | 说明 |
---|---|
Content-Type | application/json |
Authorization | Bearer ${token} |
Request Body
参数 | 说明 |
---|---|
target_type | 发送的目标类型;users:给用户发消息,chatgroups:给群发消息,chatrooms:给聊天室发消息 |
target | 发送的目标;注意这里需要用数组,并且向数组内添加的用户不能超过1000个,即使只有一个用户,也要用数组 ['u1'];给用户发送时数组元素是用户名,给群组发送时,数组元素是groupid |
msg | 消息内容 |
type | 消息类型;txt:文本消息,img:图片消息,loc:位置消息,audio:语音消息,video:视频消息,file:文件消息 |
from | 表示消息发送者;无此字段Server会默认设置为“from”:“admin”,有from字段但值为空串(“”)时请求失败 |
Response Body
在返回值中查看data字段包含的信息
参数 | 说明 |
---|---|
username | 接受消息的用户名 |
success | 表示消息发送成功 |
请求示例
curl -X POST -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Bearer YWMtP5n9zvOQEei7KclxPqJTkgAAAAAAAAAAAAAAAAAAAAGL4CTw6XgR6LaXXVmNX4QCAgMAAAFnXcBpfQBPGgDC09w5IdrfqG_H8_F53VLVTG0_82GXyEF8ZdMCt9-UpQ' -d '{"target_type": "users","target": ["user2","user3"],"msg": {"type": "txt","msg": "testmessage"},"from": "user1"}' 'http://a1.easemob.com/easemob-demo/testapp/messages'
可能返回的结果示例
返回值200,表示消息发送成功
{
"action": "post",
"application": "8be024f0-e978-11e8-b697-5d598d5f8402",
"path": "/messages",
"uri": "https://a1.easemob.com/easemob-demo/testapp/messages",
"data": {
"user2": "success",
"user3": "success"
},
"timestamp": 1543922150902,
"duration": 1,
"organization": "easemob-demo",
"applicationName": "testapp"
}
返回值400,表示 massage 结构错误
{
"error": "json_parse",
"timestamp": 1543922465246,
"duration": 0,
"exception": "org.codehaus.jackson.JsonParseException",
"error_description": "Unexpected character ('}' (code 125)): was expecting double-quote to start field name\n at [Source: java.io.BufferedInputStream@7ba7eef2; line: 11, column: 2]"
}
返回值401,表示未授权[无token、token错误、token过期]
{
"error": "auth_bad_access_token",
"timestamp": 1543922590032,
"duration": 0,
"exception": "org.apache.usergrid.rest.exceptions.SecurityException",
"error_description": "Unable to authenticate due to corrupt access token"
}
如果返回结果是429、503或者其他5xx,有可能代表该接口被限流了,请稍微暂停一下并重试。详见接口限流说明
发送图片消息
给一个或者多个用户,或者一个或者多个群组发送消息,并且通过可选的 from 字段让接收方看到发送方是不同的人。同时,支持扩展字段,通过 ext 属性,APP 可以发送自己专属的消息结构。
注意:在调用程序中,请求体如果超过 5kb 会导致413错误,需要拆成几个更小的请求体重试。详见接口限流说明。
HTTP Request
//{org_name}/{app_name}/messages |
---|
Request Headers
参数 | 说明 |
---|---|
Content-Type | application/json |
Authorization | Bearer ${token} |
Request Body
参数 | 说明 |
---|---|
target_type | 发送的目标类型;users:给用户发消息,chatgroups:给群发消息,chatrooms:给聊天室发消息 |
target | 发送的目标;注意这里需要用数组,并且向数组内添加的用户不能超过1000个,即使只有一个用户,也要用数组 ['u1'];给用户发送时数组元素是用户名,给群组发送时,数组元素是groupid |
msg | 消息内容 |
type | 消息类型;txt:文本消息,img:图片消息,loc:位置消息,audio:语音消息,video:视频消息,file:文件消息 |
url | 域名/orgname/appname/chatfiles/成功上传文件返回的UUID。参考请求示例 |
filename | 图片名称 |
secret | 成功上传文件后返回的secret |
size | 图片尺寸;height:高度,width:宽度 |
from | 表示消息发送者;无此字段Server会默认设置为“from”:“admin”,有from字段但值为空串(“”)时请求失败 |
Response Body
在返回值中查看data字段包含的信息
参数 | 说明 |
---|---|
username | 接受消息的用户名 |
success | 表示消息发送成功 |
请求示例
curl -X POST -i 'https://a1.easemob.com/easemob-demo/testapp/messages' -H 'Authorization: Bearer YWMtsFVigGSuEeSTc7k5183Z5QAAAUqzeFx_9IjRch-ZxNbIlBIvx_4GWvzheSU' -d '{"target_type":"users","target":["user2"],"from":"user1","msg":{"type":"img","filename":"testimg.jpg","secret":"VfEpSmSvEeS7yU8dwa9rAQc-DIL2HhmpujTNfSTsrDt6eNb_","url":"https://a1.easemob.com/easemob-demo/testapp/chatfiles/55f12940-64af-11e4-8a5b-ff2336f03252","size":{"width":480,"height":720}}}'
可能返回的结果示例
返回值200,表示消息发送成功
{
"action" : "post",
"application" : "4d7e4ba0-dc4a-11e3-90d5-e1ffbaacdaf5",
"uri" : "https://a1.easemob.com/easemob-demo/testapp",
"entities" : [ ],
"data" : {
"user2" : "success"
},
"timestamp" : 1415166497129,
"duration" : 12,
"organization" : "easemob-demo",
"applicationName" : "testapp"
}
返回值400,表示 massage 结构错误
{
"error": "json_parse",
"timestamp": 1543922465246,
"duration": 0,
"exception": "org.codehaus.jackson.JsonParseException",
"error_description": "Unexpected character ('}' (code 125)): was expecting double-quote to start field name\n at [Source: java.io.BufferedInputStream@7ba7eef2; line: 11, column: 2]"
}
返回值401,表示未授权[无token、token错误、token过期]
{
"error": "auth_bad_access_token",
"timestamp": 1543922590032,
"duration": 0,
"exception": "org.apache.usergrid.rest.exceptions.SecurityException",
"error_description": "Unable to authenticate due to corrupt access token"
}
如果返回结果是429、503或者其他5xx,有可能代表该接口被限流了,请稍微暂停一下并重试。详见接口限流说明
发送语音消息
发送语音文件,需要先上传语音文件,然后再发送此消息。(URL 中的 UUID 和 secret 可以从上传后的 response 获取)
注意:在调用程序中,请求体如果超过 5kb 会导致413错误,需要拆成几个更小的请求体重试。详见接口限流说明。
HTTP Request
//{org_name}/{app_name}/messages |
---|
Request Headers
参数 | 说明 |
---|---|
Content-Type | application/json |
Authorization | Bearer ${token} |
Request Body
参数 | 说明 |
---|---|
target_type | 发送的目标类型;users:给用户发消息,chatgroups:给群发消息,chatrooms:给聊天室发消息 |
target | 发送的目标;注意这里需要用数组,并且向数组内添加的用户不能超过1000个,即使只有一个用户,也要用数组 ['u1'];给用户发送时数组元素是用户名,给群组发送时,数组元素是groupid |
msg | 消息内容 |
type | 消息类型;txt:文本消息,img:图片消息,loc:位置消息,audio:语音消息,video:视频消息,file:文件消息 |
url | 成功上传文件返回的UUID |
filename | 语音名称 |
secret | 成功上传文件后返回的secret |
length | 语音时间(单位:秒) |
from | 表示消息发送者;无此字段Server会默认设置为“from”:“admin”,有from字段但值为空串(“”)时请求失败 |
Response Body
在返回值中查看data字段包含的信息
参数 | 说明 |
---|---|
username | 接受消息的用户名 |
success | 表示消息发送成功 |
请求示例
curl -X POST -H "Authorization: Bearer YWMtxc6K0L1aEeKf9LWFzT9xEAAAAT7MNR_9OcNq-GwPsKwj_TruuxZfFSC2eIQ" "https://a1.easemob.com/easemob-demo/testapp/messages" -d '{"target_type" : "users","target" : ["user2", "user3"],"msg" : {"type": "audio","url": "https://a1.easemob.com/easemob-demo/testapp/chatfiles/1dfc7f50-55c6-11e4-8a07-7d75b8fb3d42","filename": "testaudio.amr","length": 10,"secret": "Hfx_WlXGEeSdDW-SuX2EaZcXDC7ZEig3OgKZye9IzKOwoCjM"},"from" : "user1" }'
可能返回的结果示例
返回值200,表示消息发送成功
{
"action": "post",
"application": "8be024f0-e978-11e8-b697-5d598d5f8402",
"path": "/messages",
"uri": "https://a1.easemob.com/easemob-demo/testapp/messages",
"data": {
"user2": "success",
"user3": "success"
},
"timestamp": 1543922150902,
"duration": 1,
"organization": "easemob-demo",
"applicationName": "testapp"
}
返回值400,表示 massage 结构错误
{
"error": "json_parse",
"timestamp": 1543922465246,
"duration": 0,
"exception": "org.codehaus.jackson.JsonParseException",
"error_description": "Unexpected character ('}' (code 125)): was expecting double-quote to start field name\n at [Source: java.io.BufferedInputStream@7ba7eef2; line: 11, column: 2]"
}
返回值401,表示未授权[无token、token错误、token过期]
{
"error": "auth_bad_access_token",
"timestamp": 1543922590032,
"duration": 0,
"exception": "org.apache.usergrid.rest.exceptions.SecurityException",
"error_description": "Unable to authenticate due to corrupt access token"
}
如果返回结果是429、503或者其他5xx,有可能代表该接口被限流了,请稍微暂停一下并重试。详见接口限流说明
发送视频消息
发送视频消息,需要先上传视频文件和视频缩略图文件,然后再发送此消息。(URL 中的 UUID 和 secret 可以从上传后的 response 获取)
注意:在调用程序中,请求体如果超过 5kb 会导致413错误,需要拆成几个更小的请求体重试。详见接口限流说明。
HTTP Request
//{org_name}/{app_name}/messages |
---|
Request Headers
参数 | 说明 |
---|---|
Content-Type | application/json |
Authorization | Bearer ${token} |
Request Body
参数 | 说明 |
---|---|
target_type | 发送的目标类型;users:给用户发消息,chatgroups:给群发消息,chatrooms:给聊天室发消息 |
target | 发送的目标;注意这里需要用数组,并且向数组内添加的用户不能超过1000个,即使只有一个用户,也要用数组 ['u1'];给用户发送时数组元素是用户名,给群组发送时,数组元素是groupid |
type | 消息类型;txt:文本消息,img:图片消息,loc:位置消息,audio:语音消息,video:视频消息,file:文件消息 |
filename | 视频文件名称 |
thumb | 成功上传视频缩略图返回的UUID |
length | 视频播放长度 |
secret | 成功上传视频文件后返回的secret |
file_length | 视频文件大小(单位:字节) |
thumb_secret | 成功上传视频缩略图后返回的secret |
url | 成功上传视频文件返回的UUID |
Response Body
在返回值中查看data字段包含的信息
参数 | 说明 |
---|---|
username | 接受消息的用户名 |
success | 表示消息发送成功 |
请求示例
curl -X POST -i 'https://a1.easemob.com/easemob-demo/testapp/messages' -H 'Authorization: Bearer YWMtxc6K0L1aEeKf9LWFzT9xEAAAAT7MNR_9OcNq-GwPsKwj_TruuxZfFSC2eIQ' -d '{"target_type":"users","target":["user2","user3"],"from":"user1","msg":{"type":"video","filename" : "testvideo.mp4","thumb" : "https://a1.easemob.com/easemob-demo/testapp/chatfiles/67279b20-7f69-11e4-8eee-21d3334b3a97","length" : 0,"secret":"VfEpSmSvEeS7yU8dwa9rAQc-DIL2HhmpujTNfSTsrDt6eNb_","file_length" : 58103,"thumb_secret" : "ZyebKn9pEeSSfY03ROk7ND24zUf74s7HpPN1oMV-1JxN2O2I","url" : "https://a1.easemob.com/easemob-demo/testapp/chatfiles/671dfe30-7f69-11e4-ba67-8fef0d502f46"}}'
可能返回的结果示例
返回值200,表示消息发送成功
{
"action" : "post",
"application" : "4d7e4ba0-dc4a-11e3-90d5-e1ffbaacdaf5",
"uri" : "https://a1.easemob.com/easemob-demo/testapp",
"entities" : [ ],
"data" : {
"user2" : "success",
"user3" : "success"
},
"timestamp" : 1415166234863,
"duration" : 5,
"organization" : "easemob-demo",
"applicationName" : "testapp"
}
返回值400,表示 massage 结构错误
{
"error": "json_parse",
"timestamp": 1543922465246,
"duration": 0,
"exception": "org.codehaus.jackson.JsonParseException",
"error_description": "Unexpected character ('}' (code 125)): was expecting double-quote to start field name\n at [Source: java.io.BufferedInputStream@7ba7eef2; line: 11, column: 2]"
}
返回值401,表示未授权[无token、token错误、token过期]
{
"error": "auth_bad_access_token",
"timestamp": 1543922590032,
"duration": 0,
"exception": "org.apache.usergrid.rest.exceptions.SecurityException",
"error_description": "Unable to authenticate due to corrupt access token"
}
如果返回结果是429、503或者其他5xx,有可能代表该接口被限流了,请稍微暂停一下并重试。详见接口限流说明
发送位置消息
位置消息:获取到地址的经纬度,填写正确地址发送。
注意:在调用程序中,请求体如果超过 5kb 会导致413错误,需要拆成几个更小的请求体重试,同时用户消息+扩展字段的长度在4k字节以内。详见接口限流说明。
HTTP Request
//{org_name}/{app_name}/messages |
---|
Request Headers
参数 | 说明 |
---|---|
Content-Type | application/json |
Authorization | Bearer ${token} |
Request Body
参数 | 说明 |
---|---|
target_type | 发送的目标类型;users:给用户发消息,chatgroups:给群发消息,chatrooms:给聊天室发消息 |
target | 发送的目标;注意这里需要用数组,并且向数组内添加的用户不能超过1000个,即使只有一个用户,也要用数组 ['u1'];给用户发送时数组元素是用户名,给群组发送时,数组元素是groupid |
msg | 消息内容 |
type | 消息类型;txt:文本消息,img:图片消息,loc:位置消息,audio:语音消息,video:视频消息,file:文件消息 |
from | 表示消息发送者;无此字段Server会默认设置为“from”:“admin”,有from字段但值为空串(“”)时请求失败 |
Response Body
在返回值中查看data字段包含的信息
参数 | 说明 |
---|---|
username | 接受消息的用户名 |
success | 表示消息发送成功 |
请求示例
curl -X POST -H 'Content-Type: application/json' -H 'Accept: application/json' -H 'Authorization: Bearer YWMtP5n9zvOQEei7KclxPqJTkgAAAAAAAAAAAAAAAAAAAAGL4CTw6XgR6LaXXVmNX4QCAgMAAAFnXcBpfQBPGgDC09w5IdrfqG_H8_F53VLVTG0_82GXyEF8ZdMCt9-UpQ' -d '{"target_type": "users","target": ["user2"],"msg": {"type": "loc","lat": "39.966","lng":"116.322",."addr":"中国北京市海淀区中关村"},"from": "user1"}' 'http://a1.easemob.com/easemob-demo/testapp/messages'
可能返回的结果示例
返回值200,表示消息发送成功
{
"action": "post",
"application": "8be024f0-e978-11e8-b697-5d598d5f8402",
"path": "/messages",
"uri": "https://a1.easemob.com/easemob-demo/testapp/messages",
"data": {
"user2": "success"
},
"timestamp": 1543922150902,
"duration": 1,
"organization": "easemob-demo",
"applicationName": "testapp"
}
返回值400,表示 massage 结构错误
{
"error": "json_parse",
"timestamp": 1543922465246,
"duration": 0,
"exception": "org.codehaus.jackson.JsonParseException",
"error_description": "Unexpected character ('}' (code 125)): was expecting double-quote to start field name\n at [Source: java.io.BufferedInputStream@7ba7eef2; line: 11, column: 2]"
}
返回值401,表示未授权[无token、token错误、token过期]
{
"error": "auth_bad_access_token",
"timestamp": 1543922590032,
"duration": 0,
"exception": "org.apache.usergrid.rest.exceptions.SecurityException",
"error_description": "Unable to authenticate due to corrupt access token"
}
如果返回结果是429、503或者其他5xx,有可能代表该接口被限流了,请稍微暂停一下并重试。详见接口限流说明
发送透传消息
透传消息:不会在客户端提示(铃声、震动、通知栏等),也不会有 APNS 推送(苹果推送),但可以在客户端监听到,具体功能可以根据自身自定义。
注意:在调用程序中,请求体如果超过 5kb 会导致413错误,需要拆成几个更小的请求体重试。详见接口限流说明。
HTTP Request
//{org_name}/{app_name}/messages |
---|
Request Headers
参数 | 说明 |
---|---|
Content-Type | application/json |
Authorization | Bearer ${token} |
Request Body
参数 | 说明 |
---|---|
target_type | 发送的目标类型;users:给用户发消息,chatgroups:给群发消息,chatrooms:给聊天室发消息 |
target | 发送的目标;注意这里需要用数组,并且向数组内添加的用户不能超过1000个,即使只有一个用户,也要用数组 ['u1'];给用户发送时数组元素是用户名,给群组发送时,数组元素是groupid |
msg | 消息内容 |
type | 消息类型;txt:文本消息,img:图片消息,loc:位置消息,audio:语音消息,video:视频消息,file:文件消息,cmd:透传消息 |
from | 表示消息发送者;无此字段Server会默认设置为“from”:“admin”,有from字段但值为空串(“”)时请求失败 |
Response Body
在返回值中查看data字段包含的信息
参数 | 说明 |
---|---|
username | 接受消息的用户名 |
success | 表示消息发送成功 |
请求示例
curl -X POST -H "Authorization:Bearer YWMtxc6K0L1aEeKf9LWFzT9xEAAAAT7MNR_9OcNq-GwPsKwj_TruuxZfFSC2eIQ" -i "https://a1.easemob.com/easemob-demo/testapp/messages" -d '{"target_type":"users","target":["user2","user3"],"msg":{"type":"cmd","action":"action1"},"from":"user1"}}'
可能返回的结果示例
返回值200,表示消息发送成功
{
"action" : "post",
"application" : "4d7e4ba0-dc4a-11e3-90d5-e1ffbaacdaf5",
"uri" : "https://a1.easemob.com/easemob-demo/testapp",
"entities" : [ ],
"data" : {
"user2" : "success",
"user3" : "success"
},
"timestamp" : 1415167842297,
"duration" : 4,
"organization" : "easemob-demo",
"applicationName" : "testapp"
}
返回值400,表示 massage 结构错误
{
"error": "json_parse",
"timestamp": 1543922465246,
"duration": 0,
"exception": "org.codehaus.jackson.JsonParseException",
"error_description": "Unexpected character ('}' (code 125)): was expecting double-quote to start field name\n at [Source: java.io.BufferedInputStream@7ba7eef2; line: 11, column: 2]"
}
返回值401,表示未授权[无token、token错误、token过期]
{
"error": "auth_bad_access_token",
"timestamp": 1543922590032,
"duration": 0,
"exception": "org.apache.usergrid.rest.exceptions.SecurityException",
"error_description": "Unable to authenticate due to corrupt access token"
}
如果返回结果是429、503或者其他5xx,有可能代表该接口被限流了,请稍微暂停一下并重试。详见接口限流说明
发送扩展消息
扩展消息:若普通消息类型不满足用户消息需求,可以使用扩展消息。任何类型的消息都支持扩展,主要是通过message的ext字段实现。
注意:在调用程序中,请求体如果超过 5kb 会导致413错误,需要拆成几个更小的请求体重试。详见接口限流说明。
HTTP Request
//{org_name}/{app_name}/messages |
---|
Request Headers
参数 | 说明 |
---|---|
Content-Type | application/json |
Authorization | Bearer ${token} |
Request Body
参数 | 说明 |
---|---|
target_type | 发送的目标类型;users:给用户发消息,chatgroups:给群发消息,chatrooms:给聊天室发消息 |
target | 发送的目标;注意这里需要用数组,并且向数组内添加的用户不能超过1000个,即使只有一个用户,也要用数组 ['u1'];给用户发送时数组元素是用户名,给群组发送时,数组元素是groupid |
msg | 消息内容 |
type | 消息类型,不局限与文本消息。任何消息类型都可以加扩展消息;txt:文本消息,img:图片消息,loc:位置消息,audio:语音消息,video:视频消息,file:文件消息 |
msg | 消息;随意传入都可以 |
from | 表示消息发送者;无此字段Server会默认设置为“from”:“admin”,有from字段但值为空串(“”)时请求失败 |
ext | 扩展属性,由APP自己定义。可以没有这个字段,但是如果有,值不能是“ext:null”这种形式,否则出错 |
attr1 | 消息的扩展内容,可以增加字段,扩展消息主要解析部分,必须是基本类型数据 |
Response Body
在返回值中查看data字段包含的信息
参数 | 说明 |
---|---|
username | 接受消息的用户名 |
success | 表示消息发送成功 |
请求示例
curl -X POST -H "Authorization:Bearer YWMtxc6K0L1aEeKf9LWFzT9xEAAAAT7MNR_9OcNq-GwPsKwj_TruuxZfFSC2eIQ" -i "https://a1.easemob.com/easemob-demo/testapp/messages" -d '{"target_type":"users","target":["user2","user3"],"msg":{"type":"txt","msg":"testmessage"},"from":"user1","ext":{"attr1":"test"}}'
可能返回的结果示例
返回值200,表示消息发送成功
{
"action" : "post",
"application" : "4d7e4ba0-dc4a-11e3-90d5-e1ffbaacdaf5",
"uri" : "https://a1.easemob.com/easemob-demo/testapp",
"entities" : [ ],
"data" : {
"user2" : "success",
"user3" : "success"
},
"timestamp" : 1415167842297,
"duration" : 4,
"organization" : "easemob-demo",
"applicationName" : "testapp"
}
返回值400,表示 massage 结构错误
{
"error": "json_parse",
"timestamp": 1543922465246,
"duration": 0,
"exception": "org.codehaus.jackson.JsonParseException",
"error_description": "Unexpected character ('}' (code 125)): was expecting double-quote to start field name\n at [Source: java.io.BufferedInputStream@7ba7eef2; line: 11, column: 2]"
}
返回值401,表示未授权[无token、token错误、token过期]
{
"error": "auth_bad_access_token",
"timestamp": 1543922590032,
"duration": 0,
"exception": "org.apache.usergrid.rest.exceptions.SecurityException",
"error_description": "Unable to authenticate due to corrupt access token"
}
如果返回结果是429、503或者其他5xx,有可能代表该接口被限流了,请稍微暂停一下并重试。详见接口限流说明
iOS扩展消息
环信提供以下几种扩展字段:
扩展字段 | 描述 |
---|---|
em_push_content | 自定义推送显示 |
em_push_category | 向 APNs Payload 中添加 category 字段 |
em_push_sound | 自定义推送提示音 |
em_push_mutable_content | 开启 APNs 通知扩展 |
em_ignore_notification | 发送静默消息 |
em_force_notification | 设置强制推送型 APNs |