# Sending Feishu Cards with a Custom Bot

This document guides you on how to use a custom bot added to a Feishu group chat to send cards.

## Explanation on binding apps or custom bots

After adding an application or custom bot to a card template, you can use that application or custom bot to send the card template. The differences between adding an application and a custom bot are as follows:
- **Adding a specific application**: After adding an application, it can push messages to specified members via the send message API. Card templates sent by applications support interactive capabilities, allowing enterprise members to submit data to the developer's server.
- **Adding a custom bot**: After binding a custom bot, it can push card template messages to group chats via webhook. Card templates sent by custom bots only support static content, i.e., they only support link redirection and do not support submitting data to the developer's server via callback interactions.

## Demonstration effect

Following the steps in this document, you will ultimately receive a Feishu card message sent by a custom bot in a Feishu group chat.

![image.png](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/672c6fc3bfb7559435910e09196f21fe_IEpu0XyTLF.png?height=957&lazyload=true&maxWidth=500&width=1320)

## Operational steps

### Step One: Create a custom bot for the group

1. Log in to the Feishu client (PC).

2. Select a target group and click **Bots** in the group settings.

![](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/8169085c4bf399f0a467c4a5995a4706_Q6nI82ZI5z.png?height=1532&lazyload=true&maxWidth=600&width=1798)

3. Click **Add Bot** and select **Custom Bot**.

![](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/1beb0c9b05e8768e324e8723f7f35646_5mHzBwqpX6.png?height=1516&lazyload=true&maxWidth=600&width=2714)

4. Confirm the bot's avatar, name, and description, then click **Add**.

![](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/f24fcaa4e3b87bd4b9493b53c3b6d72b_CQrxY2VfiY.png?height=1148&lazyload=true&maxWidth=600&width=1664)

5. After adding, properly save the webhook URL of the bot and click **Finish**.warning    
Further requests need to be sent to the webhook URL of the bot to send message cards. Therefore, you need to save this webhook, ensure data security, and prevent others from maliciously calling the bot. 

![](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/5eef67a22d4c6b79418b3fa6fa295093_Afwh6YstGi.png?height=1140&lazyload=true&maxWidth=600&width=1668)

### Step Two: Build and Publish a Card Template

1. Log in to [Feishu Card Building Tool](https://open.feishu.cn/cardkit?from=open_docs_send_cards_with_bot).
1. On the **My Cards** page, click **Sample Library** > **Recent approval efficiency overview**.

![image.png](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/7260f812bace6ecee1275321bd24ac6c_gAwKzOEdQA.png?height=826&lazyload=true&maxWidth=500&width=1204)

1. In the **Create from sample** dialog, set the **Card Name** to `CardDemo`, then click **Create**.
1. On the card template editing page, click the application icon.

![](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/04fff9e7c4b1c10239a212100920f154_5nMiveVNbI.png?height=403&lazyload=true&maxWidth=500&width=1240)
1. In the **Add Custom Bot/Application** popup, select add custom bot.

![image.png](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/3a8de4a632ef40ca2590c4faad194b81_7ukbVXuVzy.png?height=262&lazyload=true&width=586)

1. Select **Specific Custom Bot**, fill in the webhook address you obtained in Step One to grant this bot the permissions to send the card template.

1. In the building tool top menu bar, click **Save**.
1. In the top right corner, click **Send me Preview**, and you will receive the data report card sent by the developer assistant.

![image.png](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/356f4d0772c9474ec6833f6d1b212019_P0Dz9h5pXh.png?height=110&lazyload=true&maxWidth=500&width=495)

1. After the preview is confirmed to be correct, return to the card editing page, click **Publish** in the top right corner.
1. In the **Publish Card** dialog, set the **Card Version Number**, and click **Publish**. Typically, set the version number to `1.0.0` for the first time publishing a card.

1. Obtain the card version number and card ID for use in Step Three to send the card.

Get the card ID from the building tool's top menu bar, as shown below.

![image.png](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/aaf163e9eb92edb8c9c04d8f92ab2c1e_KxhoLa5dQl.png?height=296&lazyload=true&maxWidth=500&width=546)

The card version number is the version number you manually set when publishing the card. If you need to view historical versions of the card and their corresponding version numbers, you can click **Version Management** in the building tool's top menu bar to view.

![image.png](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/4f645b8be990e9ee9ba5267818e0d123_Av5eZQWnsA.png?height=255&lazyload=true&maxWidth=500&width=1845)

### Step Three: Request the Group Custom Bot to Send a Feishu Card

Locally, send an HTTP POST request to the custom bot's webhook address using curl command. You can base this on the example code and explanations provided, modify the example values to actual values, and depending on your system environment, compress or escape the JSON code. If you are using card variables, you need to customize the variable values here.

- If your operating system is **macOS**, please open the Terminal locally, refer to the code explanation in the table below to modify the sample code, and then run it locally:

```bash
        curl -X POST -H "Content-Type: application/json" \
        -d '{"msg_type":"interactive","card":{"type":"template","data":{"template_id":"AAqyBQVmUNxxx","template_version_name":"1.0.0"}}}' \
        https://open.feishu.cn/open-apis/bot/v2/hook/f99fed8d-9b01-4dfe-ab56-xxxx
        ```

![img_v3_02h8_822aa8df-a169-4ab6-a0b9-fa90ba5d67cg.png](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/1dd44d829a7c1d18cea5076ce9b01f78_HqWYg8A0nf.png?height=244&lazyload=true&maxWidth=650&width=1464)

-  If your operating system is **Windows**, you can open the Command Prompt (cmd) tool locally, refer to the code explanation in the table below to modify the sample code, and then run it locally. Please pay attention to JSON escaping when using it:

```bash
        curl -X POST -H "Content-Type: application/json" -d "{\"msg_type\":\"interactive\",\"card\":{\"type\":\"template\",\"data\":{\"template_id\":\"AAqyBQVmUNxxx\",\"template_version_name\":\"1.0.0\"}}}" "https://open.feishu.cn/open-apis/bot/v2/hook/f99fed8d-9b01-4dfe-ab56-xxxx"
        ```

![image.png](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/ac332c21bdaf8e18c98d8b170de9fd56_ysXXeQr3xd.png?height=382&lazyload=true&maxWidth=650&width=1236)  

- If your operating system is **Windows**, you can also open the Windows PowerShell tool locally, refer to the code explanation in the table below to modify the sample code, and then run it locally:

```bash
      Invoke-RestMethod -Uri "https://open.feishu.cn/open-apis/bot/v2/hook/f99fed8d-9b01-4dfe-ab56-xxxx" -Method POST -Headers @{"Content-Type"="application/json"} -Body '{"msg_type":"interactive","card":{"type":"template","data":{"template_id":"AAqyBQVmUNxxx","template_version_name":"1.0.0"}}}'
         ```

![image.png](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/7cf96e352b91bc746cec619936d87f10_JL37HOMQf1.png?height=539&lazyload=true&maxWidth=650&width=1208)

Explanation for the above code is as follows:

Code | Description
---|---
POST | Request method, no modification needed.
Content-Type: application/json | Request header, no modification needed.
<pre><br><code><br>{<br>"msg_type": "interactive",<br>"card": {<br>"type": "template",<br>"data": {<br>"template_id": "AAqyBQVmUNxxx",<br>"template_version_name": "1.0.0"<br>}<br>}<br>}<br></code><br></pre> | Request body containing Feishu card ID and version information. The data structure is as shown, in actual use, you need to:<br>- Set the example values to actual values<br>- Depending on your system environment, compress or escape the JSON<br>- Customize variable values. Below is an example of variables added based on the data report card case:<br>```json<br>{<br>"type": "template",<br>"data": {<br>"template_id": "AAqi6xJ8rabcd",<br>"template_version_name": "1.0.0",<br>"template_variable": {<br>"summary_ticket": "10",<br>"summary_hours": "10",<br>"summary_pending": "20%",<br>"object_list_1": [<br>{<br>"name": "Wang Bing",<br>"duration": "Less than 1 hour",<br>"diff": "↓12%",<br>"diff_color": "green"<br>},<br>{<br>"name": "Wang Fang",<br>"duration": "2 hours",<br>"diff": "↑5%",<br>"diff_color": "red"<br>},<br>{<br>"name": "Zhang Min",<br>"duration": "3 hours",<br>"diff": "↓25%",<br>"diff_color": "green"<br>}<br>]<br>}<br>}<br>}
<pre><br><code><br>https://open.feishu.cn/open-apis/bot/v2/hook/f99fed8d-9b01-4dfe-ab56-xxxx<br></code><br></pre> | Custom bot's Webhook address. This is an example value, and you need to replace it with the actual webhook address of your custom bot.

Return to the group chat where the custom bot resides and check the received Feishu card. If the call fails, you can search for the returned error code and error message in the documentation to obtain troubleshooting suggestions.

![image.png](//sf3-cn.feishucdn.com/obj/open-platform-opendoc/672c6fc3bfb7559435910e09196f21fe_IEpu0XyTLF.png?height=957&lazyload=true&maxWidth=500&width=1320)
