啊鑫
2024-11-18 16bbd0a2e072bafea1dc254fbbcf86768fb3ea48
src/main/java/com/gs/dingtalk/service/SimpleExample.java
@@ -1,18 +1,22 @@
package com.gs.dingtalk.service;
import cn.hutool.core.util.StrUtil;
import com.aliyun.dingtalkoauth2_1_0.Client;
import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest;
import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenResponse;
import com.aliyun.dingtalkrobot_1_0.models.BatchSendOTOHeaders;
import com.aliyun.dingtalkrobot_1_0.models.BatchSendOTORequest;
import com.aliyun.dingtalkrobot_1_0.models.BatchSendOTOResponse;
import com.aliyun.dingtalkrobot_1_0.models.BatchSendOTOResponseBody;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.Common;
import com.aliyun.teautil.models.RuntimeOptions;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.request.OapiV2UserGetbymobileRequest;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.dingtalk.api.response.OapiRobotSendResponse;
import com.dingtalk.api.response.OapiV2UserGetbymobileResponse;
import com.google.gson.Gson;
@@ -32,6 +36,8 @@
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.List;
/**
@@ -100,27 +106,53 @@
    /**
     * 发送消息
     */
    public DingTalkMessage sendMessage(String userIdList, String message) throws Exception {
    public DingTalkMessage sendMessage(String userIdListStr, String message) throws Exception {
        if (!StrUtil.isNotEmpty(userIdListStr)) {
            return new DingTalkMessage(1, 0L, "null");
        }
        String[] strArray = userIdListStr.split(",");
        List<String> userIdList = Arrays.asList(strArray);
        String accessToken = getAccessToken();
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2");
        OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
        request.setAgentId(DataAcquisitionConfiguration.AGENT_ID);
        request.setUseridList(userIdList);
        request.setToAllUser(false);
        com.aliyun.dingtalkrobot_1_0.Client client1 = createClient1();
        BatchSendOTOHeaders batchSendOTOHeaders = new BatchSendOTOHeaders();
        batchSendOTOHeaders.xAcsDingtalkAccessToken = accessToken;
        BatchSendOTORequest batchSendOTORequest = new BatchSendOTORequest()
                .setRobotCode("ding7n8fldhylh2rt2l2")
                .setUserIds(userIdList)
                .setMsgKey("sampleText")
                .setMsgParam("{\"content\": \"" + message + "\"}");
        OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
        msg.setMsgtype("text");
        msg.setText(new OapiMessageCorpconversationAsyncsendV2Request.Text());
        msg.getText().setContent(message);
        request.setMsg(msg);
        try {
            BatchSendOTOResponse batchSendOTOResponse = client1.batchSendOTOWithOptions(batchSendOTORequest, batchSendOTOHeaders, new RuntimeOptions());
        OapiMessageCorpconversationAsyncsendV2Response rsp = client.execute(request, accessToken);
            BatchSendOTOResponseBody body = batchSendOTOResponse.getBody();
            if (StrUtil.isNotEmpty(body.getProcessQueryKey())) {
                return new DingTalkMessage(0, 0L, "null");
            }
        Gson gson = new Gson();
        } catch (TeaException err) {
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                System.out.println(err.code);
                System.out.println(err.message);
        return gson.fromJson(rsp.getBody(), DingTalkMessage.class);
                System.out.println(err.accessDeniedDetail.toString());
            }
        } catch (Exception _err) {
            TeaException err = new TeaException(_err.getMessage(), _err);
            if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
                System.out.println(err.code);
                System.out.println(err.message);
            }
        }
        return new DingTalkMessage(1, 0L, "null");
        //return gson.fromJson(rsp.getBody(), DingTalkMessage.class);
    }
    /**
@@ -155,6 +187,13 @@
        return null;
    }
    public static com.aliyun.dingtalkrobot_1_0.Client createClient1() throws Exception {
        Config config = new Config();
        config.protocol = "https";
        config.regionId = "central";
        return new com.aliyun.dingtalkrobot_1_0.Client(config);
    }
    /**
     * 使用 Token 初始化账号Client
     *
@@ -168,6 +207,7 @@
        return new Client(config);
    }
    public OapiV2UserGetbymobileResponse getOapiV2UserGetbymobileResponse(SendDingtalk s, String accessToken) {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getbymobile");
        OapiV2UserGetbymobileRequest req = new OapiV2UserGetbymobileRequest();