# 接收消息

## 事件描述

用户发送给机器人的消息时，会触发此事件。

## 订阅说明

| **属性** | **说明** |
| --- | --- |
| **事件主题** | **kso.app_chat.message** |
| **事件行为** | `create` |
| **业务域** | `-` |
| **权限要求** | 查询用户组 `kso.chat_message.readwrite` |
| **解密方式** | 详见 [解密算法](/app-integration-dev/wps365/server/event-subscription/security-verification) |


### 事件体（解密前）

<!-- 为了保持文档格式统一：（1）参数说明中请使用中文全角标点符号；（2）中英文间请添加空格 -->

| **名称**         | **参数类型** | **说明**                                          |
|:---------------|:---------|:------------------------------------------------|
| topic          | string   | 消息主题 `kso.app_chat.message`                                |
| operation      | string   | 消息变更动作 <br/>`create`：发送消息 |
| time           | integer  | 时间（秒为单位的时间戳）                                    |
| nonce          | string   | iv 向量（解密时使用）                                    |
| signature      | string   | 消息签名                                            |
| encrypted_data | string   | 消息变更的加密字段                                       |

### 事件体示例（解密前）

<!-- 为了保持文档格式统一，请使用 2 个空格作为 json 的缩进符 -->

```json
{
  "topic": "kso.app_chat.message",
  "operation": "create",
  "time": 1704074400,
  "nonce": "71***********7",
  "signature": "w6**********6Q",
  "encrypted_data": "B7**********iA=="
}
```

### 事件体数据（解密后）

<!-- 为了保持文档格式统一：（1）参数说明中请使用中文全角标点符号；（2）中英文间请添加空格 -->

| **名称**   | **参数类型** | **是否必带** | **说明**                                                                                                                       |
|----------|----------|--------------|------------------------------------------------------------------------------------------------------------------------------|
| chat     | object   | 是           | 会话信息                                                                                                                         |
| ∟ id     | string   | 是           | 会话 id                                                                                                                        |
| ∟ type   | string   | 是           | 会话类型：`p2p`（单聊），`group`（群聊）                                                                                                   |
| company_id | string   | 是           | 企业 id                                                                                                                        |
| message | object   | 是           | 消息                                                                                                                           |
| ∟ id     | string   | 是           | 消息 id                                                                                                                        |
| ∟ type   | string   | 是           | 消息类型，参考 [发送消息接口文档](https://365.kdocs.cn/3rd/open/documents/app-integration-dev/wps365/server/im/message/create-msg.html) 中 `type`   |
| ∟ content | object   | 是           | 消息体，参考 [发送消息接口文档](https://365.kdocs.cn/3rd/open/documents/app-integration-dev/wps365/server/im/message/create-msg.html) 中 `content` |
| send_time | string   | 是           | 消息发送时间戳                                                                                                                      |
| sender | object   | 是           | 消息发送者                                                                                                                        |
| ∟ id     | string   | 是           | 用户 id                                                                                                                        |
| ∟ type   | string   | 是           | 用户类型                                                                                                                         |

### 事件体数据示例（解密后）

<!-- 为了保持文档格式统一，请使用 2 个空格作为 json 的缩进符 -->

- 给机器人发送消息
```json
{
  "chat": {
    "id": "8225957",
    "type": "p2p"
  },
  "company_id": "eueue",
  "message": {
    "content": {
      "text": {
        "content": "666"
      }
    },
    "id": "w7i5fgFzh6uRtOTN",
    "type": "text"
  },
  "send_time": 1734426254026,
  "sender": {
    "id": "erueru",
    "type": "user"
  }
}
```
- 群聊@机器人
```json
{
  "chat": {
    "id": "8205341",
    "type": "group"
  },
  "company_id": "uduu",
  "message": {
    "content": {
      "text": {
        "content": "\u003cat id=\"1\"\u003eNews\u003c/at\u003e 12323[**]"
      }
    },
    "id": "B2iofLuEfxCmhOfg",
    "mentions": [
      {
        "id": "1",
        "identity": {
          "id": "AK20241127SUGTFW",
          "name": "News",
          "type": "app"
        },
        "type": "user"
      }
    ],
    "type": "text"
  },
  "send_time": 1734943955071,
  "sender": {
    "id": "dududu",
    "type": "user"
  }
}
```
- 发送图片
```json
{
  "chat": {
    "id": "8225957",
    "type": "p2p"
  },
  "company_id": "eyeyey",
  "message": {
    "content": {
      "image": {
        "height": 433,
        "name": "xiezuodomain20241223-162623.png",
        "size": 35092,
        "storage_key": "DD1****************************************bWc=",
        "type": "image/png",
        "width": 782
      }
    },
    "id": "B2iofLuEfdF5t5uX",
    "type": "image"
  },
  "send_time": 1734942384583,
  "sender": {
    "id": "eyeyey",
    "type": "user"
  }
}
```
- 发送本地文件
```json
{
  "chat": {
    "id": "8225957",
    "type": "p2p"
  },
  "company_id": "euyeuyeye",
  "message": {
    "content": {
      "file": {
        "local": {
          "name": "**.p12",
          "size": 3483,
          "storage_key": "DD1AFB29YmV0YS8zMWQ1ZDQ5NDQxNDFkZDJlNTNmOGNkNzQwYzIyZWU5ZDprczM6a29hLWltZw=="
        },
        "type": "local"
      }
    },
    "id": "kYi1fEu6fxuOf2t8",
    "type": "file"
  },
  "send_time": 1734942995610,
  "sender": {
    "id": "fuerureu",
    "type": "user"
  }
}
```
- 发送云文档
```json
{
  "chat": {
    "id": "8225957",
    "type": "p2p"
  },
  "company_id": "eyeeye",
  "message": {
    "content": {
      "file": {
        "cloud": {
          "id": "hCDZ*******************nT1h",
          "link_id": "cohiPYx868pl",
          "link_url": "https://kdocs.cn/l/cohiPYx868pl"
        },
        "type": "cloud"
      }
    },
    "id": "kYi1fEu6fxuwhETb",
    "type": "file"
  },
  "send_time": 1734943103950,
  "sender": {
    "id": "eeeeer",
    "type": "user"
  }
}
```
- 发送图文消息（会转成富文本消息）
```json
{
  "chat": {
    "id": "8225957",
    "type": "p2p"
  },
  "company_id": "duddu",
  "message": {
    "content": {
      "rich_text": {
        "elements": [
          {
            "alt_text": "",
            "elements": [
              {
                "alt_text": "",
                "indent": 0,
                "index": 0,
                "text_content": {
                  "content": "1"
                },
                "type": "text"
              }
            ],
            "indent": 0,
            "index": 0,
            "type": "nl"
          },
          {
            "alt_text": "",
            "elements": [
              {
                "alt_text": "",
                "indent": 0,
                "index": 0,
                "text_content": {
                  "content": "2"
                },
                "type": "text"
              }
            ],
            "indent": 0,
            "index": 0,
            "type": "nl"
          },
          {
            "alt_text": "",
            "elements": [
              {
                "alt_text": "",
                "image_content": {
                  "height": 433,
                  "name": "d7a87aa182412f26fbdb0adc1e57d4b7.png",
                  "size": 35092,
                  "storage_key": "DD1AFB***************************************9hLWltZw==",
                  "type": "image/png",
                  "width": 782
                },
                "indent": 0,
                "index": 0,
                "type": "image"
              }
            ],
            "indent": 0,
            "index": 0,
            "type": "nl"
          },
          {
            "alt_text": "",
            "elements": [
              {
                "alt_text": "",
                "indent": 0,
                "index": 0,
                "text_content": {
                  "content": "[**][**]"
                },
                "type": "text"
              }
            ],
            "indent": 0,
            "index": 0,
            "type": "nl"
          }
        ]
      }
    },
    "id": "jji2fZuLfguQiZSp",
    "type": "rich_text"
  },
  "send_time": 1734943195838,
  "sender": {
    "id": "yeyweey",
    "type": "user"
  }
}
```
- 发送富文本消息
```json
{
  "chat": {
    "id": "erer",
    "type": "p2p"
  },
  "company_id": "dudu",
  "message": {
    "content": {
      "rich_text": {
        "elements": [
          {
            "alt_text": "",
            "elements": [
              {
                "alt_text": "",
                "indent": 0,
                "index": 0,
                "style_text_content": {
                  "style": {
                    "bold": true,
                    "color": "",
                    "italic": false
                  },
                  "text": "2234"
                },
                "type": "text"
              }
            ],
            "indent": 0,
            "index": 0,
            "type": "nl"
          },
          {
            "alt_text": "",
            "elements": [
              {
                "alt_text": "",
                "indent": 0,
                "index": 0,
                "style_text_content": {
                  "style": {
                    "bold": false,
                    "color": "",
                    "italic": false
                  },
                  "text": "1"
                },
                "type": "text"
              }
            ],
            "indent": 0,
            "index": 0,
            "type": "ol"
          },
          {
            "alt_text": "",
            "elements": [
              {
                "alt_text": "",
                "indent": 0,
                "index": 0,
                "style_text_content": {
                  "style": {
                    "bold": false,
                    "color": "",
                    "italic": false
                  },
                  "text": "2"
                },
                "type": "text"
              }
            ],
            "indent": 0,
            "index": 1,
            "type": "ol"
          },
          {
            "alt_text": "",
            "elements": [
              {
                "alt_text": "",
                "indent": 0,
                "index": 0,
                "style_text_content": {
                  "style": {
                    "bold": false,
                    "color": "",
                    "italic": false
                  },
                  "text": "3"
                },
                "type": "text"
              }
            ],
            "indent": 0,
            "index": 2,
            "type": "ol"
          },
          {
            "alt_text": "",
            "elements": [
              {
                "alt_text": "",
                "indent": 0,
                "index": 0,
                "style_text_content": {
                  "style": {
                    "bold": false,
                    "color": "",
                    "italic": false
                  },
                  "text": "lol"
                },
                "type": "text"
              }
            ],
            "indent": 0,
            "index": 0,
            "type": "ul"
          },
          {
            "alt_text": "",
            "elements": [
              {
                "alt_text": "",
                "indent": 0,
                "index": 0,
                "style_text_content": {
                  "style": {
                    "bold": false,
                    "color": "",
                    "italic": false
                  },
                  "text": "wangzhe"
                },
                "type": "text"
              }
            ],
            "indent": 0,
            "index": 0,
            "type": "ul"
          },
          {
            "alt_text": "",
            "elements": [
              {
                "alt_text": "",
                "image_content": {
                  "height": 433,
                  "name": "d7a87aa182412f26fbdb0adc1e57d4b7.png",
                  "size": 35092,
                  "storage_key": "DD1AFB*******************************9hLWltZw==",
                  "type": "image/png",
                  "width": 782
                },
                "indent": 0,
                "index": 0,
                "type": "image"
              }
            ],
            "indent": 0,
            "index": 0,
            "type": "nl"
          },
          {
            "alt_text": "",
            "elements": [
              {
                "alt_text": "",
                "indent": 0,
                "index": 0,
                "text_content": {
                  "content": "[**]"
                },
                "type": "emoji"
              },
              {
                "alt_text": "",
                "indent": 0,
                "index": 0,
                "text_content": {
                  "content": "[**]"
                },
                "type": "emoji"
              }
            ],
            "indent": 0,
            "index": 0,
            "type": "nl"
          }
        ]
      }
    },
    "id": "9ViOfWukfZClFBfb",
    "type": "rich_text"
  },
  "send_time": 1734943548129,
  "sender": {
    "id": "euyreur",
    "type": "user"
  }
}
```