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


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

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

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

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

    0、依赖 
    1. <dependency>
    2. <groupId>com.aliyun</groupId>
    3. <artifactId>dingtalk</artifactId>
    4. <version>2.0.87</version>
    5. </dependency>
    6. <dependency>
    7. <groupId>com.aliyun</groupId>
    8. <artifactId>alibaba-dingtalk-service-sdk</artifactId>
    9. <version>2.0.0</version>
    10. </dependency>
    1、找到你的机器人,迁移到内部应用

    2、放开这个权限    Card.Instance.Write

    3、参数要求,重要 (这是发给个人的)

    3.1 发给群的参数这样写 (看红框里的)

    3.2其他参数

    3.3 你的入参是这样的就对了
    1. {
    2. "cardData":{
    3. "cardParamMap":{
    4. "count":"{\"data\":[{\"x\":\"N0\",\"type\":\"line\",\"y\":48},{\"x\":\"N1\",\"type\":\"line\",\"y\":74}],\"type\":\"histogram\",\"config\":{}}"
    5. }
    6. },
    7. "outTrackId":"test23",
    8. "cardTemplateId":"785b7cec-c8c5-4bba-99be-6da77befb022.schema",
    9. "openSpaceId":"dtv1.card//im_group.cid4cUEK93zmHMHb1ycvDESQQ==",
    10. "imRobotOpenDeliverModel":{
    11. "spaceType":"IM_ROBOT"
    12. },
    13. "imGroupOpenSpaceModel":{
    14. "supportForward":false
    15. },
    16. "imGroupOpenDeliverModel":{
    17. "robotCode":"ding0a8fitygi7torxla"
    18. }
    19. }
    4、柱状图效果

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

    5、工具类代码
    1. import com.aliyun.dingtalkcard_1_0.models.CreateAndDeliverHeaders;
    2. import com.aliyun.dingtalkcard_1_0.models.CreateAndDeliverRequest;
    3. import com.aliyun.dingtalkcard_1_0.models.CreateAndDeliverResponse;
    4. import com.aliyun.tea.TeaException;
    5. import com.aliyun.teautil.models.RuntimeOptions;
    6. import lombok.extern.slf4j.Slf4j;
    7. import org.apache.commons.lang3.RandomStringUtils;
    8. import java.util.Map;
    9. /**
    10. * @author szl
    11. * @date 2024/2/29 0029 10:38
    12. */
    13. @Slf4j
    14. public class DingCardUtils {
    15. public static final String APP_KEY = "dinwergsdfrx";
    16. public static final String APP_SECRET = "G8Gasdfsdfasd";
    17. public static String cardTemplateId_personal = "3b8besadfsdf-85asdff1.schema";
    18. public static String cardTemplateId_principal = "96sadf9.schema";
    19. public static com.aliyun.dingtalkcard_1_0.Client createClient() throws Exception {
    20. com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config();
    21. config.protocol = "https";
    22. config.regionId = "central";
    23. return new com.aliyun.dingtalkcard_1_0.Client(config);
    24. }
    25. /**
    26. * 给某个人 创建并投放卡片
    27. */
    28. public static void sendDingcard(String receiveUserId, String outTrackId, Map<String, String> cardDataCardParamMap, String cardTemplateId, String Key) throws Exception {
    29. com.aliyun.dingtalkcard_1_0.Client client = createClient();
    30. CreateAndDeliverHeaders createAndDeliverHeaders = new CreateAndDeliverHeaders();
    31. String accessToken = DingUtils.setAccessToken();
    32. createAndDeliverHeaders.xAcsDingtalkAccessToken = accessToken;
    33. CreateAndDeliverRequest.CreateAndDeliverRequestCardData cardData = new CreateAndDeliverRequest.CreateAndDeliverRequestCardData()
    34. .setCardParamMap(cardDataCardParamMap);
    35. CreateAndDeliverRequest.CreateAndDeliverRequestImRobotOpenDeliverModel imRobotOpenDeliverModel = new CreateAndDeliverRequest.CreateAndDeliverRequestImRobotOpenDeliverModel()
    36. .setSpaceType("IM_ROBOT");//折线图卡片需要这个
    37. CreateAndDeliverRequest.CreateAndDeliverRequestImRobotOpenSpaceModel imRobotOpenSpaceModel = new CreateAndDeliverRequest.CreateAndDeliverRequestImRobotOpenSpaceModel()
    38. .setSupportForward(false);
    39. CreateAndDeliverRequest.CreateAndDeliverRequestImGroupOpenDeliverModel imGroupOpenDeliverModel = new CreateAndDeliverRequest.CreateAndDeliverRequestImGroupOpenDeliverModel()
    40. .setRobotCode(APP_KEY);
    41. CreateAndDeliverRequest createAndDeliverRequest = new CreateAndDeliverRequest()
    42. .setOutTrackId(outTrackId)
    43. .setCardTemplateId(cardTemplateId)
    44. .setOutTrackId(outTrackId)
    45. .setCardData(cardData)
    46. .setImRobotOpenSpaceModel(imRobotOpenSpaceModel)
    47. .setOpenSpaceId("dtv1.card//IM_ROBOT." + receiveUserId + ";")
    48. .setImGroupOpenDeliverModel(imGroupOpenDeliverModel)
    49. .setImRobotOpenDeliverModel(imRobotOpenDeliverModel)
    50. .setUserIdType(1);
    51. try {
    52. CreateAndDeliverResponse andDeliverWithOptions = client.createAndDeliverWithOptions(createAndDeliverRequest, createAndDeliverHeaders, new RuntimeOptions());
    53. log.info("卡片id为:" + andDeliverWithOptions);
    54. } catch (TeaException err) {
    55. err.printStackTrace();
    56. }
    57. }
    58. /**
    59. * 获取消息id
    60. *
    61. * @return
    62. */
    63. public static String getOutTrackId() {
    64. return System.currentTimeMillis() + RandomStringUtils.randomAlphanumeric(6);
    65. }

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

    1. /**
    2. * 初始化--权限Client
    3. *
    4. * @return Client
    5. * @throws Exception
    6. */
    7. public static com.aliyun.dingtalkoauth2_1_0.Client createAuthClient() throws Exception {
    8. Config config = new Config();
    9. config.protocol = "https";
    10. config.regionId = "central";
    11. return new com.aliyun.dingtalkoauth2_1_0.Client(config);
    12. }
    13. /**
    14. * 获取并设置最新accessToken,每两小时失效
    15. */
    16. public static String setAccessToken() throws Exception {
    17. com.aliyun.dingtalkoauth2_1_0.Client client = createAuthClient();
    18. GetAccessTokenRequest getAccessTokenRequest = new GetAccessTokenRequest()
    19. .setAppKey(APP_KEY)
    20. .setAppSecret(APP_SECRET);
    21. try {
    22. GetAccessTokenResponse accessToken = client.getAccessToken(getAccessTokenRequest);
    23. System.out.println("000----:" + accessToken);
    24. //设置access_token
    25. return accessToken.getBody().accessToken;
    26. } catch (TeaException err) {
    27. System.out.println(err.message);
    28. return null;
    29. } catch (Exception _err) {
    30. TeaException err = new TeaException(_err.getMessage(), _err);
    31. System.out.println(err.message);
    32. return null;
    33. }
    34. }

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

    1. /**
    2. * 拼接数据后发送
    3. *
    4. * @param aname a name
    5. * @param dnum a number
    6. */
    7. public void sendDingcard(String campusName, String receiveUserId, String cardTemplateId,
    8. String aname, String bname, String cname, String dname,
    9. Integer anum, Integer bnum, Integer cnum, Integer dnum) {
    10. try {
    11. List<JSONObject> data = new ArrayList<>();
    12. JSONObject json1 = new JSONObject();
    13. json1.put("x", aname);
    14. json1.put("y", anum);
    15. json1.put("type", aname);
    16. data.add(json1);
    17. JSONObject json2 = new JSONObject();
    18. json2.put("x", bname);
    19. json2.put("y", bnum);
    20. json2.put("type", bname);
    21. data.add(json2);
    22. JSONObject json3 = new JSONObject();
    23. json3.put("x", cname);
    24. json3.put("y", cnum);
    25. json3.put("type", cname);
    26. data.add(json3);
    27. JSONObject json4 = new JSONObject();
    28. json4.put("x", dname);
    29. json4.put("y", dnum);
    30. json4.put("type", dname);
    31. data.add(json4);
    32. JSONObject count = new JSONObject();
    33. count.put("data", data);
    34. count.put("type", "histogram");
    35. count.put("config", new JSONObject());
    36. Map<String, String> cardCreateParam = new HashMap<>();
    37. cardCreateParam.put("campusName", campusName);
    38. cardCreateParam.put("yifenpei", anum.toString());
    39. cardCreateParam.put("weifenpei", bnum.toString());
    40. cardCreateParam.put("yigoutong", bnum.toString());
    41. cardCreateParam.put("weigoutong", cnum.toString());
    42. cardCreateParam.put("weizhuanhua", dnum.toString());
    43. cardCreateParam.put("chartData", count.toString());
    44. DingCardUtils.sendDingcard(receiveUserId, DingCardUtils.getOutTrackId(), cardCreateParam, cardTemplateId, null);
    45. } catch (Exception e) {
    46. e.printStackTrace();
    47. }
    48. }

    看不懂的,评论区问

    2024年3月1日14:02:18

  • 相关阅读:
    马赫数相关函数
    多线程概述
    【目标检测】【边界框回归】Bounding-Box regression
    KubeEdge 边缘端架构设计
    API 网关的功能
    web期末网站设计大作业:基于HTML+CSS+JavaScript制作新能源汽车企业网站
    linux脚本-使用top命令监控进程cpu与mem占用
    实现前后端分离开发:构建现代化Web应用
    HTML5期末大作业——HTML+CSS+JavaScript平遥古城旅游景点介绍(6页)
    电容笔和Apple pencil有啥区别?电容笔四大口碑比较好的品牌推荐
  • 原文地址:https://blog.csdn.net/weixin_40863853/article/details/136393879