100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 微信公众号开发-----微信模板消息接口-----发送模板消息

微信公众号开发-----微信模板消息接口-----发送模板消息

时间:2018-10-19 00:03:30

相关推荐

微信公众号开发-----微信模板消息接口-----发送模板消息

发送模板消息

接口调用请求说明

http请求方式:POSThttps://api./cgi-bin/message/template/send?access_token=ACCESS_TOKEN

POST数据说明

POST数据示例如下:

{"touser":"OPENID","template_id":"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY","url":"/download", "miniprogram":{"appid":"xiaochengxuappid12345","pagepath":"index?foo=bar"},"data":{"first": {"value":"恭喜你购买成功!","color":"#173177"},"keynote1":{"value":"巧克力","color":"#173177"},"keynote2": {"value":"39.8元","color":"#173177"},"keynote3": {"value":"9月22日","color":"#173177"},"remark":{"value":"欢迎再次购买!","color":"#173177"}}}

参数说明

注:url和miniprogram都是非必填字段,若都不传则模板无跳转;若都传,会优先跳转至小程序。开发者可根据实际需要选择其中一种跳转方式即可。当用户的微信客户端版本不支持跳小程序时,将会跳转至url。

返回码说明

在调用模板消息接口后,会返回JSON数据包。正常时的返回JSON数据包示例:

{"errcode":0,"errmsg":"ok","msgid":200228332}

Demo案例: 1.在微信公众号(这里是测试号,随便找的模板)中添加模板测试接口

2.建一个模板消息

TemplateMessageUtil.java

//发送模板消息public static String sendTemplateMessage(String touser,String template_id,String keyword1,String keyword2,String keyword3){AccessToken accessToken=WeixinUtil.getAccessToken(ParamesAPI.appId,ParamesAPI.secret);String sendUrl="https://api./cgi-bin/message/template/send?access_token="+accessToken.getToken();//post请求数据String url="/download";//dataJSONObject dataJson=new JSONObject();//firstJSONObject fstJson=new JSONObject();fstJson.put("value","执行人员操作提醒:");//keyword1JSONObject k1Json=new JSONObject();k1Json.put("value",keyword1);//keyword2JSONObject k2Json=new JSONObject();k2Json.put("value",keyword2);//keyword3JSONObject k3Json=new JSONObject();k3Json.put("value",keyword3);//remarkJSONObject rmkJson=new JSONObject();rmkJson.put("value","物料执行信息已送达,请尽快完成任务哦!");dataJson.put("first",fstJson);dataJson.put("keyword1",k1Json);dataJson.put("keyword2",k2Json);dataJson.put("keyword3",k3Json);dataJson.put("remark",rmkJson);JSONObject json=new JSONObject();json.put("touser",touser);json.put("template_id",template_id);json.put("data",dataJson);JSONObject jsonObject = HttpClientUtil.getInstance().httpPostRequest(sendUrl, json.toString());System.out.println("发送模板消息:" + jsonObject);String msg="errcode:"+jsonObject.getString("errcode")+"-----"+"errmsg:"+jsonObject.getString("errmsg");return msg;}

注:同使用httpClient请求服务

3.PC中执行事件时调用第2部的接口,并给出需要的参数

//给执行人员发送模板消息String touser="";//接受者openidString template_id="FMn8Ph1R-Ie04VXs-ZeaH88z32XBMMqYzoitmzvay70";//模板idString keyword1=orderDetails.getBelongOrderBase().getCustomerName();//客户姓名String keyword2=orderDetails.getBelongOrderBase().getCustomerAddress();//客户地址String keyword3=orderDetails.getMaterialName();//品名if (StringUtils.isNotBlank(belongUserInfosPer)) {String[] userInfoArray = belongUserInfosPer.split(",");for (int i = 0; i < userInfoArray.length; i++) {String item = userInfoArray[i];Long itemLong = StringConverters.ToLong(item);if (itemLong != null) {UserInfo userInfo=userInfoBiz.findModel(itemLong);touser=userInfo.getOpenId();String msg=TemplateMessageUtil.sendTemplateMessage(touser,template_id,keyword1,keyword2,keyword3);logger.info(msg);}}} else {return "用户信息不存在,修改失败";}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。