微信每日早安推送

專注的阿熊發表於2022-11-14

import com.alibaba.fastjson.JSONObject;

import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;

import me.chanjar.weixin.mp.api.WxMpService;

import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;

import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;

import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;

/**

  *@ClassName Pusher

  *@Description TODO

  *@Author ydzhao

  *@Date 2022/8/2

  */

public class Pusher {

     private static String appId = "xxxx";

     private static String secret = "xxx";

     public static void push(String openId,String templateId){

         //1 ,配置

         WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();

         wxStorage.setAppId(appId);

         wxStorage.setSecret(secret);

         WxMpService wxMpService = new WxMpServiceImpl();

         wxMpService.setWxMpConfigStorage(wxStorage);

         //2, 推送訊息

         WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()

                 .toUser(openId)

                 .templateId(templateId)

                 .build();

         //3, 如果是正式版傳送模版訊息,這裡需要配置你的資訊

         JSONObject todayWeather = 外匯跟單gendan5.comTianqi.getNanjiTianqi();

         templateMessage.addData(new WxMpTemplateData("riqi",todayWeather.getString("date") + "  "+ todayWeather.getString("week"),"#00BFFF"));

         templateMessage.addData(new WxMpTemplateData("tianqi",todayWeather.getString("text_day"),"#00FFFF"));

         templateMessage.addData(new WxMpTemplateData("low",todayWeather.getString("low") + "","#173177"));

         templateMessage.addData(new WxMpTemplateData("high",todayWeather.getString("high")+ "","#FF6347" ));

         templateMessage.addData(new WxMpTemplateData("caihongpi",CaiHongPi.getCaiHongPi(),"#FF69B4"));

         templateMessage.addData(new WxMpTemplateData("lianai",JiNianRi.getLianAi()+"","#FF1493"));

         templateMessage.addData(new WxMpTemplateData("shengri",JiNianRi.getShengRi()+"","#FFA500"));

         templateMessage.addData(new WxMpTemplateData("jinju",CaiHongPi.getJinJu()+"","#C71585"));

         //templateMessage.addData(new WxMpTemplateData("jiehun",JiNianRi.getJieHun()+""));

         templateMessage.addData(new WxMpTemplateData("linzhen",JiNianRi.getLinZhen()+"","#FF6347"));

         String beizhu = " 情人節快樂! ";

         if(JiNianRi.getJieHun() % 365 == 0){

             beizhu = " 今天是結婚紀念日! ";

         }

         if(JiNianRi.getLianAi() % 365 == 0){

             beizhu = " 今天是戀愛紀念日! ";

         }

         if(JiNianRi.getLinZhen() % 365 == 0){

             beizhu = " 今天是結婚紀念日! ";

         }

         templateMessage.addData(new WxMpTemplateData("beizhu",beizhu,"#FF0000"));

         try {

             System.out.println(templateMessage.toJson());

             System.out.println(wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage));

         } catch (Exception e) {

             System.out.println(" 推送失敗: " + e.getMessage());

             e.printStackTrace();

         }

     }

}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2923292/,如需轉載,請註明出處,否則將追究法律責任。

相關文章