微信公众号开发-关注回复小程序卡片

直接看效果


image.png

开发流程

1.进入公众号->开发->基本配置

获取appid,appSecret,填入服务器的ip白名单用于获取accessToken


image.png

2.获取accessToken

获取accessToken比较简单 大家可以百度一下接口是下面这个
https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET

3.开发接收微信转发公众号消息接口

查看 微信公众号开发指南

//此接口用来响应微信对接口的验证
@GetMapping("message")
    @ResponseBody
    public String customMessageCheck(HttpServletRequest request) {
        String signature = request.getParameter("signature");
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");
        try {
            if (SignUtil.checkSignature(Token, signature, timestamp, nonce)) {
                return echostr;
            }
        } catch (Exception e) {
            log.error("校验出错:signature:{},timestamp:{},nonce:{},echostr:{}", signature, timestamp, nonce, echostr, e);
        }
        return "";
    }

下面接口用来实际接收公众号消息

@PostMapping(value = "message", produces = {"application/xml; charset=UTF-8"})
    @ResponseBody
    public String customMessagePost(HttpServletRequest request) {
        Map<String, String> result = null;
        try {
            result = WxAppUtils.parseXMLToMap(WxAppUtils.getWeiXinResponse(request));
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (result == null) {
            return "";
        }

        log.info("微信公众号开发接口返回result:{}",result);
        String openId = "";
        //非事件消息直接通知微信转发给客服,这样原来的公众号后台客服功能还可以使用
        if (!"event".equalsIgnoreCase(result.get("MsgType"))) {

            openId = result.get("FromUserName");
            String toUserName = result.get("ToUserName");

            //转发客服消息
            Map<String,String> parameters = new HashMap<>();
            parameters.put("ToUserName",openId);
            parameters.put("FromUserName",toUserName);
            parameters.put("CreateTime",(System.currentTimeMillis()/1000)+"");
            parameters.put("MsgType","transfer_customer_service");

            String xmlResult = WxAppUtils.assembParamToXml(parameters);

            return xmlResult;
        }


        if ("subscribe".equalsIgnoreCase(result.get("Event"))) {

            openId = result.get("FromUserName");
            String toUserName = result.get("ToUserName");

            // 发送关注后消息,可以根据业务需要是否发送,该发送直接在当前接口回复
            Map<String,String> parameters = new HashMap<>();
            parameters.put("ToUserName",openId);
            parameters.put("FromUserName",toUserName);
            parameters.put("CreateTime",(System.currentTimeMillis()/1000)+"");
            parameters.put("MsgType","text");
            parameters.put("Content","TODO... add message");
            String xmlResult = WxAppUtils.assembParamToXml(parameters);


            //发送小程序卡片,使用客服回复消息接口发送
            JSONObject subscribeInfoJson = new JSONObject();
            subscribeInfoJson.put("touser",openId);
            subscribeInfoJson.put("msgtype","miniprogrampage");
            JSONObject miniprogrampage = new JSONObject();
            miniprogrampage.put("title","title");
            miniprogrampage.put("appid","小程序appid");
            miniprogrampage.put("pagepath","小程序跳转链接");
            miniprogrampage.put("thumb_media_id","封面图片");
            subscribeInfoJson.put("miniprogrampage",miniprogrampage);

            //https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN
            String subscribeInfoResult = httpUtils.postJson("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=" + accountApiService.getAccessToken(DDTConstants.CWJX_APPID),
                    subscribeInfoJson.toJSONString());

           

            return xmlResult;

        }

        return "";
    }

4.微信公众号后台填入服务器URL就是上面http://你的域名/你的项目/message接口链接

image.png

PS: 接口最好使用加密方式,这里为了简单演示没有用加密的形式

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 在前四期的文章中,我们分别学习了“环境搭建与开发接入”、“文本消息的接收与响应”、“被关注回复与关键词回复”、“图...
    小风飞鱼阅读 3,014评论 0 4
  • 很早前就想了解下微信公众号开发,懒和拖延症让这计划迟迟没落实,正巧新项目有了微信公众号的业务,我心中没数地回应,我...
    英文名叫夏天阅读 4,517评论 1 4
  • 开发前首先我们要知道一些概念 各公众号区别:1、订阅号:为媒体和个人提供一种信息传播方式,主要偏于为用户传达资讯(...
    CoderZS阅读 3,227评论 1 19
  • 1、开启公众号开发者模式 公众平台的技术文档目的为了简明扼要的交代接口的使用,语句难免苦涩难懂,甚至对于不同的读者...
    good7758阅读 1,548评论 0 1
  • 过去,我们常常凭感觉,觉得平均水准以上就是不落后了。 这个感觉肯定是有些偏差的,为什么呢?什么 首先人们的感觉永远...
    happ_iness阅读 639评论 0 50