All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.wslxm.springbootplus2.starter.robot.service.RobotService Maven / Gradle / Ivy

package io.github.wslxm.springbootplus2.starter.robot.service;

import io.github.wslxm.springbootplus2.starter.robot.api.FenShuApi;
import io.github.wslxm.springbootplus2.starter.robot.api.WechatApi;
import io.github.wslxm.springbootplus2.starter.robot.properties.RobotProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 机器人服务
 *
 * @author wangsong
 * @date 2023/05/17
 */
@Service
public class RobotService {

    @Autowired
    private FenShuApi fenShuApi;
    @Autowired
    private WechatApi wechatApi;
    @Autowired
    private RobotProperties robotProperties;


    /**
     * 发送信息
     * @param content
     * @param code 根据code 可以排除某些消息,或者指定只有那些 code 信息才进行发送
     * @return
     */
    public boolean sendMsg(String content, Integer code) {
        // 只返回 codes 中的存在数据, code等于 -1 强制发送
        List codes = robotProperties.getCodes();
        if (codes != null) {
            // 只返回指定 codes 数据
            if (codes.contains(code)) {
                return sendMsg(content);
            }
            return false;
        } else {
            // 指定 excludeCodes 不发送
            List excludeCodes = robotProperties.getExcludeCodes();
            if (excludeCodes != null && excludeCodes.contains(code)) {
                return false;
            }
            return sendMsg(content);
        }
    }


    /**
     * 发送机器人信息
     *
     * @return boolean
     */
    public boolean sendMsg(String content) {
        // 发送飞书消息
        if (robotProperties.getFeishu() != null
                && robotProperties.getFeishu().getUrl() != null && !"".equals(robotProperties.getFeishu().getUrl())
                && robotProperties.getFeishu().getSecret() != null && !"".equals(robotProperties.getFeishu().getSecret())) {
            Map contentMap = new HashMap<>(4);
            contentMap.put("text", content);
            fenShuApi.sendMsg(contentMap);
        }

        // 发送企业微信 消息
        if (robotProperties.getWechat() != null
                && robotProperties.getWechat().getUrl() != null && !"".equals(robotProperties.getWechat().getUrl())
        ) {
            Map wechatContentMap = new HashMap<>(4);
            wechatContentMap.put("content", content);
            String mobiles = robotProperties.getWechat().getMobiles();
            if (mobiles != null && !"".equals(mobiles)) {
                wechatContentMap.put("mentioned_mobile_list", Arrays.asList(mobiles.split(",")));
            }
            wechatApi.sendMsg(wechatContentMap);
        }
        return true;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy