1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > php 公众号指定人发消息 微信公众号客服接口给指定用户openid发送消息

php 公众号指定人发消息 微信公众号客服接口给指定用户openid发送消息

时间:2022-12-27 16:06:34

相关推荐

php 公众号指定人发消息 微信公众号客服接口给指定用户openid发送消息

微信公众号客服接口给指定用户openid发送消息

-09-23

微信开发文档:

客服接口-发消息

接口调用请求说明

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

各消息类型所需的JSON数据包如下:

发送文本消息

{ "touser":"OPENID", "msgtype":"text", "text": { "content":"Hello World" }}

创建封装实体类:

package com.zenithink.spp.cms.entity;import java.util.Map;/** * 客户接口消息发送实体 * * @author * @date -2-6 11:00:30 */public class TestMessage { //openid private String touser; //消息类型 private String msgtype; //消息内容 private Map text ; public String getTouser() { return touser; } public void setTouser(String touser) { this.touser = touser; } public String getMsgtype() { return msgtype; } public void setMsgtype(String msgtype) { this.msgtype = msgtype; } public Map getText() { return text; } public void setText(Map text) { this.text = text; }}

后台controller代码:

/** * 状态修改为测试中 * * @param id 要测试的ID * @param request * @return 内容展示页面 */@ResponseBody@RequestMapping(value = "/service/test", method = RequestMethod.PUT)public ResponseVO ContentTest(Long id, HttpServletRequest request) { ResponseVO vo=new ResponseVO(); if(id!=null){ //根据接收的ID查询相应的内容实体 WeixinContent weixinContent = weixinContentService.selectById(id); //判断查询到的对象的合法性 if (weixinContent!=null&&(weixinContent.getState() == 1||weixinContent.getState()==4)) { //修改内容实体状态为测试中 weixinContent.setState(2); //将内容实体进行保存操作 weixinContentService.updateById(weixinContent); //获得内容静态页面的访问路径 String templatesUrl= weixinContent.getTemplatesUrl(); //拼接访问的完整路径 String saveUrl =fileuploadPrefix + "/" + templatesUrl; //获得内容信息标题 String title = weixinContent.getTitle(); //拼接发送的消息内容 String content="你好!标题("+title+")点我测试"; //创建微信用户查询条件 Wrapper wrapper=new EntityWrapper<>(); wrapper.where("tagid_list={0}","[101]"); //获得满足条件的集合 List weixinUsers = weixinUserService.selectList(wrapper); //遍历用户集合调用业务层进行消息发送 for (WeixinUser weixinUser : weixinUsers) { String openid = weixinUser.getOpenid(); //调用业务层进行发送消息 wxContentTextService.contentTest(openid,content); } vo.setSuccess(true); } else { //不是未测试状态所以不能进行状态修改 vo.setSuccess(false); } } return vo;}后台service代码:

/** *

* 菜单信息 服务实现类 *

* * @author * @date /01/15 */@Servicepublic class WxContentTextService { private static Logger log = LoggerFactory.getLogger(WxContentTextService.class); /** * 客服接口给用户发送消息接口 */ public static String content_openid="https://api./cgi-bin/message/custom/send?access_token=ACCESS_TOKEN"; @Autowired private TokenFeignService tokenFeignService; @Autowired private WeixinPortalService weixinPortalService; /** * * @param openid openid * @param saveUrl 静态页面访问地址 * @return */ public ResponseVO contentTest(String openid,String saveUrl){ //获得令牌 String accessToken = tokenFeignService.getToken(); //创建返回实体对象 ResponseVO vo = new ResponseVO(); //替换token String url=content_openid.replace("ACCESS_TOKEN", accessToken); TestMessage testMessage=new TestMessage(); //设置消息的类型 testMessage.setMsgtype("text"); //设置要发送的openid集合 testMessage.setTouser(openid); //创建集合 Map map=new HashMap<>(); //设置发送内容 map.put("content",saveUrl); testMessage.setText(map); //将测试消息对象转成json String jsonTestMessage = JSONObject.toJSONString(testMessage); //调用接口进行发送 JSONObject jsonObject = httpRequest(url, "POST", jsonTestMessage); log.error("分组群发消息失败 errcode:{" + jsonObject.getInteger("errcode")+"} " + "errmsg:{"+jsonObject.getString("errmsg")+"} "); Integer errcode = jsonObject.getInteger("errcode"); String errorCodeText = ErrorCodeText.errorMsg(errcode); if (errcode == 0){ vo.setSuccess(true); }else{ vo.setSuccess(false); } vo.setCode(errcode); vo.setText(errorCodeText); return vo; }}获取token的业务层:

import org.flix.feign.FeignClient;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;/** * 进行请求分发 * */@FeignClient(value = "weixin-2")public interface TokenFeignService { /** * 进行token请求 * @param * @return */ @RequestMapping(value = "/getToken",method = RequestMethod.GET) String getToken();}微信请求工具类utils

public class WeixinHttpUtil { private static Logger log = LoggerFactory.getLogger(WeixinHttpUtil.class); /** * 描述: 发起https请求并获取结果 * @param requestUrl 请求地址 * @param requestMethod 请求方式(GET、POST) * @param outputStr 提交的数据 * @return JSONObject(通过JSONObject.get(key)的方式获取json对象的属性值) */ public static JSONObject httpRequest(String requestUrl, String requestMethod, String outputStr) { JSONObject jsonObject = null; StringBuffer buffer = new StringBuffer(); try { // 创建SSLContext对象,并使用我们指定的信任管理器初始化 TrustManager[] tm = { new MyX509TrustManager() }; SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE"); sslContext.init(null, tm, new java.security.SecureRandom()); // 从上述SSLContext对象中得到SSLSocketFactory对象 SSLSocketFactory ssf = sslContext.getSocketFactory(); URL url = new URL(requestUrl); HttpsURLConnection httpUrlConn = (HttpsURLConnection) url.openConnection(); httpUrlConn.setSSLSocketFactory(ssf); httpUrlConn.setDoOutput(true); httpUrlConn.setDoInput(true); httpUrlConn.setUseCaches(false); // 设置请求方式(GET/POST) httpUrlConn.setRequestMethod(requestMethod); if ("GET".equalsIgnoreCase(requestMethod)) {httpUrlConn.connect();} // 当有数据需要提交时 if (null != outputStr) { OutputStream outputStream = httpUrlConn.getOutputStream(); // 注意编码格式,防止中文乱码 outputStream.write(outputStr.getBytes("UTF-8")); outputStream.close(); } // 将返回的输入流转换成字符串 InputStream inputStream = httpUrlConn.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8"); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String str = null; while ((str = bufferedReader.readLine()) != null) { buffer.append(str); } bufferedReader.close(); inputStreamReader.close(); // 释放资源 inputStream.close(); inputStream = null; httpUrlConn.disconnect(); jsonObject = JSONObject.parseObject(buffer.toString()); } catch (ConnectException ce) { log.error("Weixin server connection timed out."); } catch (Exception e) { log.error("https request error:{}", e); } return jsonObject; }}

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。

/style/images/nopic.gif

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