FeiShuEventController.java 3.01 KB
package com.pipihelper.project.feishu.controller;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import com.pipihelper.project.feishu.dto.FeiShuConfig;
import com.pipihelper.project.feishu.dto.FeiShuEventDTO;
import com.pipihelper.project.feishu.dto.FeiShuMsgCardEventDTO;
import com.pipihelper.project.feishu.service.FeiShuEventService;
import com.pipihelper.project.feishu.utils.FeiShuEventDataDecrypter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Description: TODO
 * @author: charles
 * @date: 2022年03月30日 17:39
 */
@Slf4j
@RestController
@RequestMapping("/pipitest/feishu")
public class FeiShuEventController {

    @Resource
    private FeiShuConfig feiShuConfig;

    @Resource
    private FeiShuEventService feiShuEventService;


    @PostMapping("/event")
    public JSONObject event(@RequestBody String s) throws Exception {
        log.info("接收到飞书事件,密文:{}", s);
        JSONObject reqJsonObject = JSON.parseObject(s);
        FeiShuEventDataDecrypter d = new FeiShuEventDataDecrypter(feiShuConfig.getEncryptKey());
        JSONObject encryptJsonObject = JSON.parseObject(d.decrypt(reqJsonObject.getString("encrypt")));
        log.info("接收到飞书事件,明文:{}", encryptJsonObject);
        if (encryptJsonObject.containsKey("challenge")) {
            System.out.println(encryptJsonObject);
            return encryptJsonObject;
        }
        FeiShuEventDTO feiShuEventDTO = encryptJsonObject.toJavaObject(FeiShuEventDTO.class);
        if (!feiShuEventDTO.getHeader().getToken().equalsIgnoreCase(feiShuConfig.getVerificationToken())) {
            return null;
        }
        if ("im.message.receive_v1".equalsIgnoreCase(feiShuEventDTO.getHeader().getEvent_type())) {
//            feiShuEventService.imMessageReceiveV1(feiShuEventDTO);
        } else if ("im.message.message_read_v1".equalsIgnoreCase(feiShuEventDTO.getHeader().getEvent_type())) {
//            feiShuEventService.imMessageMessageReadV1(feiShuEventDTO);
        }
        return null;
    }

    @PostMapping("/msg_card")
    public JSONObject msgCardEvent(@RequestBody String s) {
        log.info("接收到飞书消息卡片,密文:{}", s);
        JSONObject reqJsonObject = JSON.parseObject(s);
        if (reqJsonObject.containsKey("challenge")) {
            JSONObject verifyRepJson = new JSONObject();
            verifyRepJson.put("challenge", reqJsonObject.getString("challenge"));
            log.info("接收到飞书机器人接口验证事件,返回值:{}", verifyRepJson);
            return verifyRepJson;
        }
        FeiShuMsgCardEventDTO feiShuMsgCardEventDTO = reqJsonObject.toJavaObject(FeiShuMsgCardEventDTO.class);
        String actionType = feiShuMsgCardEventDTO.getAction().getValue().getKey().split("\\.")[0];
        System.out.println(s);
        return null;
    }

//        switch (actionType) {
//            case "TEST_DATA":
//
////            default:
////        }
//        }
//
//    }
}