企业内部机器人群聊实现@人接入

钉钉@机器人
链接: 官网地址

  • pom.xml
<dependency>
   <groupId>com.aliyun</groupId>
    <artifactId>dingtalk</artifactId>
    <version>2.0.18</version>
</dependency>
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>alibaba-dingtalk-service-sdk</artifactId>
    <version>2.0.0</version>
</dependency>

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.76</version>
</dependency>
  • Test.java
package com.example.demo.controller;

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

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import com.taobao.api.ApiException;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RestController
public class TestController {
    private static final String Text = "Text@人员效果";

    /**
     * 实现机器人发送text@人员消息
     */
    @RequestMapping(value = "/robots", method = RequestMethod.POST)
    public String helloRobots(@RequestBody(required = false) JSONObject json) {

        System.out.println(JSON.toJSONString(json));

        String content = json.getJSONObject("text").get("content").toString().replaceAll(" ","");
        //获取用户userId
        String userId = json.getString("senderStaffId");
        String sessionWebhook = json.getString("sessionWebhook");
        DingTalkClient client = new DefaultDingTalkClient(sessionWebhook);

        String pattern = "((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})(\\.((2(5[0-5]|[0-4]\\d))|[0-1]?\\d{1,2})){3}";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(content);
        System.out.println("是否为ip:" + m.matches());

        if (m.matches()) {
            text(client, userId, content);
        }else {
            text(client, userId, "非ip格式");
        }


//        try {
//            sendMessageWebhook(userId);
//        } catch (ApiException e) {
//            e.printStackTrace();
//        }

        return null;
    }

    /**
     * 实现@人员
     * @param client
     * @param userId
     */
    private void text(DingTalkClient client, String userId,String content) {
        try {
            OapiRobotSendRequest request = new OapiRobotSendRequest();
            request.setMsgtype("text");
            OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();

            text.setContent(" @" + userId + "  \n  " + content + "【-----已处理--】");
            request.setText(text);
            OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();

            System.out.println("用户id:"+userId);

            at.setAtUserIds(Arrays.asList(userId));

            //   isAtAll类型如果不为Boolean,请升级至最新SDK
            at.setIsAtAll(false);

            request.setAt(at);
            OapiRobotSendResponse response = client.execute(request);

            System.out.println(response.getBody());

        } catch (ApiException e) {
            e.printStackTrace();
        }
    }



    // 机器人发送群聊消息
//    public  static void sendMessageWebhook(String userId) throws ApiException {
//        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send?access_token=6d0c**************d4d5d");
//        OapiRobotSendRequest request = new OapiRobotSendRequest();
//        request.setMsgtype("text");
//        OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
//        text.setContent("测试文本TEST消息2233");
//        request.setText(text);
//        OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
//        at.setAtUserIds(Arrays.asList(userId));
//        at.setIsAtAll(false);
//        request.setAt(at);
//        OapiRobotSendResponse response = client.execute(request);
//        System.out.println(response.getBody());
//    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值