API 参考
在线调试

发送消息

更新时间: 2024/07/23 10:32:59

网易云信服务端支持发送单聊消息和群聊消息,消息类型包括文本消息、图片消息、语音消息、视频消息、地理位置消息、文件消息、提示消息和自定义消息。

功能描述

  • 各类消息及相关功能的更多介绍,请参考 消息概述

  • 该接口可以在会话中发送一条单聊/群聊消息。会话类型包括单聊、群聊、超大群会话。聊天室消息发送请参考 发送聊天室消息

调用频率

单个应用默认最高调用频率请参考 频控说明

请求

URL

POST https://open.yunxinapi.com/im/v2/conversations/:{conversation_id}/messages

请求 Header 的参数说明请参考 请求 Header

路径参数

参数名称 类型 是否必选 说明 示例
conversation_id String 必选 会话 ID。会话 ID 需要用户自行拼装,拼装规则:
owner_id | conversation_type | other_idowner_id | conversation_type | team_id
  • owner_id:操作者账号 ID
  • conversation_type:会话类型,1:单聊会话。2:高级群会话。3:超大群会话
  • other_id:对方账号 ID(单聊对话)
  • team_id:群组 ID(群组会话)
单聊会话:Accid1 | 1 | Accid2
高级群会话:Accid1 | 2 | tid
超大群会话:Accid1 | 3 | tid

请求体参数

参数名称 类型 是否必选 描述
message Object 必选 消息体。
message_config Object 可选 消息配置项。
route_config Object 可选 抄送相关配置项。
push_config Object 可选 推送相关配置项。
antispam_config Object 可选 安全通相关配置项。
p2p_option Object 可选 单聊消息功能配置项。
team_option Object 可选 高级群消息功能配置项。
superteam_option Object 可选 超大群消息功能配置项。
robot_config Object 可选 机器人功能配置项。
ai_params Object 可选 数字人功能配置项。
sender_no_sense Boolean 可选 发送方是否无感知,默认为 false。若设置为无感知(true),则消息发送者无该消息的多端、漫游、历史记录等信息。
查询历史消息时可设置是否在历史记录查询结果中包含无感知消息,具体参考 分页查询历史消息发送者和接收者必须有一方感知,即不能将两者都设置为无感知。
receiver_no_sense Boolean 可选 接收方是否无感知,默认为 false。若设置为无感知(true),则消息接收者无该消息的多端、漫游、历史记录等信息。
查询历史消息时可设置是否在历史记录查询结果中包含无感知消息,具体参考 分页查询历史消息
extension String 可选 开发者扩展字段,JSON 格式,长度上限为 1024 位字符。例如:"{"k":"v"}"

请求体示例

json{
    "sender_no_sense": false,
    "receiver_no_sense": false,
    "extension": """message": {
        "type": 0,
        "sub_type": 0,
        "text": "hahaha",
        "attachment": {
          "name":"图片发送于2015-05-07 13:59",
          "md5":"9894907e4ad9de4678091277509361f7",
          "url":"http://nimtest.nos.netease.com/cbc500e8-e19c-4b0f-834b-c32d4dc1075e",
          "ext":"jpg",
          "w":6814,
          "h":2332,
          "size":388245
        }
    },
    "message_config": {
        "unread_enabled": true,
        "mutil_sync_enabled": true,
        "offline_enabled": true,
        "history_enabled": true,
        "roaming_enabled": true,
        "conversation_update_enabled": true
    },
    "route_config": {
        "route_enabled": true,
        "route_environment": ""
    },
    "push_config": {
        "push_enabled": false,
        "push_nick_enabled": false,
        "push_content": "",
        "push_payload": "",
        "push_forcepush_all": false,
        "push_forcepush_ids": [
            ""
        ],
        "push_forcepush_content": ""
    },
    "antispam_config": {
        "antispam_enabled": true,
        "antispam_business_id": "",
        "antispam_extension": "",
        "antispam_custom_message_enabled": false,
        "antispam_custom_message": "",
        "antispam_cheating": ""
    },
    "p2p_option": {
        "check_friend": false
    },
    "team_option": {
        "mark_as_read": false
    },
    "superteam_option": {
        "check_mute": false
    },
    "robot_config": {
        "robot_account_id": "qweqw",
        "robot_topic": "",
        "robot_function": "",
        "robot_custom_content": ""
    }
}

响应

Header

响应 Header 的参数说明请参考 响应 Header

响应体参数

参数名称 类型 说明 是否必返回
code Integer 状态码,200 表示请求成功。
msg String 提示信息。请求失败时返回错误信息,请求成功时返回 "success"。
data Object 返回的 JSON 数据对象,请求失败则返回空对象。

响应体示例

json{
    "code": 200,
    "msg": "success",
    "data": {
        "message_type": 0,
        "text": "msg",
        "attachment": ["name", "image"],
        "sub_type": 1,
        "sender_id": "wqt",
        "conversation_type": 1,
        "receiver_id": "apiv2test"
    }
}

错误码

错误码 错误码描述 错误信息示例
200 请求成功 success
414 参数错误 parameter error
102404 用户不存在 account not exist
102421 用户被禁言 account chat banned
102422 用户被禁用 account banned
104404 好友不存在 friend not exist
107410 该 App 未开启发消息功能 messaging function disabled
107451 该消息未通过反垃圾审核 message hit antispam
107302 由于群成员过多导致标记已读失败,消息发送失败 sending message failed for marking message read failed for too many team members
107329 不是机器人帐号 %s is not chatbot account
107330 不允许发送方和接收方均无感知 sender or receiver must sense message
108311 超大群服务未开通 super team service disabled
108404 群不存在 team not exist
108302 非高级群 not advanced team
108423 群全体禁言 all team members chat banned
108306 群普通成员禁言 team normal member chat banned
109404 群成员不存在 team member not exist
109424 群成员被禁言 team member chat banned
500 服务器内部错误 internal server error

消息格式示例

文本消息(type = 0)

text 字段必填,attachment 字段无效。

{
  "msg":  "您好" //消息内容
}

图片消息(type = 1)

text 字段选填,attachment 字段必填。

json{
  "name": "图片发送于 2015-05-07 13:59",   // 图片 name
  "md5": "9894907e4ad9de4678091277509361f7",    // 图片文件 md5,按照字节流加密
  "url": "http://nimtest.nos.netease.com/cbc500e8-e19c-4b0f-834b-c32d4dc1075e",    // 生成的 url
  "ext": "jpg",    // 图片后缀
  "w": 6814,    // 宽,单位为像素
  "h": 2332,    // 高,单位为像素
  "size": 388245    // 图片文件大小,单位为字节(Byte)
}

语音消息(type = 2)

text 字段选填,attachment 字段必填。

json{
  "dur": 4551, // 语音持续时长 ms
  "md5": "87b94a090dec5c58f242b7132a530a01", // 语音文件的 md5 值,按照字节流加密
  "url": "http://nimtest.nos.netease.com/a2583322-413d-4653-9a70-9cabdfc7f5f9", // 生成的 url
  "ext": "aac", // 语音消息格式
  "size": 16420 // 语音文件大小,单位为字节(Byte)
}

视频消息(type = 3)

text 字段选填,attachment 字段必填。

json{
  "dur": 8003,
  "md5": "da2cef3e5663ee9c3547ef5d127f7e3e",
  "url": "http://nimtest.nos.netease.com/21f34447-e9ac-4871-91ad-d9f03af20412",
  "w": 360,
  "h": 480,
  "ext": "mp4",
  "size": 16420
}

地理位置消息(type = 4)

text 字段选填,attachment 字段必填。

json{
  "title": "中国浙江省杭州市网商路 599 号", // 地理位置 title
  "lng": 120.1908686708565, // 经度
  "lat": 30.18704515647036 // 纬度
}

文件消息(type = 6)

text 字段选填,attachment 字段必填。

json{
  "name": "BlizzardReg.ttf",    // 文件名
  "md5": "79d62a35fa3d34c367b20c66afc2a500", // 文件 MD5,按照字节流加密
  "url": "http://nimtest.nos.netease.com/08c9859d-183f-4daa-9904-d6cacb51c95b", // 文件 URL
  "ext": "ttf",    // 文件后缀类型
  "size": 91680    // 大小,单位为字节(Byte)
}

提示消息(type = 10)

text 字段必填,attachment 字段无效。

json{
  "msg":  "您收到一份礼物"
}

自定义消息(type = 100)

text 字段选填,attachment 字段必填。

json//开发者自定义的 body,JSON 格式
{
  "myKey":myValue
}
此文档是否对你有帮助?
有帮助
去反馈
  • 功能描述
  • 调用频率
  • 请求
  • URL
  • Header
  • 路径参数
  • 请求体参数
  • 请求体示例
  • 响应
  • Header
  • 响应体参数
  • 响应体示例
  • 错误码
  • 消息格式示例
  • 文本消息(type = 0)
  • 图片消息(type = 1)
  • 语音消息(type = 2)
  • 视频消息(type = 3)
  • 地理位置消息(type = 4)
  • 文件消息(type = 6)
  • 提示消息(type = 10)
  • 自定义消息(type = 100)