官方文档
个人也可以创建团队(此功能非必须)。
发启一个群聊后,就可以在设置里添加自定义机器人。
https://open.dingtalk.com/document/robots/custom-robot-access/title-72m-8ag-pqw
https://open.dingtalk.com/document/orgapp/custom-bot-to-send-group-chat-messages?spm=ding_open_doc.document.0.0.453c5e59xJitRD
自定义机器人发送群聊消息
1 2 3 4 5 6 7 8 9 10 11 12 13
| <dependencies> <dependency> <groupId>com.aliyun</groupId> <artifactId>alibaba-dingtalk-service-sdk</artifactId> <version>2.0.0</version> </dependency>
<dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.11</version> </dependency> </dependencies>
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
| import com.dingtalk.api.DefaultDingTalkClient; import com.dingtalk.api.DingTalkClient; import com.dingtalk.api.request.OapiRobotSendRequest; import com.dingtalk.api.response.OapiRobotSendResponse; import com.taobao.api.ApiException; import org.apache.commons.codec.binary.Base64;
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;
public class customRobotGroupMessage {
public static final String CUSTOM_ROBOT_TOKEN = "<your custom robot token>";
public static final String USER_ID= "<you need @ group user's userId>";
public static final String SECRET = "<your custom robot SECRET>";
public static void main(String[] args) { try { Long timestamp = System.currentTimeMillis(); System.out.println(timestamp); String secret = SECRET; String stringToSign = timestamp + "\n" + secret; Mac mac = Mac.getInstance("HmacSHA256"); mac.init(new SecretKeySpec(secret.getBytes("UTF-8"), "HmacSHA256")); byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8")); String sign = URLEncoder.encode(new String(Base64.encodeBase64(signData)),"UTF-8"); System.out.println(sign);
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/robot/send?sign="+sign+"×tamp="+timestamp); OapiRobotSendRequest req = new OapiRobotSendRequest();
OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text(); text.setContent("钉钉,让进步发生"); OapiRobotSendRequest.At at = new OapiRobotSendRequest.At(); at.setAtUserIds(Arrays.asList(USER_ID)); req.setMsgtype("text"); req.setText(text); req.setAt(at); OapiRobotSendResponse rsp = client.execute(req, CUSTOM_ROBOT_TOKEN); System.out.println(rsp.getBody()); } catch (ApiException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (InvalidKeyException e) { throw new RuntimeException(e); } } }
|