企业微信回调配置及被动回复

前期准备:

1.公司域名映射到本机ip

一、配置接收消息API

API配置.jpg

二、URL验证

需结合自身项目情况和官方的回调服务demo完成代码,以下仅供参考(项目未被拆分,只看wx服务器代码即可)

/**
     * app服务器
     * URL验证转发
     * @return
     */ 
    @ResponseBody 
    @RequestMapping("/message/callback")
    public String getUrl(HttpServletRequest request,
            @RequestParam("msg_signature")String signature,
            @RequestParam("timestamp") String timestamp,
            @RequestParam("nonce") String nonce,
            @RequestParam("echostr") String echostr) {
        String url=Const.getWechatServiceUrl()+"/wx/cp/portal/{agentId}";
        System.out.println("pushErrorMessage url:"+url);
        Map<String, String> params=new HashMap<String, String>();
        params.put("msg_signature", signature);
        params.put("timestamp", timestamp);
        params.put("nonce", nonce);
        params.put("echostr", echostr);
        //調用公用微信消息發送接口(app服务器转发到wx服务器)
        String result= HttpClientUtil.doGetURL(url, params);
        System.out.println(result);
        return result;
    } 
/**
   * wx服务器
   * Content-Type类型
   * text/plain 文本格式
   * application/json json数据格式
   * application/xml xml数据格式
   * */
  @GetMapping(produces = "text/plain;charset=utf-8")
  public String authGet(@PathVariable Integer agentId,
                        @RequestParam(name = "msg_signature", required = false) String signature,
                        @RequestParam(name = "timestamp", required = false) String timestamp,
                        @RequestParam(name = "nonce", required = false) String nonce,
                        @RequestParam(name = "echostr", required = false) String echostr) {
    this.logger.info("\n接收到来自微信服务器的认证消息:signature = [{}], timestamp = [{}], nonce = [{}], echostr = [{}]",
        signature, timestamp, nonce, echostr);

    if (StringUtils.isAnyBlank(signature, timestamp, nonce, echostr)) {
      throw new IllegalArgumentException("请求参数非法,请核实!");
    }
    final WxCpService wxCpService = WxCpConfiguration.getCpService(agentId);
    if (wxCpService == null) {
      throw new IllegalArgumentException(String.format("未找到对应agentId=[%d]的配置,请核实!", agentId));
    }
    if (wxCpService.checkSignature(signature, timestamp, nonce, echostr)) {
        String sEchoStr = new WxCpCryptUtil(wxCpService.getWxCpConfigStorage()).decrypt(echostr);
        this.logger.debug("\n返回的明文:{}",sEchoStr);
        return sEchoStr;
    }
    return "非法请求";
  }

利用官方调试工具验证URL是否配置成功


URL验证.jpg

三、被动回复

需结合自身项目情况和官方的回调服务demo完成代码,以下仅供参考(项目未被拆分,只看wx服务器代码即可)

/**
     * app服务器
     * 转发用户回复的信息
     * @return
     */ 
    @ResponseBody 
    @PostMapping("/message/callback")
    public String postMessage(@RequestBody String requestBody,
            @RequestParam("msg_signature") String signature,
            @RequestParam("timestamp") String timestamp,
            @RequestParam("nonce") String nonce) { 
        String url=Const.getWechatServiceUrl()+"/wx/cp/portal/{agentId}";
        //System.out.println("postMessage url:"+url);
        Map<String, String> params=new HashMap<String, String>();
        params.put("msg_signature", signature);
        params.put("timestamp", timestamp);
        params.put("nonce", nonce);
        params.put("body", requestBody);
        //調用公用微信消息發送接口
        String result= HttpClientUtil.doPostMessage(url, params);
        JSONObject object= JSONObject.parseObject(result);
        //返回xml格式数据到企业微信
        String xml = object.get("out").toString();
        System.out.println(xml);
        return xml;
    }
/**
   * wx服务器
   * Content-Type类型
   * text/plain 文本格式
   * application/json json数据格式
   * application/xml xml数据格式
   * */
@PostMapping(produces = "application/json; charset=UTF-8")
  public R post(@PathVariable Integer agentId,
                     @RequestParam("msg_signature") String signature,
                     @RequestParam("timestamp") String timestamp,
                     @RequestParam("nonce") String nonce,
                     @RequestParam("body") String requestBody) {
    this.logger.info("\n接收微信请求:\n[signature=[{}],\n timestamp=[{}],\n nonce=[{}],\n requestBody=[\n{}\n] ",signature, timestamp, nonce, requestBody);
    R r;
    final WxCpService wxCpService = WxCpConfiguration.getCpService(agentId);
    WxCpXmlMessage inMessage = WxCpXmlMessage.fromEncryptedXml(requestBody, wxCpService.getWxCpConfigStorage(),timestamp, nonce, signature);
    this.logger.debug("\n消息解密后内容为:\n{} ", JsonUtils.toJson(inMessage));
    //根据不同类型做出不同被动响应
    if(inMessage.getMsgType().equals("text")){
        WxCpXmlOutMessage text = WxCpXmlOutMessage
          .TEXT()
          .toUser(inMessage.getToUserName())
          .fromUser(inMessage.getFromUserName())
          .content("文本消息")
          .build();
        String out = text.toEncryptedXml(wxCpService.getWxCpConfigStorage());
        r=R.ok("回复成功。");
        r.put("out", out);
        r.put("message", inMessage);
        return r;
    }
  }
被动回复测试.jpg

四、附

1.企业微信官方API文档:https://developer.work.weixin.qq.com/document/path/90235
2.WxJava:
https://github.com/Wechat-Group/WxJava/blob/develop/demo.md
3.回调服务:https://developer.work.weixin.qq.com/devtool/introduce?id=36388

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

推荐阅读更多精彩内容