# 发送文本消息

**注意事项**：为了更好地提升该接口的安全性，我们对其进行了升级，请尽快迁移至[新版本>>](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/create)

给指定用户或者会话发送文本消息，其中会话包括私聊会话和群会话。

- 需要启用机器人能力；私聊会话时机器人需要拥有对用户的可见性，群会话需要机器人在群里

## 请求

基本 | &nbsp;
---|---
HTTP URL | https://open.feishu.cn/open-apis/message/v4/send/
HTTP Method | POST
支持的应用类型 | Custom App、Store App
权限要求<br>**调用该 API 所需的权限。开启其中任意一项权限即可调用** | 以应用的身份发消息(im:message:send_as_bot)

### 请求头

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

### 请求体
参数|类型|必填/选填|说明|默认值|实例  
--|--|--|--|--|--  
open_id <br>user_id <br> email <br> chat_id  | string | 必填 | 给用户发私聊消息，只需要填 open_id([什么是 Open ID？](https://open.feishu.cn/document/home/user-identity-introduction/open-id))、email（真实邮箱）、user_id([什么是 User ID？](https://open.feishu.cn/document/home/user-identity-introduction/user-id)) 中的一个即可，向群里发消息使用群的 chat_id。服务端依次读取字段的顺序为 chat_id > open_id > user_id > email   ( user_id 对应V3接口的 employee_id , chat_id 对应V3的 open_chat_id )||ou_5ad573a6411d72b8305fda3a9c15c70e|
root_id | string | 选填 | 如果需要回复某条消息，填对应消息的消息 ID||om_40eb06e7b84dc71c03e009ad3c754195|
msg_type | string | 必填 | 消息类型，此处固定填 "text"||text|
content | object | 必填 | 消息内容|-|-|
&emsp;∟text | string | 必填 | 文本消息内容，文本消息中可以 at 个人或全体成员<br>at 全体成员：<at user_id="all">  </at> <br> at 个人：<at user_id="ou_xxxxxxx"></at>，user_id 为用户 user_id或者open_id ||text content<at user_id=\"ou_88a56e7e8e9f680b682f6905cc09098e\">test</at>|

### 请求体示例

```json
{
   "open_id":"ou_5ad573a6411d72b8305fda3a9c15c70e", 
   "root_id":"om_40eb06e7b84dc71c03e009ad3c754195",
   "chat_id":"oc_5ad11d72b830411d72b836c20", 
   "user_id": "92e39a99",
   "email":"fanlv@gmail.com", 
   "msg_type":"text",
   "content":{
        "text":"text content<at user_id=\"ou_88a56e7e8e9f680b682f6905cc09098e\">test</at>"
    }
}
```
@单个用户时，user_id填open\_id，必须是有效值，否则取名字展示，没有@效果；@所有人填"all"，@所有人必须满足所在群开启@所有人功能。

### Curl 请求 Demo
```json 
curl -X POST \
  https://open.feishu.cn/open-apis/message/v4/send/ \
  -H 'Authorization: Bearer t-fee42159a366c575f2cd2b2acde2ed1e94c89d5f' \
  -H 'Content-Type: application/json' \
  -d '{
    "chat_id": "oc_f5b1a7eb27ae2c7b6adc2a74faf339ff",
    "msg_type": "text",
    "content": {
        "text": "text content<at user_id=\"ou_88a56e7e8e9f680b682f6905cc09098e\">test</at>"
    }
}'
```
## 响应
### 响应体
|参数|类型|说明|
|-|-|-|
|code|int|返回码，非 0 表示失败|
|msg|string|返回码描述|
data | - | -
&emsp;∟message_id |string| 消息 ID

### 响应体示例
```json
{
    "code": 0,
    "msg": "ok",
    "data":{
       "message_id": "om_92eb70a7120ga8c3ca56a12ee9ba7ca2"
    }
}
```

### 错误码

具体可参考：[服务端错误码说明](https://open.feishu.cn/document/ukTMukTMukTM/ugjM14COyUjL4ITN)

