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.conts.WebhooksTypeConst;
import io.github.wslxm.springbootplus2.starter.robot.properties.RobotProperties;
import io.github.wslxm.springbootplus2.starter.robot.properties.WebhookProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

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

    @Autowired
    private RobotProperties robotProperties;


    /**
     * 发送信息方法 (系统专用)
     *
     * 

* 可在发信息时指定发送的 code, 可以指定 code 或 排除 code 来控制消息最终是否发送 * 读取默认配置: 使用 primary 指定的 webhook, 不可修改艾特指定人 *

* @param content * @param code 根据 code 可以排除某些消息, 或者指定只有那些 code 信息才进行发送 * @return */ public String sendSysMsg(String content, Integer code) { // 只返回 codes 中的存在数据, code等于 -1 强制发送 List codes = robotProperties.getCodes(); if (codes != null) { // 只返回指定 codes 数据 if (codes.contains(code)) { return sendMsg(content, ""); } return null; } else { // 指定 excludeCodes 不发送 List excludeCodes = robotProperties.getExcludeCodes(); if (excludeCodes != null && excludeCodes.contains(code)) { return null; } return sendMsg(content, ""); } } /** * 发送机器人信息 * * @return boolean */ public String sendMsg(String content) { return sendMsg(content, null); } /** * 发送机器人信息 * * @return boolean */ public String sendMsg(String content, String atUser) { return sendMsg(robotProperties.getPrimary(), content, atUser); } /** * 发送消息 * @param primary 指定源 * @param content 发送内容 * @param atUser 艾特指定人 * @date 2024/8/30 15:35 * @return boolean */ public String sendMsg(String primary, String content, String atUser) { WebhookProperties webhookProperties = robotProperties.getWebhooks().get(primary); if (webhookProperties == null) { log.error("primary 错误, 没有找到名为 {} 的 webhook 源", primary); return null; } if (webhookProperties.getUrl() == null || "".equals(webhookProperties.getUrl() )) { log.error("{} 配置中没有配置 webhook 地址", primary); return null; } String type = webhookProperties.getType(); // 构建发送信息 WebhookProperties newWebhookProperties = new WebhookProperties(); newWebhookProperties.setUrl(webhookProperties.getUrl()); newWebhookProperties.setSecret(webhookProperties.getSecret()); newWebhookProperties.setDefaultAtUser(webhookProperties.getDefaultAtUser()); if (atUser != null && !"".equals(atUser)) { newWebhookProperties.setDefaultAtUser(atUser); } // 开始发送 if (WebhooksTypeConst.FEI_SHU.equals(type)) { // 发送飞书群消息 return FenShuApi.sendMsg(newWebhookProperties, content); } else if (WebhooksTypeConst.WECHAT.equals(type)) { // 发送企业微信群消息 return WechatApi.sendMsg(newWebhookProperties, content); } return null; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy