FeiShuEventService.java 3.03 KB
package com.pipihelper.project.feishu.service;

import cn.hutool.core.util.IdUtil;
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.FeiShuEventHeaderDTO;
import com.pipihelper.project.feishu.dto.FeiShuEventReceiveMessageDTO;
import com.pipihelper.project.feishu.dto.msg.FeiShuMsgDTO;
import com.pipihelper.project.tx.dto.TxConfig;
import com.pipihelper.project.tx.service.TxApiService;

import com.tencentcloudapi.tbp.v20190627.models.Group;
import com.tencentcloudapi.tbp.v20190627.models.TextProcessResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.regex.Pattern;

/**
 * @Description: TODO
 * @author: charles
 * @date: 2022年04月11日 17:01
 */

@Service
public class FeiShuEventService {
    @Resource
    private FeiShuConfig feiShuConfig;


    @Resource
    private FeiShuApiService feiShuApiService;



    @Autowired
    private TxApiService txApiService;



    @Autowired
    private TxConfig txConfig;



    @Async
    public void imMessageReceiveV1(FeiShuEventDTO feiShuEventDTO){
        FeiShuEventReceiveMessageDTO feiShuEventReceiveMessageDTO = feiShuEventDTO.getEvent().toJavaObject(FeiShuEventReceiveMessageDTO.class);
        FeiShuEventReceiveMessageDTO.Sender sender= feiShuEventReceiveMessageDTO.getSender();
        FeiShuEventReceiveMessageDTO.Message message= feiShuEventReceiveMessageDTO.getMessage();
        if(message.getChat_type().equals("p2p")) {
                robotTalk(feiShuEventDTO);

        }
    }

    public void robotTalk(FeiShuEventDTO feiShuEventDTO){
        FeiShuEventReceiveMessageDTO feiShuEventReceiveMessageDTO = feiShuEventDTO.getEvent().toJavaObject(FeiShuEventReceiveMessageDTO.class);
        FeiShuEventReceiveMessageDTO.Sender sender= feiShuEventReceiveMessageDTO.getSender();
        FeiShuEventReceiveMessageDTO.Message message= feiShuEventReceiveMessageDTO.getMessage();
        FeiShuMsgDTO feiShuMsgDTO = new FeiShuMsgDTO();
        feiShuMsgDTO.setReceiveId(sender.getSender_id().getOpen_id());
        if(message.getMessage_type().equals("text")){
            JSONObject jsonObjectText = JSON.parseObject(message.getContent());
            String inputText = jsonObjectText.getString("text");

            String uuid = IdUtil.randomUUID();
            TextProcessResponse textProcessResponse = txApiService.txTextProcessRequest(uuid, inputText);
            for (Group group : textProcessResponse.getResponseMessage().getGroupList()) {
                JSONObject content = new JSONObject();
                content.put("text", group.getContent());
                feiShuMsgDTO.setContent(content.toString());
                feiShuMsgDTO.setMsgType("text");
                feiShuApiService.sendMsg(feiShuMsgDTO, "open_id");
                }

            }
        }


}