即时消息告警工具(一)企业微信
注册企业微信
填写企业微信部分信息
1.打开一下网站,填写部分信息。
https://work.weixin.qq.com/wework_admin/register_wx?from=myhome_baidu
2.登陆进去后,找到"我的企业" 下拉找到 "企业ID/corpid" 这个记录一下后面会用到
在企业微信内生成应用
1.找到"应用管理" ==>> "自建" ==>> "创建应用"
2.随便选择一张图片,修改图片的大小为 750*750 创建应用需要图标
3.选择上一步创建的图片,填写应用信息
4.记录 "Agentid" "Secret" 后面会用
5.配置企业可用IP
里面需要企业的域名,在域名下放一个文件
上面这个配置不通过一直会报错:
{"errcode":60020,"errmsg":"not allow to access from your ip, hint: [***], from ip: ***, more info at https://open.work.weixin.qq.com/devtool/query?e=60020"}
调用代码
package test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONObject;
public class SendMessage {
public static String http_get_value_strng(String url,String errcode,String errcode_value,String value) throws Exception {
HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
con.setRequestMethod("GET");
int responseCode = con.getResponseCode();
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
String response = "";
String line;
while ((line = br.readLine()) != null) {
response += line;
}
br.close();
if(responseCode == 200){
JSONObject jsonObject = new JSONObject(response);
if(jsonObject.getString(errcode).equals(errcode_value)){
String access_token = jsonObject.getString(value);
System.out.println(access_token);
return access_token;
}
System.out.println(response);
}
return "";
}
public static void http_post_value_null(String url,String requestBody,String errcode,String errcode_value) throws Exception {
HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json");
con.setDoOutput(true);
OutputStream os = con.getOutputStream();
os.write(requestBody.getBytes());
os.flush();
os.close();
int responseCode = con.getResponseCode();
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
String response = "";
String line;
while ((line = br.readLine()) != null) {
response += line;
}
br.close();
if(responseCode == 200){
JSONObject jsonObject = new JSONObject(response);
if(jsonObject.getString(errcode).equals(errcode_value)){
System.out.println("调用成功");
return ;
}
}
System.out.println(response);
System.out.println("调用失败");
}
public static String get_access_token(String corpid,String secret) throws Exception {
String url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid="+corpid+"&corpsecret="+secret;
return http_get_value_strng(url,"errcode","0","access_token");
}
public static void send_message(String touser,String content) throws Exception {
String agentid = "agentid";
String access_token = get_access_token("替换成corpid","替换成Secret");
String url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?debug=1&access_token="+access_token;
String requestBody = "{\"touser\":\""+touser+"\",\"msgtype\":\"text\",\"agentid\":"+agentid+"," + "\"text\":{\"content\":\""+content+"\"}}";
System.out.println(requestBody);
http_post_value_null(url,requestBody,"errcode","0");
}
public static void main(String[] args) throws Exception {
send_message("替换成发送的用户名称就行","发送的内容\r\n告警测试最终版1");
}
}
搬砖多年终不得要领,遂载源码看之望得真经。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· 清华大学推出第四讲使用 DeepSeek + DeepResearch 让科研像聊天一样简单!
· 实操Deepseek接入个人知识库
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库