钉钉机器人发送折线图卡片 工具类代码

钉钉机器人 "创建并投放卡片 接口 " 可以 发送折线图、柱状图

官方文档:创建并投放卡片 - 钉钉开放平台

0依赖、1模板、2机器人放到内部应用、3放开这个权限 、4工具类、5调用工具类 拼接入参

卡片模板 自己看文档创建,卡片模板的id 有用

0、依赖
<dependency>
  <groupId>com.aliyun</groupId>
  <artifactId>dingtalk</artifactId>
  <version>2.0.87</version>
</dependency>

        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>alibaba-dingtalk-service-sdk</artifactId>
            <version>2.0.0</version>
        </dependency>
1、找到你的机器人,迁移到内部应用
2、放开这个权限 Card.Instance.Write
3、参数要求,重要 (这是发给个人的)
3.1 发给群的参数这样写 (看红框里的)
3.2其他参数
3.3 你的入参是这样的就对了
{
  "cardData":{
    "cardParamMap":{
        "count":"{\"data\":[{\"x\":\"N0\",\"type\":\"line\",\"y\":48},{\"x\":\"N1\",\"type\":\"line\",\"y\":74}],\"type\":\"histogram\",\"config\":{}}"
            }
  },
  "outTrackId":"test23",
  "cardTemplateId":"785b7cec-c8c5-4bba-99be-6da77befb022.schema",
  "openSpaceId":"dtv1.card//im_group.cid4cUEK93zmHMHb1ycvDESQQ==",

  "imRobotOpenDeliverModel":{
    "spaceType":"IM_ROBOT"
  },
  "imGroupOpenSpaceModel":{
    "supportForward":false
  },
  "imGroupOpenDeliverModel":{
    "robotCode":"ding0a8fitygi7torxla"
  }
}
4、柱状图效果

卡片模板 自己看文档创建,卡片模板的id 有用

5、工具类代码
import com.aliyun.dingtalkcard_1_0.models.CreateAndDeliverHeaders;
import com.aliyun.dingtalkcard_1_0.models.CreateAndDeliverRequest;
import com.aliyun.dingtalkcard_1_0.models.CreateAndDeliverResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teautil.models.RuntimeOptions;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;

import java.util.Map;

/**
 * @author szl
 * @date 2024/2/29 0029 10:38
 */
@Slf4j
public class DingCardUtils {
    public static final String APP_KEY = "dinwergsdfrx";
    public static final String APP_SECRET = "G8Gasdfsdfasd";
    public static String cardTemplateId_personal = "3b8besadfsdf-85asdff1.schema";
    public static String cardTemplateId_principal = "96sadf9.schema";

    public static com.aliyun.dingtalkcard_1_0.Client createClient() throws Exception {
        com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
        config.protocol = "https";
        config.regionId = "central";
        return new com.aliyun.dingtalkcard_1_0.Client(config);
    }

    /**
     * 给某个人 创建并投放卡片
     */
    public static void sendDingcard(String receiveUserId, String outTrackId, Map<String, String> cardDataCardParamMap, String cardTemplateId, String Key) throws Exception {
        com.aliyun.dingtalkcard_1_0.Client client = createClient();
        CreateAndDeliverHeaders createAndDeliverHeaders = new CreateAndDeliverHeaders();
        String accessToken = DingUtils.setAccessToken();
        createAndDeliverHeaders.xAcsDingtalkAccessToken = accessToken;
        CreateAndDeliverRequest.CreateAndDeliverRequestCardData cardData = new CreateAndDeliverRequest.CreateAndDeliverRequestCardData()
                .setCardParamMap(cardDataCardParamMap);
        CreateAndDeliverRequest.CreateAndDeliverRequestImRobotOpenDeliverModel imRobotOpenDeliverModel = new CreateAndDeliverRequest.CreateAndDeliverRequestImRobotOpenDeliverModel()
                .setSpaceType("IM_ROBOT");//折线图卡片需要这个
        CreateAndDeliverRequest.CreateAndDeliverRequestImRobotOpenSpaceModel imRobotOpenSpaceModel = new CreateAndDeliverRequest.CreateAndDeliverRequestImRobotOpenSpaceModel()
                .setSupportForward(false);
        CreateAndDeliverRequest.CreateAndDeliverRequestImGroupOpenDeliverModel imGroupOpenDeliverModel = new CreateAndDeliverRequest.CreateAndDeliverRequestImGroupOpenDeliverModel()
                .setRobotCode(APP_KEY);

        CreateAndDeliverRequest createAndDeliverRequest = new CreateAndDeliverRequest()
                .setOutTrackId(outTrackId)
                .setCardTemplateId(cardTemplateId)
                .setOutTrackId(outTrackId)
                .setCardData(cardData)
                .setImRobotOpenSpaceModel(imRobotOpenSpaceModel)
                .setOpenSpaceId("dtv1.card//IM_ROBOT." + receiveUserId + ";")
                .setImGroupOpenDeliverModel(imGroupOpenDeliverModel)
                .setImRobotOpenDeliverModel(imRobotOpenDeliverModel)
                .setUserIdType(1);
        try {
            CreateAndDeliverResponse andDeliverWithOptions = client.createAndDeliverWithOptions(createAndDeliverRequest, createAndDeliverHeaders, new RuntimeOptions());
            log.info("卡片id为:" + andDeliverWithOptions);
        } catch (TeaException err) {
            err.printStackTrace();
        }
    }

    /**
     * 获取消息id
     *
     * @return
     */
    public static String getOutTrackId() {
        return System.currentTimeMillis() + RandomStringUtils.randomAlphanumeric(6);
    }

5.2 获取token 的代码 不再详细描述 APP_KEY, APP_SECRET 自己的

/**
     * 初始化--权限Client
     *
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dingtalkoauth2_1_0.Client createAuthClient() throws Exception {
        Config config = new Config();
        config.protocol = "https";
        config.regionId = "central";
        return new com.aliyun.dingtalkoauth2_1_0.Client(config);
    }

    /**
     * 获取并设置最新accessToken,每两小时失效
     */
    public static String setAccessToken() throws Exception {
        com.aliyun.dingtalkoauth2_1_0.Client client = createAuthClient();
        GetAccessTokenRequest getAccessTokenRequest = new GetAccessTokenRequest()
                .setAppKey(APP_KEY)
                .setAppSecret(APP_SECRET);
        try {
            GetAccessTokenResponse accessToken = client.getAccessToken(getAccessTokenRequest);
            System.out.println("000----:" + accessToken);
            //设置access_token
            return accessToken.getBody().accessToken;
        } catch (TeaException err) {
            System.out.println(err.message);
            return null;
        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            System.out.println(err.message);
            return null;
        }
    }

5.3、拼接数据,调用工具类 (入参看不懂的,评论区问)

/**
     * 拼接数据后发送
     *
     * @param aname a name
     * @param dnum  a number
     */
    public void sendDingcard(String campusName, String receiveUserId, String cardTemplateId,
                             String aname, String bname, String cname, String dname,
                             Integer anum, Integer bnum, Integer cnum, Integer dnum) {
        try {
            List<JSONObject> data = new ArrayList<>();
            JSONObject json1 = new JSONObject();
            json1.put("x", aname);
            json1.put("y", anum);
            json1.put("type", aname);
            data.add(json1);
            JSONObject json2 = new JSONObject();
            json2.put("x", bname);
            json2.put("y", bnum);
            json2.put("type", bname);
            data.add(json2);
            JSONObject json3 = new JSONObject();
            json3.put("x", cname);
            json3.put("y", cnum);
            json3.put("type", cname);
            data.add(json3);
            JSONObject json4 = new JSONObject();
            json4.put("x", dname);
            json4.put("y", dnum);
            json4.put("type", dname);
            data.add(json4);
            JSONObject count = new JSONObject();
            count.put("data", data);
            count.put("type", "histogram");
            count.put("config", new JSONObject());
            Map<String, String> cardCreateParam = new HashMap<>();
            cardCreateParam.put("campusName", campusName);
            cardCreateParam.put("yifenpei", anum.toString());
            cardCreateParam.put("weifenpei", bnum.toString());
            cardCreateParam.put("yigoutong", bnum.toString());
            cardCreateParam.put("weigoutong", cnum.toString());
            cardCreateParam.put("weizhuanhua", dnum.toString());
            cardCreateParam.put("chartData", count.toString());
            DingCardUtils.sendDingcard(receiveUserId, DingCardUtils.getOutTrackId(), cardCreateParam, cardTemplateId, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

看不懂的,评论区问

2024年3月1日14:02:18

相关推荐
Daniel 大东8 分钟前
BugJson因为json格式问题OOM怎么办
java·安全
Theodore_10224 小时前
4 设计模式原则之接口隔离原则
java·开发语言·设计模式·java-ee·接口隔离原则·javaee
冰帝海岸5 小时前
01-spring security认证笔记
java·笔记·spring
世间万物皆对象6 小时前
Spring Boot核心概念:日志管理
java·spring boot·单元测试
没书读了6 小时前
ssm框架-spring-spring声明式事务
java·数据库·spring
小二·6 小时前
java基础面试题笔记(基础篇)
java·笔记·python
开心工作室_kaic7 小时前
ssm161基于web的资源共享平台的共享与开发+jsp(论文+源码)_kaic
java·开发语言·前端
懒洋洋大魔王7 小时前
RocketMQ的使⽤
java·rocketmq·java-rocketmq
武子康7 小时前
Java-06 深入浅出 MyBatis - 一对一模型 SqlMapConfig 与 Mapper 详细讲解测试
java·开发语言·数据仓库·sql·mybatis·springboot·springcloud
转世成为计算机大神7 小时前
易考八股文之Java中的设计模式?
java·开发语言·设计模式