【接口设计】企业微信消息推送


代码链接:https://github.com/AlenYang123456/corpwx
复制代码

package com.gabriel.corpwx.task;
import com.alibaba.fastjson.JSON;
import com.gabriel.corpwx.comom.WxMsgConstant;
import com.gabriel.corpwx.comom.WxMsgTypeEnum;
import com.gabriel.corpwx.config.CorpWechatConfig;
import com.gabriel.corpwx.service.RedisService;
import com.gabriel.corpwx.util.DateUtils;
import com.gabriel.corpwx.util.HttpClientUtils;
import com.gabriel.corpwx.util.LocalDateTimeUtils;
import com.gabriel.corpwx.vo.resp.WxMessageResp;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: yq
 * @date: 2019/12/13 17:35
 * @discription
 */
@Slf4j
@Component
public class WxMessageTask {

    @Autowired
    private CorpWechatConfig corpWechatConfig;

    @Autowired
    private RedisService redisService;


    /**
    * @Description: 企业微信消息推送 每天9点推送一次
    * @Param: [] Scheduled(cron = "0 0 9 1/1 * ? *") 正式环境使用 每天9点推送消息
    * @return: void
    * @Date: 2019-12-17
    */
    //@Scheduled(cron = "0 */5 * * * ?") //每5分钟发送一次 调试用
    @Scheduled(cron = "0 0 9 1/1 * ?")
    public void pushMessage(){
        log.info("消息发送  开始=> 时间:[{}]", LocalDateTimeUtils.formatTime(LocalDateTime.now(), DateUtils.YYYY_MM_DD_HH_MM_SS));
        //1.获取access_token -这里我是将access_token放到了redis缓存并添加失效时间,使用时取出来即可
        String accessToken = (String) redisService.get(WxMsgConstant.TOKEN);
        if (StringUtils.isBlank(accessToken)){
            //redis里面有重试
            log.info("消息推送:access_token获 取失败");
            throw new RuntimeException("access_token无法获取");
        }

        //2.构造请求
        String url = String.format(corpWechatConfig.getSendMessageUrl(), accessToken);
        //请求头
        Map<String,String> headers=new HashMap(16);
        headers.put("Content-Type", "application/json; charset=UTF-8");
        //请求体
        Map<String,Object> params=new HashMap<>(16);
        //TODO 暂时用自己的userId测试
        params.put(WxMsgConstant.TO_USER, "YangQiang");
        params.put(WxMsgConstant.MSG_TYPE, WxMsgTypeEnum.TEXT.getType());
        params.put(WxMsgConstant.AGENT_ID, corpWechatConfig.getAgentId());
        Map<String,String> contentMap =new HashMap<>(16);
        contentMap.put(WxMsgConstant.CONTENT, "测试消息-请及时查看");
        params.put(WxMsgConstant.TEXT, contentMap);
        log.info("企业微信消息推送==>> body:[{}],header:[{}],url:[{}]",params,headers,url);
        String result = HttpClientUtils.sendPostRequest(url, headers, JSON.toJSONString(params));

        if (StringUtils.isBlank(result)){
            throw new RuntimeException("消息发送失败");
        }
        WxMessageResp wxMessageResp = JSON.parseObject(result, WxMessageResp.class);
        if (wxMessageResp.getErrcode()!=0){
            log.info("消息发送失败 WxMessageResp:[{}],时间:[{}]",wxMessageResp,
                    LocalDateTimeUtils.formatTime(LocalDateTime.now(), DateUtils.YYYY_MM_DD_HH_MM_SS));
            throw  new RuntimeException("消息发送失败");
        }
        log.info("消息发送成功=> wxMessageResp[{}],时间:[{}]",wxMessageResp,
                LocalDateTimeUtils.formatTime(LocalDateTime.now(), DateUtils.YYYY_MM_DD_HH_MM_SS));
    }

    /**
     * 手动测试的方法
     * @param args
     */
    public static void main(String[] args) {
        String url = String.format("https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s",
                "Z8fUMY3Z_BEhC8qFpXdLhjyu0B6NeTtgI3CRLzht-_FJugFS9705NFvuHmpT8kYezLY7C_sEwwr15UeN8LTcdrckIsKaIaSxhKka3UN_NFb2hvahUSBBnuYNMr-kGOZU1vv2TBIXDs5siXqI52Dq9Rw7cnTmq5t07soGu4qEvgIwwfNwp4CkBW1utf4giEVCDfWXn9rlAKZzo3UZ1Ie9uw");
        Map<String,String> headers=new HashMap(16);
        headers.put("Content-Type", "application/json; charset=UTF-8");

        Map<String,Object> params=new HashMap<>();
        params.put(WxMsgConstant.TO_USER, "XXXX");
        params.put(WxMsgConstant.MSG_TYPE,"text");
        params.put(WxMsgConstant.AGENT_ID, 1000002);
        Map<String,String> contentMap =new HashMap<>(16);
        contentMap.put(WxMsgConstant.CONTENT, "测试消息-请及时查看");
        params.put(WxMsgConstant.TEXT, contentMap);

        String result = HttpClientUtils.sendPostRequest(url, headers, JSON.toJSONString(params));
        if (StringUtils.isBlank(result)){
            throw new RuntimeException("消息发送失败");
        }
        WxMessageResp wxMessageResp = JSON.parseObject(result, WxMessageResp.class);
        if (wxMessageResp.getErrcode()!=0){
            log.info("消息发送失败 WxMessageResp:[{}]",wxMessageResp);
            throw  new RuntimeException("消息发送失败");
        }
        log.info("消息发送成功:[{}]",wxMessageResp);
    }

}
复制代码

 

相关参数配置

复制代码
@Data
@Component
@ConfigurationProperties(prefix = "corpwechat")
public class CorpWechatConfig {

    /** 企业Id */
    private String corpid;

    /** 企业号密钥 */
    private String corpsecret;
    
    /** 企业应用的id */
    private Integer agentId;
    
    /** 消息推送url */
    private String sendMessageUrl;
}
复制代码

微信消息推送相关常量

复制代码
public class WxMsgConstant {
    
    /** 接收消息的成员 */
    public  static final String TO_USER="toUser";
    
    /** 消息类型 */
    public static final String  MSG_TYPE="msgtype";

    /** 企业应用的id */
    public  static final String AGENT_ID="agentid";

    /** 消息内容 */
    public  static final String CONTENT="content";

    /** 微信access_token */
    public static final String TOKEN="WX_ACCESS_TOKEN";
}


复制代码
posted @   听风是雨  阅读(2373)  评论(1编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
/* 看板娘 */
点击右上角即可分享
微信分享提示