【接口設計】企業微信消息推送

 
  
代碼連接: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); } }

 

相關參數配置java

@Data @Component @ConfigurationProperties(prefix = "corpwechat") public class CorpWechatConfig { /** 企業Id */
    private String corpid; /** 企業號密鑰 */
    private String corpsecret; /** 企業應用的id */
    private Integer agentId; /** 消息推送url */
    private String sendMessageUrl; }

微信消息推送相關常量git

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"; }
相關文章
相關標籤/搜索