DingTalk Mini Program Ecology 5—DingTalk Group Robot Message Notification and DingTalk Work Notification

article navigation

DingTalk Mini Program Ecology 1 - Distinguish between enterprise internal applications, third-party enterprise applications, and third-party personal applications
DingTalk Mini Program Ecology 2 - Distinguish between Mini Programs and H5 micro-
applications
DingTalk Mini Program Ecology 4 - DingTalk Mini Program Three-party Enterprise Application Events and Callbacks
DingTalk Mini Program Ecology 5 - DingTalk Group Robot Message Notification and DingTalk Work Notification

foreword

In terms of message notifications, DingTalk can be said to have played well, such as job notifications, group robot notifications, and the evil Ding. DingTalk’s notifications are not only diverse, but most channels support customization, that is, you can customize the sending time and content, and also support various styles of messages such as text, cards, and Markdown.
In this article, I mainly introduce two commonly used types: DingTalk group robot notification and DingTalk job notification.

Dingding group robot notification renderings

DingTalk work notification renderings

I won’t write the specific differences and comparisons. The robot rule is posted in the group, and the work notice is posted in the work notice column. To put it simply, everyone can see the message sent by the DingTalk group robot message notification , and only the selected people can see the DingTalk work notification , so the DingTalk group robot message notification is suitable for sending announcements to notify everyone, DingTalk work notification It is suitable for sending work arrangement notices to specific people, but how to use it depends on the needs of the business.

DingTalk robot notification implementation

Official document link

自定义机器人接入:https://open.dingtalk.com/document/robots/custom-robot-access

自定义机器人安全设置:https://open.dingtalk.com/document/robots/customize-robot-security-settings

configuration process

1. Select the group chat where you want to add a bot, and then click Group Settings > Bot.

2. Click Add Bot and find Custom Bot

3. Enter configuration, security settings need to pay attention

由于创建自定义机器人必须选择安全设置,这里注意不要乱输,如果明白安全设置是啥最好,如果不懂建议选择加签的方式,然后看一下说明文档,不然测试的时候有可能会发送不成功。

4. Click Finish, and the Webhook will be generated

develop robot

1. Save the token and Webhook of the security settings

2. Import maven legacy dependencies

    <!-- 使用旧版钉钉开放api -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>alibaba-dingtalk-service-sdk</artifactId>
            <version>2.0.0</version>
        </dependency>
    </dependencies>

3. Write the test class

package com.example.dingtalkmsg.service;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.request.OapiRobotSendRequest.Links;
import com.dingtalk.api.response.OapiRobotSendResponse;
import org.apache.tomcat.util.codec.binary.Base64;

public class DingTalkRobotSendService {
    
    

    public static void main(String[] args) throws Exception {
    
    
        //安全设置加签
        Long timestamp = System.currentTimeMillis();
        String secret = "xxx ";
        String stringToSign = timestamp + "\n" + secret;
        Mac mac = Mac.getInstance("HmacSHA256");
        mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256"));
        byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
        String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), "UTF-8");

        //构建消息发送Client
        DingTalkClient client = new DefaultDingTalkClient(
            "https://oapi.dingtalk"
                + ".com/robot/send?access_token=xxx"
                + "&timestamp="
                + timestamp + "&sign=" + sign);
        //创建发送请求体
        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("xxxxx"));
        // isAtAll类型如果不为Boolean,请升级至最新SDK
        //at.setIsAtAll(true);
        //at.setAtUserIds(Arrays.asList("109929","32099"));

        request.setMsgtype("link");
        OapiRobotSendRequest.Link link = new OapiRobotSendRequest.Link();
        link.setMessageUrl("https://www.dingtalk.com/");
        link.setPicUrl("");
        link.setTitle("时代的火车向前开");
        link.setText("这个即将发布的新版本,创始人xx称它为红树林。而在此之前,每当面临重大升级,产品经理们都会取一个应景的代号,这一次,为什么是红树林");
        request.setLink(link);

        request.setMsgtype("markdown");
        OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
        markdown.setTitle("杭州天气");
        markdown.setText("#### 杭州天气 @156xxxx8827\n" +
            "> 9度,西北风1级,空气良89,相对温度73%\n\n" +
            "> ![screenshot](https://gw.alicdn.com/tfs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png)\n"  +
            "> ###### 10点20分发布 [天气](http://www.thinkpage.cn/) \n");
        request.setMarkdown(markdown);

        request.setMsgtype("feedCard");
        OapiRobotSendRequest.Feedcard feedcard = new OapiRobotSendRequest.Feedcard();
        OapiRobotSendRequest.Links links1 = new OapiRobotSendRequest.Links();
        links1.setTitle("时代的火车向前开1");
        links1.setMessageURL("https://www.dingtalk.com/");
        links1.setPicURL("https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png");

        OapiRobotSendRequest.Links links2 = new OapiRobotSendRequest.Links();
        links2.setTitle("时代的火车向前开2");
        links2.setMessageURL("https://www.dingtalk.com/");
        links2.setPicURL("https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png");
        List<Links> links = new ArrayList<>();
        links.add(links1);
        links.add(links2);
        feedcard.setLinks(links);
        request.setFeedCard(feedcard);

        OapiRobotSendResponse response = client.execute(request);
        System.out.println(response);
    }
}

Generally speaking, the development of the DingTalk robot push message does not have those convoluted settings, and it can be developed basically after reading the document, and does not require any special environmental support.

Implementation of DingTalk job notification

Official document link

第三方企业应用开发:https://open.dingtalk.com/document/isvapp/send-job-notification

configuration process

1. Create a third-party enterprise application

I originally thought that enterprise applications, like third-party enterprise applications, have a work notification after the application is created. After creating the application, I found that there is indeed one, but the difference is very big. In-enterprise application notifications are robot swarm messages, while third-party enterprise applications are template messages.

2. Click Application Functions—>Message Push—>Add

3. Select a Markdown template to create —> send a test message

4. After saving the message template, click Submit for review, and the review will usually pass in about half a day

Development work notification

1. Import maven legacy dependencies

    <!-- 使用旧版钉钉开放api -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>alibaba-dingtalk-service-sdk</artifactId>
            <version>2.0.0</version>
        </dependency>
    </dependencies>

2. Write the test class

package com.example.dingtalkmsg.service;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
import com.dingtalk.api.request.OapiServiceGetCorpTokenRequest;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.dingtalk.api.response.OapiServiceGetCorpTokenResponse;

public class DingTalkWorkNoticeSendService {
    
    

    public static void main(String[] args) throws Exception {
    
    
        //获取小程序的accessToken
        DefaultDingTalkClient client1 = new DefaultDingTalkClient("https://oapi.dingtalk.com/service/get_corp_token");
        OapiServiceGetCorpTokenRequest req = new OapiServiceGetCorpTokenRequest();
        req.setAuthCorpid("dingxxxx");
        //suiteTicket获取比较麻烦,后续我会单独写文章说明
        OapiServiceGetCorpTokenResponse execute = client1.execute(req, "xxx", "xxx", "xxx");

        //构建工作通知推送Client
        DingTalkClient client2 = new DefaultDingTalkClient(
            "https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
        OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
        request.setAgentId(836390886L);
        request.setUseridList("user123");
        request.setToAllUser(false);

        //构建消息模板
        OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
        msg.setMsgtype("text");
        msg.setText(new OapiMessageCorpconversationAsyncsendV2Request.Text());
        msg.getText().setContent("test123");
        request.setMsg(msg);

        msg.setMsgtype("image");
        msg.setImage(new OapiMessageCorpconversationAsyncsendV2Request.Image());
        msg.getImage().setMediaId("@lADOdvRYes0CbM0CbA");
        request.setMsg(msg);

        msg.setMsgtype("file");
        msg.setFile(new OapiMessageCorpconversationAsyncsendV2Request.File());
        msg.getFile().setMediaId("@lADOdvRYes0CbM0CbA");
        request.setMsg(msg);

        msg.setMsgtype("link");
        msg.setLink(new OapiMessageCorpconversationAsyncsendV2Request.Link());
        msg.getLink().setTitle("test");
        msg.getLink().setText("test");
        msg.getLink().setMessageUrl("test");
        msg.getLink().setPicUrl("test");
        request.setMsg(msg);

        msg.setMsgtype("markdown");
        msg.setMarkdown(new OapiMessageCorpconversationAsyncsendV2Request.Markdown());
        msg.getMarkdown().setText("##### text");
        msg.getMarkdown().setTitle("### Title");
        request.setMsg(msg);

        msg.setOa(new OapiMessageCorpconversationAsyncsendV2Request.OA());
        msg.getOa().setHead(new OapiMessageCorpconversationAsyncsendV2Request.Head());
        msg.getOa().getHead().setText("head");
        msg.getOa().setBody(new OapiMessageCorpconversationAsyncsendV2Request.Body());
        msg.getOa().getBody().setContent("xxx");
        msg.setMsgtype("oa");
        request.setMsg(msg);

        msg.setActionCard(new OapiMessageCorpconversationAsyncsendV2Request.ActionCard());
        msg.getActionCard().setTitle("xxx123411111");
        msg.getActionCard().setMarkdown("### 测试123111");
        msg.getActionCard().setSingleTitle("测试测试");
        msg.getActionCard().setSingleUrl("https://www.dingtalk.com");
        msg.setMsgtype("action_card");
        request.setMsg(msg);
        OapiMessageCorpconversationAsyncsendV2Response rsp = client2.execute(request, execute.getAccessToken());
        System.out.println(rsp.getBody());
    }
}

In fact, it is not difficult to develop work notifications, but since this is a third-party application, all interfaces of the third-party applications require an accessToken. And this accessToken is not simply obtained by ak/sk. DingTalk also needs a suiteTicket. This suiteTicket is the biggest difficulty in development.
The suiteTicket is not obtained by calling the interface, but is actively pushed by DingTalk, so we also need to write a callback interface for DingTalk to call. For details, please refer to this article: Add a link description .

Guess you like

Origin blog.csdn.net/weixin_33005117/article/details/130067798