Java 推送企业微信(新版本)消息

1、企业微信地址,注册账号,新建企业

2、3个关键参数获取

CORPID:企业ID
CORPSECRET:应用中的Secret
AGENTID:消息发送应用id

3、IP白名单配置

①新版本需添加IP白名单校验,否则会返回60020,错误内容如下:

微信告警返回值:{"errcode":60020,"errmsg":"not allow to access from your ip, hint: [1695863164339604070459795], from ip: 221.182.171.162, more info at https://open.work.weixin.qq.com/devtool/query?e=60020"}

②针对60020错误,需在应用管理-》自建应用中添加IP白名单

③添加IP白名单之前需设置可信域名或者设置接收消息服务器URL

④本次通过设置可信域名的方式,填写域名之前需先申请校验域名,点击申请校验域名将文件下载到本地:以wx.qq.com域名为例,假设系统生成的校验文件是WW_verify_7rG3kjVbXHngiald.txt,则下载该文件并放到wx.qq.com的根目录下,然后在浏览器打开以下链接检查是否能正常访问:https://wx.qq.com/WW_verify_7rG3kjVbXHngiald.txt 或 http://wx.qq.com/WW_verify_7rG3kjVbXHngiald.txt 。(我所使用的域名为nginx作为代理到Windows服务器,所以将系统生成的文件放在nginx根目录下【windows默认nginx根目录为html】)

⑤完成设置后,配置好域名和IP白名单即可成功。

4、应用消息发送代码示例

package com.......

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.sunsheen.dao.UserAlarmInChargeOfDao;
import com.sunsheen.util.ObjectUtils;

public class WechatAPI {
	//获取token的url
	public static String token_url="https://qyapi.weixin.qq.com/cgi-bin/gettoken";
	//发送应用消息url
	public static String app_url="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=";

	//公司的id
	public static String corpid="";
	//业务全流程告警
	public static String AgentId="";
	public static String Secret="";

	
	public static String sendWechatMessage(String name,String value,String systemName,String id,String state){
		String result = "-1";
		try {
			if (ObjectUtils.isNullOrEmpty(name)||ObjectUtils.isNullOrEmpty(value)||ObjectUtils.isNullOrEmpty(systemName)||ObjectUtils.isNullOrEmpty(id)) {
				result = "0";
			}else {
				//消息内容
				String content = "消息内容";
				
		        System.out.println("content:"+content);
		        
		        //	获取token
		        String token = getToken(corpid,Secret);
		        if (ObjectUtils.isNullOrEmpty(token)) {
		        	System.out.println("微信告警发送失败!");
					result = "-2";
				}else {
			        String sendAppMessage = sendAppMessage(content,token,AgentId);
			        JSONObject resultJson=JSONObject.parseObject(sendAppMessage);
			        System.out.println("resultJson微信告警返回值:"+resultJson);
			        if(resultJson.getString("errmsg").equals("ok")){
						System.out.println("微信告警发送成功!");
						result = "1";
					}
					else{
						result = "-1";
						System.out.println("微信告警发送失败!");
					}
				}
			}
			return result;
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return result;
		}
	}
	//获取token
	public static String getToken(String corpid,String corpsecret){
		String token="";
		HashMap<String, Object> param=new HashMap<String, Object>();
		param.put("corpid", corpid);
		param.put("corpsecret", corpsecret);
		String jsonContent=JSON.toJSONString(param);
		String result=Util.doPostForJson(token_url, jsonContent);
		JSONObject resultJson=JSONObject.parseObject(result);
		if(resultJson.getString("errmsg").equals("ok")){
			token=resultJson.getString("access_token");
		}
		return token;
	}
	
	//发送应用消息
	public static String sendAppMessage(String msg,String token,String agentid){
		HashMap<String, Object> map=new HashMap<>();
		//用户id列表,多个用户用|分隔,@all表示该应用下的全部用户
		map.put("touser", "@all");
		//消息类型,此时固定为:text
		map.put("msgtype", "text");
		//应用id
		map.put("agentid",agentid);	
		
		//消息内容,最长不超过2048个字节,超过将截断
		HashMap<String, Object> text=new HashMap<>();
		text.put("content", msg);
		map.put("text", text);
		
		//表示是否是保密消息,0表示否,1表示是,默认0
		map.put("safe",0);	
		//表示是否开启id转译,0表示否,1表示是,默认0。仅第三方应用需要用到,企业自建应用可以忽略。
		map.put("enable_id_trans",0);	
		//表示是否开启重复消息检查,0表示否,1表示是,默认0
		map.put("enable_duplicate_check",0);	
		//表示是否重复消息检查的时间间隔,默认1800s,最大不超过4小时
		//map.put("duplicate_check_interval",1800);	
		
		String jsonContent=JSON.toJSONString(map);
		String result =Util.doPostForJson(app_url+token, jsonContent);
		return result;
	}
}

4、返回结果示例

微信告警返回值:{"errcode":0,"errmsg":"ok","msgid":"WpLDpQFMGSE843kRbNhgXRQ4cWlq9TlhfYReZgyoYss04KyEJkbyhleQ2PfC2A7JSUByLMThttPBK6QTn3Zfff"}
 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值