# 编辑消息

调用该接口编辑已发送的消息内容，支持编辑文本、富文本消息。如需编辑卡片消息，请使用[更新应用发送的消息卡片](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/patch)接口。

## 前提条件

- 应用需要开启[机器人能力](https://open.feishu.cn/document/uAjLw4CM/ugTN1YjL4UTN24CO1UjN/trouble-shooting/how-to-enable-bot-ability)。  
- 编辑用户单聊内的消息时，用户需要在机器人的[可用范围](https://open.feishu.cn/document/home/introduction-to-scope-and-authorization/availability)内。
- 编辑群组内的消息时，机器人需要在该群组中，且拥有发言权限。

## 使用限制
- 一条消息最多可编辑 20 次。
- 仅可编辑当前操作者自己发送的消息。
- 不可编辑已撤回，已删除，超出可编辑时间的消息。可编辑时间由企业管理员设定，详情了解[管理员设置撤回和编辑消息权限](https://www.feishu.cn/hc/zh-CN/articles/325339752183)。

## 请求

基本 | &nbsp;
---|---
HTTP URL | https://open.feishu.cn/open-apis/im/v1/messages/:message_id
HTTP Method | PUT
接口频率限制 | [1000 次/分钟、50 次/秒](https://open.feishu.cn/document/ukTMukTMukTM/uUzN04SN3QjL1cDN)
支持的应用类型 | Custom App、Store App
权限要求<br>**调用该 API 所需的权限。开启其中任意一项权限即可调用**<br>开启任一权限即可 | 获取与发送单聊、群组消息(im:message)<br>以应用的身份发消息(im:message:send_as_bot)<br>更新消息(im:message:update)

### 请求头

名称 | 类型 | 必填 | 描述
---|---|---|---
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"

### 路径参数

名称 | 类型 | 描述
---|---|---
message_id | string | 待编辑的消息 ID，仅支持编辑文本（text）、富文本（post）消息。<br>ID 获取方式：<br>- 调用[发送消息](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/create)接口后，从响应结果的 `message_id` 参数获取。<br>- 监听[接收消息](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/events/receive)事件，当触发该事件后可以从事件体内获取消息的 `message_id`。<br>- 调用[获取会话历史消息](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/list)接口，从响应结果的 `message_id` 参数获取。<br>**示例值**："om_dc13264520392913993dd051dba21dcf"

### 请求体

名称 | 类型 | 必填 | 描述
---|---|---|---
msg_type | string | 是 | 消息类型。<br>**可选值有**：<br>- text：文本<br>- post：富文本<br>**示例值**："text"
content | string | 是 | 消息内容，JSON 结构序列化后的字符串。该参数的取值与 `msg_type` 对应，例如 `msg_type` 取值为 `text`，则该参数需要传入文本类型的内容。<br>**注意：**<br>- JSON字符串需进行转义，如换行符转义后为`\\n`<br>- 文本消息请求体最大不能超过 150 KB<br>- 富文本消息请求体最大不能超过 30 KB<br>- 如果消息中包含样式标签，会使实际消息体长度大于您输入的请求体长度。<br>了解不同类型的消息内容格式、使用限制，可参见[发送消息内容](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/im-v1/message/create_json)。<br>**示例值**："{\"text\":\"test content\"}"

### 请求体示例
```json
{
    "msg_type": "text",
    "content": "{\"text\":\"test content\"}"
}
```

## 响应

### 响应体

名称 | 类型 | 描述
---|---|---
code | int | 错误码，非 0 表示失败
msg | string | 错误描述
data | message | \-
message_id | string | 消息 ID。后续对消息的管理维护操作均需要使用该 ID。
root_id | string | 根消息 ID。在有多个回复的消息树中，`root_id` 为根消息的 `message_id`。关于 `root_id` 的更多说明，参见[消息管理概述](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/intro)。
parent_id | string | 父消息 ID。在有多个回复的消息树中，`parent_id` 为当前消息上一层的消息 `message_id`。如果是某一话题内的消息，则 `parent_id` 始终为话题内根消息的 `message_id`。关于 `parent_id` 的更多说明，参见[消息管理概述](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/intro)。
thread_id | string | 消息所属的话题 ID（不返回说明该消息不是话题形式的消息）。了解话题可参见[话题概述](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/thread-introduction)。
msg_type | string | 消息类型。<br>**可能值有**：<br>- text：文本<br>- post：富文本
create_time | string | 消息生成的时间戳。单位：毫秒
update_time | string | 消息更新的时间戳。单位：毫秒
deleted | boolean | 当前消息是否被撤回。编辑消息时只会返回 false，表示未被撤回。
updated | boolean | 当前消息是否被更新。编辑消息时只会返回 false，表示未被更新。
chat_id | string | 消息所属的群 ID。你可以调用[获取群信息](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/chat/get)接口，根据群 ID 获取群详情。
sender | sender | 消息发送者的信息。
id | string | 发送者的 ID。
id_type | string | 发送者的 ID 类型。<br>**可能值有：**<br>- `open_id`：表示发送者为用户，且返回的 ID 是用户的 open_id。<br>- `app_id`：表示发送者为应用，且返回的 ID 是应用的 app_id。
sender_type | string | 发送者的类型<br>**可能值有：**<br>- `user`: 用户<br>- `app`: 应用<br>- `anonymous`: 匿名<br>- `unknown`: 未知
tenant_key | string | 租户唯一标识。该标识用来识别租户，也可以用来获取租户访问凭证（tenant_access_token）。
body | message_body | 通过 `body` 内的 `content` 参数，返回当前的消息内容。
content | string | 消息内容，JSON 结构序列化后的字符串，不同消息类型（`msg_type`）对应不同内容。
mentions | mention\[\] | 消息内被 @ 的用户列表。
key | string | 被 @ 的用户序号。例如，第 3 个被 @ 到的成员，取值为 `@_user_3`。
id | string | 被 @ 的用户的 open_id。
id_type | string | 被 @ 的用户的 ID 类型，目前仅支持 `open_id` (了解[什么是 Open ID](https://open.feishu.cn/document/home/user-identity-introduction/open-id))。
name | string | 被 @ 的用户姓名。
tenant_key | string | 租户唯一标识。该标识用来识别被 @ 用户的租户，也可以用来获取租户访问凭证（tenant_access_token）。
upper_message_id | string | 合并转发消息中，上一层级的消息 ID，仅在合并转发场景会有返回值。了解 upper_message_id 可参见[消息管理概述](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/intro)。

### 响应体示例
```json
{
    "code": 0,
    "msg": "success",
    "data": {
        "message_id": "om_dc13264520392913993dd051dba21dcf",
        "root_id": "om_40eb06e7b84dc71c03e009ad3c754195",
        "parent_id": "om_d4be107c616aed9c1da8ed8068570a9f",
        "thread_id": "omt_d4be107c616a",
        "msg_type": "text",
        "create_time": "1609296809",
        "update_time": "1609336806",
        "deleted": false,
        "updated": true,
        "chat_id": "oc_5ad11d72b830411d72b836c20",
        "sender": {
            "id": "cli_9f427eec54ae901b",
            "id_type": "app_id",
            "sender_type": "app",
            "tenant_key": "736588c9260f175e"
        },
        "body": {
            "content": "{\"text\":\"@_user_1 test content\"}"
        },
        "mentions": [
            {
                "key": "@_user_1",
                "id": "ou_155184d1e73cbfb8973e5a9e698e74f2",
                "id_type": "open_id",
                "name": "Tom",
                "tenant_key": "736588c9260f175e"
            }
        ],
        "upper_message_id": "om_40eb06e7b84dc71c03e00ida3c754892"
    }
}
```

### 错误码

HTTP状态码 | 错误码 | 描述 | 排查建议
---|---|---|---
400 | 230001 | Your request contains an invalid request parameter. | 参数错误，可根据接口实际返回的错误信息，并参考接口文档内容，检查输入参数是否填写错误。
400 | 230002 | The bot can not be outside the group. | 机器人不在对应群组中。你需要将应用机器人添加到消息相应的群组中。如何添加机器人参考[机器人使用指南](https://open.feishu.cn/document/ukTMukTMukTM/uATM04CMxQjLwEDN)。
400 | 230006 | Bot ability is not activated. | 应用未启用机器人能力。启用方式参见[如何启用机器人能力](https://open.feishu.cn/document/uAjLw4CM/ugTN1YjL4UTN24CO1UjN/trouble-shooting/how-to-enable-bot-ability)。
400 | 230011 | The message is recalled. | 消息已被撤回，不支持该操作。
400 | 230013 | Bot has NO availability to this user. | 目标用户（以用户的 user_id/open_id/union_id/email 指定的消息接收者）或单聊用户（以群聊的 chat_id 指定的消息接收者，但 chat_id 对应的群聊类型为单聊 `p2p`）不在应用机器人的可用范围内，或者是在应用的禁用范围内。<br>**注意**：如果目标用户已离职，也会报错 230013。<br>解决方案：<br>1. 登录[开发者后台](https://open.feishu.cn/app)，找到并进入指定应用详情页。<br>2. 在左侧导航栏进入 **应用发布** >  **版本管理与发布** 页面，点击 **创建版本**。<br>3. 在 **版本详情** 页面，找到 **可用范围** 区域，点击 **编辑**。<br>4. 在弹出的对话框内，配置应用的可用范围，将用户添加至可用范围内。<br>5. 在页面底部点击 **保存**，并发布应用使配置生效。<br>6. （可选）如果以上配置完成后仍报错，则需要联系企业管理员登录[管理后台](https://feishu.cn/admin)，在 **工作台** > **应用管理** 中进入指定应用详情页，在 **应用可用范围** 内查看该用户是否被设置为了 **禁用成员**。<br>具体操作参见[配置应用可用范围](https://open.feishu.cn/document/home/introduction-to-scope-and-authorization/availability)。
400 | 230018 | These operations are NOT allowed at current group settings. | 当前操作被群设置禁止，例如群聊设置了仅指定成员可以在此群发言。请检查群设置或联系群管理员修改群设置。
400 | 230020 | This operation triggers the frequency limit. | 当前操作触发频率限制，请降低请求频率后重试。
400 | 230022 | The content of the message contains sensitive information. | 消息内容包含敏感信息，请修改消息内容后重试。
400 | 230025 | The length of the message content reaches its limit. | 消息体长度超出限制。文本消息最大不能超过 150 KB、卡片及富文本消息最大不能超过 30 KB。需要注意：<br>- 若使用卡片模板（template_id）发送消息，实际大小也包含模板对应的卡片数据大小。<br>- 若消息中包含大量样式标签，会使实际消息体长度大于传入的请求体长度。
400 | 230027 | Lack of necessary permissions. | 无权进行本次操作。可能的原因有：<br>1. 缺少相应权限，可根据实际的错误信息进行排查。<br>2. 未检查到用户授权信息。<br>3. 暂不支持在外部群中进行本操作。
400 | 230028 | The messages do NOT pass the audit. | 消息的数据防泄漏审查未通过。当消息内容包含有明文电话号码、明文邮箱地址等内容时可能会触发该错误。请根据接口返回的实际错误信息检查并修改消息内容。
400 | 230035 | Send message permission deny. | 没有发送消息的权限。请排查群聊是否已开启禁言、机器人是否被接收者屏蔽或受到租户维度的沟通权限管控。
400 | 230038 | Cross tenant p2p chat operate forbid. | 跨租户的单聊不允许通过本接口发送消息。
400 | 230050 | The message is invisible to the operator. | 消息对于当前的操作者不可见，无法进行本次操作。
400 | 230054 | This operation is not supported for this message type. | 该消息类型不支持本操作。具体说明参见[消息与群组部分API增加不支持的消息类型校验](https://open.feishu.cn/document/uAjLw4CM/ugTN1YjL4UTN24CO1UjN/breaking-change/unsupported-message-type-verification)。
400 | 230055 | The type of file upload does not match the type of message being sent. | 文件上传时选择的类型与发送的消息类型不匹配。例如，上传文件时，选择的文件格式为 MP4，则发送消息时消息类型需要为视频（media）。
400 | 230071 | The operator is not the sender of the message. | 当前操作者不是该消息的发送者，无法进行本次操作。
400 | 230072 | The message has reached the number of times it can be edited. | 消息已超过最大可编辑次数 20 次，无法继续编辑。
400 | 230073 | The message in the secret group is not allowed to be edited. | 密聊群中的消息暂不支持编辑。
400 | 230074 | The message in the third-party encryption group is not allowed to be edited. | 密盾聊中的消息暂不支持编辑。
400 | 230075 | The message has exceeded the time that can be edited. | 该消息已超出可编辑的时间，不能编辑。可编辑时间由企业管理员设定，详情了解[管理员设置撤回和编辑消息权限](https://www.feishu.cn/hc/zh-CN/articles/325339752183)。
400 | 230110 | Action unavailable as the message has been deleted. | 消息已被删除，无法操作。
400 | 232009 | Your request specifies a chat which has already been dissolved. | 消息所在的群组已被解散，无法操作。

更多错误码信息，参见[通用错误码](https://open.feishu.cn/document/ukTMukTMukTM/ugjM14COyUjL4ITN)。

