用于钉钉机器人推送方式消息,已测试通过。

//此代码时间20200522 ,用于机器人推送方式消息,已测试通过。

package com.dingding.test;

import java.net.URLEncoder;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiRobotSendRequest;
import com.dingtalk.api.response.OapiRobotSendResponse;
public class DingUtil {
    
    //这个代码已经测试成功了。
    public static void main(String[] args) throws Exception
     {
        //机器人调用成功
        String dingDingToken="https://oapi.dingtalk.com/robot/send?access_token=c18ad461783e5c4aa7b49493aaa69081ec5373106dd350be9e1a156d0b82e0d6";
        String dingDingToken2=dingDingToken+"&"+System.currentTimeMillis()+"$"+gettimestamp();
        System.out.println("签名2:"+dingDingToken2);
         DingTalkClient client = new DefaultDingTalkClient(dingDingToken2);
         OapiRobotSendRequest request = new OapiRobotSendRequest();
//         request.setMsgtype("text");
//         OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
//         text.setContent("财务通知 定时推送消息");
//         request.setText(text);
//         OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
//         at.setAtMobiles(Arrays.asList("13413011401"));
//         request.setAt(at);
//         
         //发送链接
//         request.setMsgtype("link");
//         OapiRobotSendRequest.Link link = new OapiRobotSendRequest.Link();
//         link.setMessageUrl("https://www.dingtalk.com/");
//         link.setPicUrl("https://img.alicdn.com/tfs/TB1bB9QKpzqK1RjSZFoXXbfcXXa-576-96.png");
//         link.setTitle("时代的火车向前开");
//         link.setText("财务通知 这个即将发布的新版本,创始人xx称它为红树林。而在此之前,每当面临重大升级,产品经理们都会取一个应景的代号,这一次,为什么是红树林");
//         request.setLink(link);
         
         //markdown  图文并茂
         request.setMsgtype("markdown");
         OapiRobotSendRequest.Markdown markdown = new OapiRobotSendRequest.Markdown();
         markdown.setTitle("杭州天气");
         markdown.setText("#### 财务通知 杭州天气 @156xxxx8827\n" +
                 "> 9度,西北风1级,空气良89,相对温度73%\n\n" +
                 "> ![screenshot](https://gw.alicdn.com/tfs/TB1ut3xxbsrBKNjSZFpXXcXhFXa-846-786.png)\n"  +
                 "> ###### 10点20分发布 [天气](http://www.thinkpage.cn/) \n");
         request.setMarkdown(markdown);
         
         OapiRobotSendResponse response = client.execute(request);
         //response.
         System.out.println(response.isSuccess()+"是否成功"+response.getCode());

     }
    
    public static String gettimestamp () throws Exception
    {
           Long timestamp = System.currentTimeMillis();
           String secret = "this is 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");
            return sign;
    }
 
}
 

你可能感兴趣的:(用于钉钉机器人推送方式消息,已测试通过。)