当前位置: 移动技术网 > IT编程>开发语言>Java > java实现钉钉机器人消息推送的示例代码

java实现钉钉机器人消息推送的示例代码

2020年03月09日  | 移动技术网IT编程  | 我要评论

正点闹钟官网,qq堂双开,广西快乐双彩走势图

先建个钉钉群,并加好机器人

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

此时,机器人已经添加完毕,接下来编写我们连接机器人小哥的代码

import com.alibaba.fastjson.json;
import com.google.common.collect.lists;
import com.google.common.collect.maps;
import java.util.list;
import java.util.map;

/**
 * @author yanghao
 * @version dingtalktest.java, v 0.1 2019-03-29 11:36
 */
public class dingtalktest {

 public static void main(string[] args){

 try {
  //钉钉机器人地址(配置机器人的webhook)
  string dingurl = "https://oapi.dingtalk.com/robot/send?access_token=............";

  //是否通知所有人
  boolean isatall = false;
  //通知具体人的手机号码列表
  list<string> mobilelist = lists.newarraylist();

  //钉钉机器人消息内容
  string content = "小哥,你好!";
  //组装请求内容
  string reqstr = buildreqstr(content, isatall, mobilelist);

  //推送消息(http请求)
  string result = httputil.postjson(dingurl, reqstr);
  system.out.println("result == " + result);

 }catch (exception e){
  e.printstacktrace();

 }

 }

 /**
 * 组装请求报文
 * @param content
 * @return
 */
 private static string buildreqstr(string content, boolean isatall, list<string> mobilelist) {
 //消息内容
 map<string, string> contentmap = maps.newhashmap();
 contentmap.put("content", content);

 //通知人
 map<string, object> atmap = maps.newhashmap();
 //1.是否通知所有人
 atmap.put("isatall", isatall);
 //2.通知具体人的手机号码列表
 atmap.put("atmobiles", mobilelist);

 map<string, object> reqmap = maps.newhashmap();
 reqmap.put("msgtype", "text");
 reqmap.put("text", contentmap);
 reqmap.put("at", atmap);

 return json.tojsonstring(reqmap);
 }

}

运行结果如下:

result == {"errmsg":"ok","errcode":0}

钉钉群显示消息:

在这里插入图片描述

ok,简单的消息推送,这就完成了!

我们再来测试一下通知所有人和通知具体人

将isatall更改为true

//是否通知所有人
boolean isatall = true;
//通知具体人的手机号码列表
list<string> mobilelist = lists.newarraylist();

在这里插入图片描述

增加通知人号码列表(注:isatall和mobilelist 不能同时生效)

//是否通知所有人
boolean isatall = false;
//通知具体人的手机号码列表
list<string> mobilelist = lists.newarraylist();
mobilelist.add("182********");

在这里插入图片描述

再来测试一下特殊符号

换行标识符

/**
 * 换行标识符
 */
private static final string newline = "\n";

//钉钉机器人消息内容
//string content = "小哥,你好!";
stringbuffer sb = new stringbuffer();
sb.append("小哥,你好!")
 .append(newline)
 .append("看会书");

string content = sb.tostring();

在这里插入图片描述

emoji图片

先获取emoji图片的unicode编码

在这里插入图片描述

编写代码如下:

/**
 * 苹果unicode编码
 */
private static final string apple = "\ud83c\udf4e";

//钉钉机器人消息内容
//string content = "小哥,你好!";
stringbuffer sb = new stringbuffer();
sb.append("小哥,你好!")
 .append(newline)
 .append("看会书")
 .append(newline)
 .append("吃个").append(apple);

string content = sb.tostring();

在这里插入图片描述

通常在我们的项目中,作为一些告警加入,方便且实用
很有意思的钉钉机器人,很多实用技巧,可以深入去探索一波!

更新于2019-12-05

很多小伙伴留言咨询http请求,这边给大家2个http请求代码

1. maven项目

添加依赖

<!--糊涂工具-->
<dependency>
 <groupid>cn.hutool</groupid>
 <artifactid>hutool-all</artifactid>
 <version>4.0.12</version>
</dependency>

http请求代码

private static final int timeout = 10000; 

public static string postjson(string url, string reqstr) {
 string body = null;
 try {
  body = httprequest.post(url).body(reqstr).timeout(timeout).execute().body();
 } catch (exception e) {
  e.printstacktrace();
 }
 return body;
}

2. 非maven项目

添加jar包
httpclient-xxx.jar
commons-logging-xxx.jar

http请求代码

public static string postjson(string url, string body) {
  // 创建httpclient对象
  closeablehttpclient httpclient = createcustomclient();
  closeablehttpresponse response = null;
  string resultstring = null;
  try {
   // 创建http post请求
   httppost httppost = new httppost(url);
   httppost.addheader("content-type", "application/json");

   if (body != null) {
    httppost.setentity(new stringentity(body, "utf-8"));
   }
   // 执行http请求
   response = httpclient.execute(httppost);
   resultstring = entityutils.tostring(response.getentity(), "utf-8");
  } catch (exception e) {
   e.printstacktrace();
  } finally {
   try {
    if (response != null) {
     response.close();
    }
   } catch (exception e) {
    e.printstacktrace();
   }
  }

  return resultstring;
 }

 public static closeablehttpclient createcustomclient() {
  requestconfig defaultrequestconfig = requestconfig.custom()
    .setsockettimeout(120 * 1000)
    .setconnecttimeout(120 * 1000)
    .setconnectionrequesttimeout(120 * 1000)
    .setstaleconnectioncheckenabled(true)
    .build();

  return httpclients.custom().setdefaultrequestconfig(defaultrequestconfig).build();
 }

方法仅供参考,项目里面有现成的http请求,可以直接用!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网