-
创建群机器人:对于任意我们所在的群,我们都可以创建自己的机器人,不需要群主权限,每个机器人信息只能自己看到。 我们项目通过对应的链接调用机器人发送消息。
-
创建对应的鉴权方式, 关键字只要消息内容携带即可,签名需要对应秘钥对timestamp加密生成sign校验,链接后需要传递timestamp和sign
-
接口实现
package com.adutiaoxun.study.service.impl;import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.adutiaoxun.study.common.Constants;
import com.adutiaoxun.study.domain.MessageRequest;
import com.adutiaoxun.study.enums.validateEnums.group.MessageTypeEnum;
import com.adutiaoxun.study.service.MessageSendService;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
import com.taobao.api.ApiException;
import lombok.Getter;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.List;/*** DingTalkSendService** @author yejianjun* @CreateDate 2022/10/25*/
@Service
@Getter
public class DingTalkSendService implements MessageSendService {public String channel = "DingTalk";@Value("${dingTalk.baseUrl}")private String baseUrl;@Value("${dingTalk.accessToken}")private String accessToken;@Value("${dingTalk.secret}")private String secret;@Overridepublic void sendMessage(MessageRequest messageRequest)throws ApiException, NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException {String messageType = messageRequest.getMessageType();Long timestamp = System.currentTimeMillis();String stringToSign = timestamp + "\n" + secret;Mac mac = Mac.getInstance(Constants.HMAC_SHA256);mac.init(new SecretKeySpec(secret.getBytes(Constants.UTF_8), Constants.HMAC_SHA256));byte[] signData = mac.doFinal(stringToSign.getBytes(Constants.UTF_8));String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)), Constants.UTF_8);String url = StrUtil.concat(true, baseUrl, accessToken, "×tamp=", timestamp.toString(), "&sign=", sign);DefaultDingTalkClient client = new DefaultDingTalkClient(url);OapiRobotSendRequest request = new OapiRobotSendRequest();request.setMsgtype(messageType);buildAt(request, messageRequest);buildContentByMsgType(request, messageRequest);List<String> atMobiles = StrUtil.split(messageRequest.getAtMobiles(), StrUtil.COMMA);OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();at.setIsAtAll(messageRequest.getIsAtAll());at.setAtMobiles(atMobiles);request.setAt(JSONUtil.toJsonStr(at));OapiRobotSendResponse response = client.execute(request);System.out.println(response);}private void buildAt(OapiRobotSendRequest request, MessageRequest messageRequest) {if (StrUtil.equalsAny(messageRequest.getMessageType(), MessageTypeEnum.MARKDOWN.getType(), MessageTypeEnum.TEXT.getType()) &&StrUtil.isNotBlank(messageRequest.getAtMobiles())){OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();at.setIsAtAll(messageRequest.getIsAtAll());at.setAtMobiles(Arrays.asList(messageRequest.getAtMobiles().split(StrUtil.COMMA)));request.setAt(at);}}private void buildContentByMsgType(OapiRobotSendRequest request, MessageRequest messageRequest) {String msgType = messageRequest.getMessageType();switch (MessageTypeEnum.getByType(msgType)) {case MARKDOWN:OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();markdown.setText(messageRequest.getMessageBody());markdown.setTitle(messageRequest.getTitle());request.setMarkdown(markdown);break;case TEXT:OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();text.setContent(messageRequest.getMessageBody());request.setText(text);break;case ACTION_CARD:OapiRobotSendRequest.Actioncard actionCard = new OapiRobotSendRequest.Actioncard();actionCard.setTitle(messageRequest.getMessageBody());actionCard.setText(messageRequest.getMessageBody());actionCard.setBtnOrientation("1");OapiRobotSendRequest.Btns okBtn = new OapiRobotSendRequest.Btns();okBtn.setTitle("我来看看");okBtn.setActionURL("https://gitee.com/ye-jianjun");OapiRobotSendRequest.Btns noBtn = new OapiRobotSendRequest.Btns();noBtn.setTitle("什么玩意儿");actionCard.setBtns(Arrays.asList(okBtn, noBtn));request.setActionCard(actionCard);break;default:break;}}
}
消息体详情见:https://open.dingtalk.com/document/robots/custom-robot-access