企业微信消息存档

企业微信消息存档


一、配置动态库

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一种用于开发Java应用程序的开源框架,它简化了基于Spring框架的应用程序的配置和部署。企业微信是一款为企业提供即时通讯和协同办公的应用,它能轻松满足企业的沟通和协作需求。 为了在Spring Boot应用程序中引入企业微信会话存档SDK,需要执行以下步骤: 1. 下载企业微信会话存档SDK:你可以从企业微信官方网站或其他可信来源下载适用于Java的企业微信会话存档SDK。 2. 引入SDK依赖:将下载的SDK文件导入Spring Boot应用程序的依赖管理系统中。可以使用Maven或Gradle等构建工具来管理依赖,并在配置文件中指定SDK的版本号。 3. 创建企业微信会话存档SDK的配置文件:在Spring Boot的配置文件中,添加企业微信会话存档SDK所需的配置参数,如企业微信的Corpid、Secret和Agentid等。 4. 创建企业微信会话存档SDK的服务类:在Spring Boot应用程序中创建一个服务类,用于调用企业微信会话存档SDK的接口。根据业务需求,可以根据文档提供的接口进行数据的存储、获取等操作。 5. 在应用程序中调用企业微信会话存档服务:在需要使用企业微信会话存档功能的地方,通过注入企业微信会话存档服务的实例,调用相应的方法来实现会话存档的功能。 需要注意的是,引入企业微信会话存档SDK前,要确保企业微信账号已通过认证并开通了相应的权限。 以上是使用Spring Boot引入企业微信会话存档SDK的基本步骤,根据具体的需求,可能还需要对业务逻辑进行进一步的处理和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值