微信公众号开发-----微信模板消息接口-----发送模板消息
来源:互联网 发布:linux创建用户和密码 编辑:程序博客网 时间:2024/05/16 05:44
发送模板消息
接口调用请求说明
http请求方式: POSThttps://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKENPOST数据说明
POST数据示例如下:
{ "touser":"OPENID", "template_id":"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY", "url":"http://weixin.qq.com/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":"2014年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.weixin.qq.com/cgi-bin/message/template/send?access_token="+accessToken.getToken(); //post请求数据 String url="http://weixin.qq.com/download"; //data JSONObject dataJson=new JSONObject(); //first JSONObject fstJson=new JSONObject(); fstJson.put("value","执行人员操作提醒:"); //keyword1 JSONObject k1Json=new JSONObject(); k1Json.put("value",keyword1); //keyword2 JSONObject k2Json=new JSONObject(); k2Json.put("value",keyword2); //keyword3 JSONObject k3Json=new JSONObject(); k3Json.put("value",keyword3); //remark JSONObject 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 "用户信息不存在,修改失败";}
阅读全文
0 0
- 微信公众号开发-----微信模板消息接口-----发送模板消息
- 微信公众号发送模板消息接口
- 微信公众号发送模板消息
- 微信公众号发送模板消息
- 微信公众号发送模板消息
- 微信公众号开发之[发送模板消息]
- 微信公众平台开发-消息管理-发送模板消息
- 微信公众号开发--模板消息
- 微信公众平台模板消息发送接口文档
- 微信公众平台发送模板消息
- 微信公众平台发送模板消息
- 微信公众平台 发送模板消息
- 微信公众平台发送模板消息
- 微信公众平台开发-发送模板消息
- Java微信公众平台开发之发送模板消息
- Java微信公众平台开发之发送模板消息
- 微信公众平台开发-发送模板消息
- Java微信公众平台开发之发送模板消息
- android 大神讲解Rxjava
- maven集成tomcat7
- HttpServletRequest对象
- Spring Boot基础知识(四)----springMVC与模板引擎
- 使用项目管理工具对Svn代码查看和统计
- 微信公众号开发-----微信模板消息接口-----发送模板消息
- Android WebView 关闭无痕浏览
- mongodb删除过期数据
- Maven远程仓库的各种配置
- javascirpt 获取汉字首字母
- Javaweb开发遇到的问题及解决方案(2017.5.18)
- tomcat内存溢出
- 聊聊MV+
- git、maven