In the Java project, Feishu custom robot Webhook is used to push alarm notifications to Feishu Group.

Today, let’s take a look at how to use Feishu’s custom robot Webhook in a Java project to push alarm notifications to Feishu Group.

1. Functional scenarios

        Enterprises have the need to automatically push messages to specific groups , such as: monitoring alarm push, sales lead push, operation content push, etc.
       You can add one to the group chat 自定义机器人and call the address on the server  webhook to push notification messages from external systems to the group chat instantly. We also provide security configurations in three dimensions  : custom keywords , IP whitelists and signatureswebhook to control  the calling scope.


 Notice :

  • You need to have a certain foundation in server-side development to implement the message push function by calling the custom robot's webhook address.
  • The custom robot can be used after being added to the group and does not require review by the tenant administrator. This improves the convenience of developing robots, but also limits the usage scenarios of custom robots due to tenant data security considerations. Custom bots do not have any data access.

2. Operation process

Step one: Invite custom robots to join the group

       Enter your target group, open the conversation settings , find the group robot , click Add Robot , and select a custom robot to join the group chat.

 Enter a suitable name and description for your robot, and you can also set a suitable avatar for the robot, then click Next.

Step 2: Configure webhook

You will get the webhook address of the robot in the following format:

https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxxxxxxxxxxxx

Step 3: Call webhook to send message

       Use any method to initiate an HTTP POST request to the webhook to send a message to the group chat where the custom robot is located.

Note:
You need a certain foundation in server-side development to call the webhook address through server-side request.
Taking the curl instruction as an example, the request example is as follows:

curl -X POST -H "Content-Type: application/json" \
	-d '{"msg_type":"text","content":{"text":"request example"}}' \
  https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxxxxxxxxxxxx 

You can copy the above instructions to the "Terminal" application of macOS system (or the "Console" application of Windows system) for testing.

Please replace https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxxxxxxxxxxxx in the above code with the address of the real webhook. If there is an error in the test, please first check whether the copied instruction has the same structure as the test instruction.

If the request is successful, the return body is:

{
	"Extra": null,
	"StatusCode": 0,
	"StatusMessage": "success"
}

If the request body format is incorrect, the return body is as follows. Check, please:

  • Whether the request body content format is consistent with the sample code of each message type
  • The request body size cannot exceed 20k
{
	"code": 9499,
	"msg": "Bad Request",
	"data": {}
}

3. Java code writing

After the group is configured and the Webhook value is obtained, message push can be called in the java code.

Paste the code below

import cn.hutool.http.HttpRequest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
 

public class SendMessage {
    
    //这里就是刚才拿到的Webhook的值
    public static final String WebHookUrl =
            "https://open.feishu.cn/open-apis/bot/v2/hook/xxxxxxxxxxxxxxxxx";
 
    public static void sendMessage(String msg){
        //请求的JSON数据,这里用map在工具类里转成json格式
        Map<String,Object> json=new HashMap();
        Map<String,Object> text=new HashMap();
        json.put("msg_type", "text");
        text.put("text", "项目告警通知:" + msg);
        json.put("content", text);
        //发送post请求
        String result = HttpRequest.post(WebHookUrl).body(JSON.toJSONString(json), "application/json;charset=UTF-8").execute().body();
        System.out.println(result);
    }
 
}

 Test case:

@SpringBootTest
public class SendMsgTests {

    @Test
    public void sendTest() throws IOException {
        SendMessage.sendMessage("123");
    }
}

After clicking Run, you can see that the Feishu group you just created has received the test message we pushed.


References: Development Documents - Feishu Open Platform
References: Using DingTalk Robot Webhook to Push Alarm Notifications to DingTalk Groups in Java Projects_The Blog of the Cat Who Eats Melon Seeds-CSDN Blog_java webhook

Guess you like

Origin blog.csdn.net/chennnnnn_/article/details/127249088
Recommended