温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

java微信公众号发送消息模板

发布时间:2020-09-28 04:11:47 来源:脚本之家 阅读:172 作者:堆栈 栏目:编程语言

本文实例为大家分享了java微信公众号发送消息模板的具体代码,供大家参考,具体内容如下

这段时间接触公众号开发,写下向用户发送消息模板的接口调用

先上接口代码

public static JSONObject sendModelMessage(ServletContext context,JSONObject jsonMsg) {
    System.out.println("消息内容:"+jsonMsg);
    boolean result = false;
    try {
      getWX_AccessToken(context);
    } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    // 拼接请求地址
    String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
    requestUrl = requestUrl.replace("ACCESS_TOKEN", context.getAttribute(ContextTokenName).toString());
    // 发送客服消息
    JSONObject jsonObject = getJsonByWX(requestUrl, context, "POST",jsonMsg, false);
 
    if (null != jsonObject) {
      int errorCode = jsonObject.getInt("errcode");
      String errorMsg = jsonObject.getString("errmsg");
      if (0 == errorCode) {
        result = true;
        System.out.println("模板消息发送成功 errcode:{} "+errorCode+"----"+errorMsg);
      } else {
        System.out.println("模板消息发送失败 errcode:{} "+errorCode+"----"+errorMsg);
      }
    }
 
    return null;
  }

15行那段getJsonByWX是统一调用微信接口的方法,每个项目都有自己的调用方法,我这里就不贴了。接口调用链接:https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN

接下来就是建个bean类,里面写入一下颜色及值

private String value;
  private String color;
  
  public String getValue() {
    return value;
  }
 
  public void setValue(String value) {
    this.value = value;
  }
 
  public String getColor() {
    return color;
  }
 
  public void setColor(String color) {
    this.color = color;
  

}

在公众号里填写模板消息的对应格式

java微信公众号发送消息模板

之后就是有个触发点,我选择发货后把发货信息发送给用户

PageData wechatTemplate = new PageData();
    wechatTemplate.put("template_id", "填写你的模板id");
    wechatTemplate.put("touser", userInfo.get("openid"));//获取用户的openid
  
    Map<String,TemplateMessageUtil> mapdata = new HashMap<>();
    
    TemplateMessageUtil first = new TemplateMessageUtil();    
    first.setColor("#173177");
    first.setValue("发货通知");
    mapdata.put("first", first);
    
    TemplateMessageUtil text1 = new TemplateMessageUtil();  
    text1.setColor("#173177");
    text1.setValue("您好,您所购买的商品已发货。");
    mapdata.put("text1", text1);
    
    TemplateMessageUtil text2 = new TemplateMessageUtil();  
    text2.setColor("#173177");
    text2.setValue(expresser_name);
    mapdata.put("text2", text2);
    
    TemplateMessageUtil text3 = new TemplateMessageUtil();  
    text3.setColor("#173177");
    text3.setValue(expresser_phone);
    mapdata.put("text3", text3);
    
    TemplateMessageUtil remark = new TemplateMessageUtil();    
    remark.setColor("#173177");
    remark.setValue("请保持电话畅通>>");
    mapdata.put("remark", remark);

    JSONObject json = new JSONObject(); 
    json.put("data",mapdata);
    json.putAll(wechatTemplate);//转为json
        
    WXInterface.sendModelMessage(context,json);

之后手机就会收到信息了

java微信公众号发送消息模板

整体思路是这样,也是参照百度而来,因为每个人的项目里方法都不一样,我就不详细贴上,既然做到发送模板消息了,统一调用微信接口的方法应每个人该也早写在工具类里了,每个人都不同,当应该都有,调用这个方法,把微信模板消息连接的条件access_token写进去就请求了,剩下的就是传入你要发送的消息,消息存入集合,集合转json才行,JSONObject类相信也都有,我也不贴了,每个人项目都不一样,没必要照搬过去,就照着自己原先已有的类改进。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI