1.概述
上文介绍了使用钉钉机器人推送离线和实时任务出错告警,值班人员信息推送使用DataWorks自动推送,但是DataWorks自动推送不能@指定人员,只能@all和不@,这里使用钉钉API,写代码定时向群里推送相应值班人员信息。
2.实现
1.创建钉钉机器人
1.在所要推送消息的钉钉群内创建自定义机器人
2.输入机器人名称,这里采用关键字,推送的内容中包含以下关键词均能成功推送消息,否则推送失败
3.保存好Webhook地址,代码就是向这个地址推送
2.Java代码
1.pom依赖导入
<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>org.example</groupId><artifactId>dataworks</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>com.aliyun</groupId><artifactId>dataworks_public20200518</artifactId><version>4.3.9</version><scope>provided</scope></dependency><dependency><groupId>com.aliyun</groupId><artifactId>alibaba-dingtalk-service-sdk</artifactId><version>2.0.0</version></dependency></dependencies><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.6.1</version><!--所有的编译都依照JDK1.8来搞--><configuration><source>1.8</source><target>1.8</target></configuration></plugin><plugin><artifactId>maven-assembly-plugin</artifactId><configuration><descriptorRefs><descriptorRef>jar-with-dependencies</descriptorRef></descriptorRefs><archive><manifest><!--注意:要和自己建的全类名一致。--><mainClass>cn.xxx.dataworks.DutyOnDataWorks</mainClass></manifest></archive></configuration><executions><execution><id>make-assembly</id><phase>package</phase><goals><goal>single</goal></goals></execution></executions></plugin></plugins></build></project>
2.编写工具类,用于获取Webhook地址,日期,值班人姓名手机号、消息体等
packagecn.xxx.dataworks.utils; importjava.text.ParseException; importjava.text.SimpleDateFormat; importjava.util.Calendar; importjava.util.Date; /*** 工具类*/publicclassUtils { /*** 钉钉机器人WebHook* @return*/publicstaticStringgetWebHook(){ //正式机器人Webhook地址Stringurl="xxx"; returnurl; } /*** 发送消息体* @return*/publicstaticStringgetContent() throwsParseException { Stringtxt="DataWorks值班提醒:"+"\n"+"> "+getData()+"("+getWeekOfDate()+") "+"00:00开始 23:59结束"+"\n"+"> 值班表: 每日任务运维"+"\n"+"> 值班人员:"+getShiftPersonnel() ; returntxt; } /*** 获取当前日期是星期几+1,因为是当天发送明天的消息* @return 当前日期是星期几+1*/publicstaticStringgetWeekOfDate() { //正常顺序String[] weekDays= { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" }; //星期+1顺序String[] weekDays2= { "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日" }; Datedate=newDate(System.currentTimeMillis()); Calendarcal=Calendar.getInstance(); cal.setTime(date); intw=cal.get(Calendar.DAY_OF_WEEK) -1; if(w<0){ w=0; } returnweekDays2[w]; } /*** 获取日期+1* @return*/publicstaticStringgetData(){ SimpleDateFormatformatter=newSimpleDateFormat("yyyy年MM月dd日"); Datedate=newDate(System.currentTimeMillis()); Calendarc=Calendar.getInstance(); c.setTime(date); c.add(Calendar.DAY_OF_MONTH,1); Stringday1=formatter.format(c.getTime()); returnday1; } /*** 获取值班人员名称(周一至周日)* @return 姓名*/publicstaticStringgetShiftPersonnel() { StringweekOfDate=getWeekOfDate(); if("星期一".equals(weekOfDate)){ return"xxx"; } elseif("星期二".equals(weekOfDate)){ return"xxx"; } elseif("星期三".equals(weekOfDate)){ return"xxx"; } elseif("星期四".equals(weekOfDate)){ return"xxx"; } elseif("星期五".equals(weekOfDate)){ return"xxx"; } elseif("星期六".equals(weekOfDate)){ return"xxx"; } elseif("星期日".equals(weekOfDate)){ return"xxx"; } else{ return""; } } /*** 获取值班人员联系方式* @return 手机号码*/publicstaticStringgetPhoneNumber() { StringshiftPersonnel=getShiftPersonnel(); if("xxx".equals(shiftPersonnel)){ return"xxx"; } elseif("xxx".equals(shiftPersonnel)){ return"xxx"; } elseif("xxx".equals(shiftPersonnel)){ return"xxx"; } elseif("xxx".equals(shiftPersonnel)){ return"xxx"; } elseif("xxx".equals(shiftPersonnel)){ return"xxx"; } elseif("xxx".equals(shiftPersonnel)){ return"xxx"; } else{ return""; } } }
3.编写主类
packagecn.xxx.dataworks; importcn.xxx.dataworks.utils.Utils; importcom.dingtalk.api.DefaultDingTalkClient; importcom.dingtalk.api.DingTalkClient; importcom.dingtalk.api.request.OapiRobotSendRequest; importcom.dingtalk.api.response.OapiRobotSendResponse; importcom.taobao.api.ApiException; importjava.text.ParseException; importjava.util.Arrays; publicclassDutyOnDataWorks { publicstaticvoidmain(String[] args) throwsApiException, ParseException { //获取钉钉机器人WebHookDingTalkClientclient=newDefaultDingTalkClient(Utils.getWebHook()); //创建请求OapiRobotSendRequestrequest=newOapiRobotSendRequest(); //消息类型,此时固定为:text。当前自定义机器人支持文本 (text)、链接 (link)、markdown(markdown)、ActionCard、FeedCard消息类型request.setMsgtype("text"); //指定消息内容OapiRobotSendRequest.Texttext=newOapiRobotSendRequest.Text(); text.setContent(Utils.getContent()); request.setText(text); //被@人的手机号。在content里添加@人的手机号,且只有在群内的成员才可被@,非群内成员手机号会被脱敏。OapiRobotSendRequest.Atat=newOapiRobotSendRequest.At(); at.setAtMobiles(Arrays.asList(Utils.getPhoneNumber())); // isAtAll类型如果不为Boolean,请升级至最新SDKat.setIsAtAll(false); request.setAt(at); //发送请求OapiRobotSendResponseexecute=client.execute(request); } }
4.打成jar包,放到服务器设置定时运行,即可每日收到值班人员信息
3.总结
以上就是通过java代码实现向钉钉群推送消息的过程,DataWorks可以邮件推送,淡然代码也是可以实现的,这里就不做叙述了。
拜了个拜