SpringBoot集成钉钉企业内机器人群消息推送

参考

开发钉钉内部机器人

机器人概述

一、简介

企业机器人是钉钉为用户提供的组织内部使用的机器人,为组织数字化转型业务服务。本教程指导你为企业或组织开发一个内部机器人,企业内的其他成员可以在不同的群中添加该机器人,实现消息问答。

二、效果

1、群聊自动回复

在这里插入图片描述

2、单聊

在这里插入图片描述

三、开始开发

创建机器人

1、下载代码

下载机器人服务代码

git clone https://github.com/open-dingtalk/org-mini-program-tutorial-java.git

下载公网内网穿透映射工具


git clone https://github.com/open-dingtalk/pierced.git
cd pierced
cd windows_64/
# 映射SpringBoot端口
./ding -config ding.cfg -subdomain dingrobot 8080

启动完客户端后,你访问 http://dingrobot.vaiwan.com/xxxxx 都会映射到 http://127.0.0.1:8080/xxxxx。

支持的消息类型
在这里插入图片描述

2、获取Appkey等参数

在这里插入图片描述

3、配置回调

在这里插入图片描述

4、机器人被@时触发接收消息并自动回复

/**
     * 响应群里@机器人毁掉
     * <a href="https://open.dingtalk.com/document/tutorial/create-a-robot">...</a>
     * @param json
     * @return
     */
    @RequestMapping(value = "/robots")
    public String helloRobots(@RequestBody(required = false) JSONObject json
    ) {
        System.out.println(json);
        String content = json.getJSONObject("text").get("content").toString().replaceAll(" ", "");
        if ("测试卡片消息".contains(content)) {
            // String mobile = getUserMobile(json.getString("senderStaffId"));
            String sessionWebhook = json.getString("sessionWebhook");
            DingTalkClient client = new DefaultDingTalkClient(sessionWebhook);
            hello(client);
            return null;
        }
        return learning();
    }
    /**
     * 回答你好
     */
    public void hello(DingTalkClient client) {
        try {
            OapiRobotSendRequest request = new OapiRobotSendRequest();
            request.setMsgtype("text");
            OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
            text.setContent("你也好 ~");
            request.setText(text);
            // OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
            // at.setAtMobiles(Arrays.asList(mobile));
            // isAtAll类型如果不为Boolean,请升级至最新SDK
            // at.setIsAtAll(false);
            // request.setAt(at);
            OapiRobotSendResponse response = client.execute(request);
            System.out.println(response.getBody());
        } catch (ApiException e) {
            e.printStackTrace();
        }
    }

收到的回调消息,字段说明参考:https://open.dingtalk.com/document/orgapp/receive-message

{
    "conversationId": "xxx",
    "atUsers": [
        {
            "dingtalkId": "xxx",
            "staffId":"xxx"
        }
    ],
    "chatbotCorpId": "dinge8a565xxxx",
    "chatbotUserId": "$:LWCP_v1:$Cxxxxx",
    "msgId": "msg0xxxxx",
    "senderNick": "杨xx",
    "isAdmin": true,
    "senderStaffId": "user123",
    "sessionWebhookExpiredTime": 1613635652738,
    "createAt": 1613630252678,
    "senderCorpId": "dinge8a565xxxx",
    "conversationType": "2",
    "senderId": "$:LWCP_v1:$Ff09GIxxxxx",
    "conversationTitle": "机器人测试-TEST",
    "isInAtList": true,
    "sessionWebhook": "https://oapi.dingtalk.com/robot/sendBySession?session=xxxxx",
    "text": {
        "content": " 你好"
    },
    "msgtype": "text"
}

回复功能通过解析回调中webhook方式,"https://oapi.dingtalk.com/robot/sendBySession?session=1bed7e87c19xxe95289d40265ba

五、消息推送

1、测试批量发送人与机器人会话消息(单聊)

https://open-dev.dingtalk.com/apiExplorer?spm=ding_open_doc.document.0.0.330b60e1hBuxK7#/?devType=org&api=robot_1.0%23BatchSendOTO
userIds获取方式可以通过用户先发送消息给机器人,机器人在回调中拿到"senderStaffId":"09605xxxx180530"即用户id
在这里插入图片描述

public void sendOtoMessage(){
        try {
            String accessToken = AccessTokenUtil.getAccessToken();
            DingTalkClient client = new CustomDingTalkClient("https://api.dingtalk.com/v1.0/robot/oToMessages/batchSend");
            CustomDingmiO2oSendRequest req = new CustomDingmiO2oSendRequest();
            Map<String,String> headers = new HashMap<>();
            headers.put("x-acs-dingtalk-access-token",accessToken);
            req.setTopApiCallType(null);
            req.setHeaderMap(headers);
            req.setMsgKey("sampleText");
            req.setRobotCode("dingytxxxxatbs0ml");
            req.setUserid("01080429xxx3180530,09605xxxx0530");
            req.setMsgParam("{\"content\":\"你好\"}");
            OapiDingmiO2oSendResponse response = client.execute(req,accessToken);
            System.out.println(response.getBody());
        } catch (ApiException e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

2、测试使用webhook方式发送群聊消息

每个群聊中的机器人,都会有一个webhook的url,可以通过这个推送群聊消息

 /**
     * 通过企业内机器人的webhook发送消息
     * https://open.dingtalk.com/document/orgapp/assign-a-webhook-url-to-an-internal-chatbot
     */
    public void sendCorpInnerGroupMsgByWebhook() throws ApiException {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send?access_token=ef2b84d53d9571e5fxxx01c1f24d29973311c4d42260c7fc470e06");
            OapiRobotSendRequest request = new OapiRobotSendRequest();
            request.setMsgtype("text");
            OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
            text.setContent("测试文本消息");
            request.setText(text);
            OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
            at.setAtMobiles(Arrays.asList("155xxxxxxxx"));
            at.setIsAtAll(false);
            request.setAt(at);
            OapiRobotSendResponse response = client.execute(request);
            System.out.println(response.getBody());
    }

3、发送工作通知

/**
     * 发送工作群通知
     * <a href="https://open.dingtalk.com/document/orgapp/message-notification-overview?spm=a2q3p.21071111.0.0.5e891cfaDlrCma">...</a>
     */
    public void sendText3(){
        try {
            String accessToken = AccessTokenUtil.getAccessToken();
            System.out.println("accessToken:"+accessToken);
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
            OapiMessageCorpconversationAsyncsendV2Request req = new OapiMessageCorpconversationAsyncsendV2Request();
            req.setAgentId(2471223777L);
            req.setUseridList("0108xxxx180530");
            req.setToAllUser(false);
            OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
            msg.setMsgtype("text");
            msg.setText(new OapiMessageCorpconversationAsyncsendV2Request.Text());
            msg.getText().setContent("test123456");
            req.setMsg(msg);

            OapiMessageCorpconversationAsyncsendV2Response response = client.execute(req, accessToken);
            System.out.println(response.getBody());
        } catch (ApiException e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot集成钉钉单聊功能,你可以按照以下步骤进行操作: 1. 首先,确保你已经下载了钉钉SDK,并在Spring Boot项目中配置了相应的依赖。如果你没有配置maven插件,可能会导致加载bean时找不到对应的Class文件的异常。\[1\] 2. 获取钉钉的Appkey等参数,这些参数将用于与钉钉服务器进行通信。 3. 配置钉钉的回调功能,以便在机器人被@时触发接收消息并自动回复。 4. 开发单聊功能的代码逻辑,可以使用钉钉SDK提供的接口来实现。 5. 进行消息推送的测试,可以测试批量发送人与机器人会话消息(单聊)或使用webhook方式发送消息。 需要注意的是,如果你的Spring Boot项目部署在服务器上,还需要配置公网内网穿透映射工具,以便钉钉服务器能够正常访问你的项目。你可以下载并使用钉钉提供的pierced工具来进行映射配置。\[2\]\[3\] 希望以上信息对你有所帮助! #### 引用[.reference_title] - *1* [SpringBoot集成钉钉报警sdk(解决Failed to introspect Class异常)](https://blog.csdn.net/weixin_34621186/article/details/112814789)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [SpringBoot集成钉钉企业机器人消息推送](https://blog.csdn.net/Blueeyedboy521/article/details/129560022)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值