企业微信消息存档

企业微信消息存档


一、配置动态库

1.企业微信官方文档下载sdk:https://open.work.weixin.qq.com/api/doc/90000/90135/91774#%E6%95%B4%E4%BD%93%E6%B5%81%E7%A8%8B

2.将sdk中的libWeWorkFinanceSdk_Java.so文件上传到服务器/usr/local/lib路径,路径可自定义,爱搁哪搁哪

3.配置环境变量,修改~/.bashrc
增加 "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib,修改了路径配置的变量路径要相应修改


二、拉取数据

新建包com.tencent.wework
新建包com.tencent.wework
新建包com.tencent.wework
(重要的事说三遍)
把sdk中的Finance文件丢进去

有使用到大佬封装的jar包,github搜一下去琢磨文档

<dependency>
  <groupId>com.github.binarywang</groupId>
  <artifactId>weixin-java-cp</artifactId>
  <version>4.1.0</version>
</dependency>

解密的私钥放在工程resources/privateKey目录下
获取到回调之后去拉取存档消息
WxCpMessageArchive类是最终解析出来的数据

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
import me.chanjar.weixin.cp.message.WxCpMessageHandler;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.*;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Component
@Slf4j
public class MsgauditNotifyEventHandle implements WxCpMessageHandler {
   

    @Autowired
    ResourceLoader resourceLoader;

    public final Map<String, Long> FINANCE_MAP = new HashMap<>();
    private final Map<String, String> PRIVATE_KEY_MAP = new HashMap<>();
    private final Map<String, String> MSG_TYPE_MAP = new HashMap<>();

    @PostConstruct
    public void initConvertMap() throws Exception {
   
        //检查动态库是否存在
        File file = new File("/usr/local/lib/libWeWorkFinanceSdk_Java.so");
        List<String> execute = CmdUtils.execute("cat ~/.bashrc");
        boolean contains = execute.contains("export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib");
        if (!file.exists() || !contains){
   
            throw new BizException("会话存档所需动态库未配置\n\n" +
                    "1.企业微信官方文档下载sdk:https://open.work.weixin.qq.com/api/doc/90000/90135/91774#%E6%95%B4%E4%BD%93%E6%B5%81%E7%A8%8B\n" +
                    "2.将sdk中的libWeWorkFinanceSdk_Java.so文件上传到服务器/usr/local/lib路径\n" +
                    "3.配置环境变量,修改~/.bashrc,增加 \"export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib\"\n");
        }
        MSG_TYPE_MAP.put("external_redpacket", "redpacket");
        MSG_TYPE_MAP.put("news", "info");
        MSG_TYPE_MAP.put("markdown", "info");
        MSG_TYPE_MAP.put("docmsg", "doc");
    }

    @Override
    public WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, Map<String, Object> context, WxCpService wxCpService,
                                    WxSessionManager sessionManager) {
   
        String appId = wxMessage.getToUserName();
        Integer agentId = wxMessage.getAgentId();
        String key = appId + "#" + agentId;
        String privateKey = PRIVATE_KEY_MAP.getOrDefault(key, null);
        if (StringUtils.isBlank(privateKey)) {
   
            try {
   
                privateKey = this.readerFile("/privateKey/" + appId + "#" + agentId + "#private.pem");
            } catch (IOException ignored) {
   
            }
            if (StringUtils.isBlank(privateKey)) {
   
                log.error("会话存档初始化失败,未获取到解密私钥,appId:{},agentId:{}", appId, agentId);
                return null;
            }
            PRIVATE_KEY_MAP.put(key, privateKey);
        }
        Long sdk = this.getSdk(appId

你可能感兴趣的:(微信,java)