package com.pomelotea.hoperun.sign.service import com.pomelotea.hoperun.sign.api.model.ScNotifyRequest import com.pomelotea.hoperun.sign.api.model.ScNotifyResponse import com.pomelotea.hoperun.sign.api.model.DingTalkNotifyResponse import com.pomelotea.hoperun.sign.config.DingTalkConfig import com.pomelotea.hoperun.sign.config.ServerChan3Config import com.pomelotea.hoperun.sign.notify.DingTalkNotifyHelper import com.pomelotea.hoperun.sign.notify.ServerChan3NotifyHelper import com.pomelotea.hoperun.sign.scheduler.AutoDakaScheduler.Companion.logger import org.springframework.stereotype.Service @Service open class NotificationService( private val serverChan3NotifyHelper: ServerChan3NotifyHelper, private val dingTalkNotifyHelper: DingTalkNotifyHelper, private val serverChan3Config: ServerChan3Config, private val dingTalkConfig: DingTalkConfig ) { fun sendAllNotifications(title: String, content: String) { // 发送 ServerChan3 通知 try { val scResponse = sendServerChanNotification(title, content) logger.info("ServerChan3 通知发送结果: ${scResponse.message}") } catch (e: Exception) { logger.error("ServerChan3 通知发送失败", e) } // 发送钉钉通知 try { val dtResponse = sendDingTalkNotification(title, content) logger.info("钉钉通知发送结果: ${dtResponse.errmsg}") } catch (e: Exception) { logger.error("钉钉通知发送失败", e) } } fun sendServerChanNotification(title: String, content: String): ScNotifyResponse { val request = ScNotifyRequest(title = title, desp = content) return serverChan3NotifyHelper.push(request) } fun sendDingTalkNotification(title: String, content: String): DingTalkNotifyResponse { // 如果内容包含markdown格式,使用markdown类型消息 return if (content.contains("**") || content.contains("#") || content.contains("*")) { dingTalkNotifyHelper.sendMarkdown(title, content) } else { dingTalkNotifyHelper.sendText("$title\n\n$content") } } fun sendDingTalkText(text: String): DingTalkNotifyResponse { return dingTalkNotifyHelper.sendText(text) } fun sendDingTalkMarkdown(title: String, content: String): DingTalkNotifyResponse { return dingTalkNotifyHelper.sendMarkdown(title, content) } fun isServerChanEnabled(): Boolean { return serverChan3Config.uid.isNotEmpty() && serverChan3Config.sendKey.isNotEmpty() } fun isDingTalkEnabled(): Boolean { return dingTalkConfig.enabled && dingTalkConfig.webhook.isNotEmpty() && !dingTalkConfig.webhook.contains("YOUR_ACCESS_TOKEN") } }