mirror of
https://gitee.com/shuto-github/intranet_app_manager.git
synced 2026-05-27 00:00:14 +08:00
添加钉钉发送工具
This commit is contained in:
@@ -36,6 +36,8 @@ dependencies {
|
||||
compile group: 'commons-io', name: 'commons-io', version: '2.6'
|
||||
compile group: 'com.jcraft', name: 'jzlib', version: '1.1.3'
|
||||
compile group: 'org.freemarker', name: 'freemarker', version: '2.3.28'
|
||||
compile group: 'com.squareup.okhttp3', name: 'okhttp', version: '4.0.1'
|
||||
compile group: 'com.alibaba', name: 'fastjson', version: '1.2.59'
|
||||
developmentOnly 'org.springframework.boot:spring-boot-devtools'
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
}
|
||||
|
||||
@@ -36,6 +36,9 @@ public class App {
|
||||
// 包列表
|
||||
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "app")
|
||||
private List<Package> packageList;
|
||||
// webHook列表
|
||||
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "app")
|
||||
private List<WebHook> webHookList;
|
||||
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
|
||||
// 当前包
|
||||
@JoinColumn(name = "currentID",referencedColumnName = "id")
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package org.yzr.model;
|
||||
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.hibernate.annotations.GenericGenerator;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "tb_web_hook")
|
||||
@Setter
|
||||
@Getter
|
||||
public class WebHook {
|
||||
// 主键
|
||||
@Id
|
||||
@GeneratedValue(generator = "system-uuid")
|
||||
@GenericGenerator(name = "system-uuid", strategy = "uuid")
|
||||
@Column(length = 32)
|
||||
private String id;
|
||||
|
||||
private String name;
|
||||
|
||||
private String url;
|
||||
|
||||
@ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.LAZY)
|
||||
@JoinColumn(name="appId")
|
||||
private App app;
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package org.yzr.utils;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import okhttp3.*;
|
||||
import org.springframework.http.converter.json.GsonBuilderUtils;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class DingdingUtils {
|
||||
|
||||
private static OkHttpClient client = new OkHttpClient();
|
||||
/**
|
||||
* 发送钉钉消息
|
||||
* @param jsonString 消息内容
|
||||
* @param webhook 钉钉自定义机器人webhook
|
||||
* @return
|
||||
*/
|
||||
public static boolean sendToDingding(String jsonString, String webhook) {
|
||||
try{
|
||||
String type = "application/json; charset=utf-8";
|
||||
RequestBody body = RequestBody.create(MediaType.parse(type), jsonString);
|
||||
Request.Builder builder = new Request.Builder().url(webhook);
|
||||
builder.addHeader("Content-Type", type).post(body);
|
||||
|
||||
Request request = builder.build();
|
||||
Response response = client.newCall(request).execute();
|
||||
String string = response.body().string();
|
||||
System.out.println(String.format("send ding message:%s", string));
|
||||
JSONObject result = JSONObject.parseObject(jsonString);
|
||||
System.out.println(result.getInteger("errcode"));
|
||||
return true;
|
||||
}catch (Exception e){
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean sendMarkdown() {
|
||||
Map<String, Object> markdown = new HashMap<>();
|
||||
markdown.put("title", "时代的火车向前开");
|
||||
markdown.put("text", "[ticket(Android)更新](https://www.baidu.com) \n\n  \n\n 链接:[https://fir.im/cq7f](https://fir.im/cq7f) \n\n 版本: 1.0 (Build: 1)");
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
jsonObject.put("msgtype", "markdown");
|
||||
jsonObject.put("markdown", markdown);
|
||||
|
||||
return sendToDingding(jsonObject.toJSONString(), "https://oapi.dingtalk.com/robot/send?access_token=a2030da1a934473cc8f950d342888321387ed65f92bcf16a6bf4c048903e5a0e");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user