# 步骤四：发送消息卡片

在本步骤，你将调用发送消息接口，发送卡片消息到群聊。

## 步骤一：发送消息

1. 登录[API调试台](https://open.feishu.cn/api-explorer)。

2. 在左上角单击**切换应用**，然后在弹出的对话框中选择步骤一创建的应用，最后单击**确定**完成切换。

![image.png](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/4d5a7e7de83436ba022781e8bda13546_Od8BpTsWRf.png?height=1870&lazyload=true&maxWidth=450&width=2716)

3. 在左侧查看鉴权凭证栏，单击获取 **tenant_access_token** 和 **user_access_token** 。

如下图所示，在获取 **user_access_token** 时，你需要将API调试台添加为当前应用的可信的重定向地址。

![image.png](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/f483e923413fee36368919d635f51785_Sqv5Dn1e6s.png?height=1304&lazyload=true&maxWidth=450&width=2548)

4. 调用 [通过手机号或邮箱获取用户 ID](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/contact-v3/user/batch_get_id) 接口，获取用户的open_id。

1. 在API列表中依次选择 **通讯录** > **用户** > **通过手机号或邮箱获取用户ID** 。

2. 单击 **权限配置**，然后选中未开通的权限，单击 **批量开通**。

![image.png](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/ee9811872d8452dbd17f0f40b5ff4ea8_Nz0qd7L4QS.png?height=1368&lazyload=true&maxWidth=450&width=2656)

3. 单击 **查询参数**，修改 **user_id_type**为 `open_id`。

4. 单击 **请求体**，在请求 JSON 中填入以下内容并修改对应的参数信息，最后单击右上角 **开始调试**。

需要修改`mobiles`的值为你的手机号。

```json
      {
        "mobiles": [
          "180xxx120" 
        ]
      }
      ```

5. 调用成功后记录下返回结果中 **user_id** 的值，该值就是用户的 **open_id**。

后续步骤中将使用该值作为群组的      **owner_id**      。

![image.png](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/96822641e767bec666cc1fc2df280f09_K7SifGkF0u.png?height=1288&lazyload=true&maxWidth=450&width=1912)

5. 调用[创建群](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/chat/create)接口，创建一个用于测试的群聊。

1. 在API列表中依次选择 **群组** > **群组管理** > **创建群**。

2. 单击 **权限配置**，然后选中未开通的权限，单击 **批量开通**。

![image.png](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/2c63541f6a99308890cad1131d3af9e9_SfOhKS5ZXp.png?height=1230&lazyload=true&maxWidth=450&width=1924)

3. 单击 **查询参数**，然后修改 **set_bot_manager** 参数为 `true`。

4. 单击**请求体**，在请求 JSON 中填入以下内容。

* 修改 **name** 的值为你的群聊名称。

* 修改 **owner_id** 的值为**步骤4.e**获取的用户 **open_id**。

```json
      {
        "name": "测试群名称",     // 群聊名称
        "owner_id":"ou_00bdxxx5e68"  // 群主的open_id
      }
      ```

5. 调用成功后记录下群的 **chat_id**。

![image.png](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/75069601fc794fd2879cdc4be85f4d2b_rg0p8axRha.png?height=1290&lazyload=true&maxWidth=450&width=1918)

6. 参考以下步骤，调用[发送消息](https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/im-v1/message/create)接口，发送卡片消息到群。

1. 在API列表中依次选择 **消息** > **消息管理** > **发送消息**。

2. 单击 **查询参数**，修改 **receive_id_type** 参数为 `chat_id`。

3. 单击 **请求体**，在请求 JSON 中填入以下内容，并修改参数值为实际参数值。单击右上角 **发起调用**。

```JSON
      {
        "receive_id": "oc_7bc568xxx24e",   // 群chat_id
        "msg_type": "interactive", // 消息类型，interactive 为消息卡片类型
        // template_id 为消息卡片 ID。
        "content": "{\"type\": \"template\", \"data\": {\"template_id\": \"ctp_AAxxxxxxxxxx\"}}"
      }
      ```

调用成功后的结果类似如下：

![image.png](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/270c50f556dc6f45c433c0acc180c322_YxDAKemlBL.png?height=1748&lazyload=true&maxWidth=450&width=2624)

## 步骤二：验证
1. 打开飞书客户端，找到上一步中创建的群，单击群名称打开聊天窗口。

如下图所示，卡片消息已经成功发送到群聊中。

![image.png](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/f4bdacf20284bd5fc85d967865b0e1fe_60nbCgkqcT.png?height=1510&lazyload=true&maxWidth=450&width=1676)

2. 依次单击下方三个按钮，查看对应功能是否符合预期。

* 跳转按钮效果如下：

![1.1.gif](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/f43d9166f0898a3edd5569f1b753cbbf_1rEhBK6BMz.gif?height=1230&lazyload=true&maxWidth=450&width=1686)

* 二次确认效果如下：

![1.2.gif](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/5e2c17292739024ccf10137862248d5b_6RHH25KCYm.gif?height=1232&lazyload=true&maxWidth=450&width=1684)

* 回传交互效果如下：

![1.3.gif](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/5b7910e82966b459ebad935a98574c6b_ME93VqHXdS.gif?height=1232&lazyload=true&maxWidth=450&width=1684)

点击回传交互按钮后，你的服务端将收到包含以下请求体的 POST 请求。

```JSON
      {
          "open_message_id": "om_a7acxxxc58e4",
          "tenant_key": "109xxx758",
          "open_id": "ou_d72axxx5454622e2e",
          "user_id": "4bxxx9f8",
          "action": {
              "value": {
                  "key": "value",
                  "key1": "value1"
              },
              "tag": "button"
          },
          "open_chat_id": "oc_43aaaxxxe6bcd",
          "token": "c-cfffac0129xxx3eb3f8f5"
      }
      ```

如果需要对用户操作进行反馈，可参考[步骤五：响应用户操作](https://open.feishu.cn/document/home/build-a-beautiful-message-card-in-5-minutes/feedback-on-user-behavior)。

