釘釘機器人 “創建并投放卡片 接口?”? ?可以? 發送折線圖、柱狀圖
官方文檔:創建并投放卡片 - 釘釘開放平臺
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_tokenreturn 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