From ba032dd4cceee16e512f8f1117a21c3914fe9fcf Mon Sep 17 00:00:00 2001 From: stubenhuang Date: Wed, 15 Nov 2023 10:06:16 +0800 Subject: [PATCH 1/5] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E8=AF=AD?= =?UTF-8?q?=E9=9F=B3=E9=80=9A=E7=9F=A5=20#9686?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../devops/common/notify/enums/NotifyType.kt | 13 +- .../common/notify/pojo/VoiceNotifyPost.kt | 35 ++ .../com/tencent/devops/notify/Constants.kt | 2 + .../devops/notify/pojo/VoiceMessage.kt | 77 +++ .../pojo/messageTemplate/MessageTemplate.kt | 2 + .../pojo/messageTemplate/VoiceTemplate.kt | 14 + .../config/BluekingNotifyConfiguration.kt | 6 - .../devops/notify/blueking/sdk/CMSApi.kt | 14 + .../notify/blueking/sdk/pojo/SendVoiceReq.kt | 35 ++ .../blueking/service/inner/OrgServiceImpl.kt | 51 -- .../notify/blueking/utils/NotifyService.kt | 6 + .../notify/consumer/NotifyMessageConsumer.kt | 50 +- .../devops/notify/dao/MessageTemplateDao.kt | 26 +- .../notify/dao/NotifyMessageTemplateDao.kt | 84 +++ .../devops/notify/dao/VoiceNotifyDao.kt | 60 ++ .../model/VoiceNotifyMessageWithOperation.kt | 16 + .../NotifyMessageTemplateServiceImpl.kt | 592 ++++++------------ .../devops/notify/service/OrgService.kt | 31 - .../devops/notify/service/VoiceService.kt | 13 + .../notify/service/notifier/BaseNotifier.kt | 23 + .../notify/service/notifier/EmailNotifier.kt | 89 +++ .../notify/service/notifier/INotifier.kt | 23 + .../notify/service/notifier/NotifierUtils.kt | 60 ++ .../notify/service/notifier/RtxNotifier.kt | 57 ++ .../notify/service/notifier/VoiceNotifier.kt | 44 ++ .../notify/service/notifier/WechatNotifier.kt | 64 ++ .../service/notifier/WeworkGroupNotifier.kt | 77 +++ .../notify/service/notifier/WeworkNotifier.kt | 47 ++ .../pojo/pipeline/PipelineSubscriptionType.kt | 3 +- support-files/i18n/notify/template_en_US.yaml | 20 + support-files/i18n/notify/template_zh_CN.yaml | 20 + .../sql/1001_ci_notify_ddl_mysql.sql | 31 + 32 files changed, 1155 insertions(+), 530 deletions(-) create mode 100644 src/backend/ci/core/common/common-notify/src/main/kotlin/com/tencent/devops/common/notify/pojo/VoiceNotifyPost.kt create mode 100644 src/backend/ci/core/notify/api-notify/src/main/kotlin/com/tencent/devops/notify/pojo/VoiceMessage.kt create mode 100644 src/backend/ci/core/notify/api-notify/src/main/kotlin/com/tencent/devops/notify/pojo/messageTemplate/VoiceTemplate.kt create mode 100644 src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/blueking/sdk/pojo/SendVoiceReq.kt delete mode 100644 src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/blueking/service/inner/OrgServiceImpl.kt create mode 100644 src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/dao/VoiceNotifyDao.kt create mode 100644 src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/model/VoiceNotifyMessageWithOperation.kt delete mode 100644 src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/OrgService.kt create mode 100644 src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/VoiceService.kt create mode 100644 src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/BaseNotifier.kt create mode 100644 src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/EmailNotifier.kt create mode 100644 src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/INotifier.kt create mode 100644 src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/NotifierUtils.kt create mode 100644 src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/RtxNotifier.kt create mode 100644 src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/VoiceNotifier.kt create mode 100644 src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/WechatNotifier.kt create mode 100644 src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/WeworkGroupNotifier.kt create mode 100644 src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/WeworkNotifier.kt diff --git a/src/backend/ci/core/common/common-notify/src/main/kotlin/com/tencent/devops/common/notify/enums/NotifyType.kt b/src/backend/ci/core/common/common-notify/src/main/kotlin/com/tencent/devops/common/notify/enums/NotifyType.kt index c8bf8002161..834b68f9ed5 100644 --- a/src/backend/ci/core/common/common-notify/src/main/kotlin/com/tencent/devops/common/notify/enums/NotifyType.kt +++ b/src/backend/ci/core/common/common-notify/src/main/kotlin/com/tencent/devops/common/notify/enums/NotifyType.kt @@ -33,5 +33,16 @@ enum class NotifyType { WECHAT, SMS, WEWORK, - WEWORK_GROUP + WEWORK_GROUP, + VOICE + ; + + /** + * OP系统可以编辑的通知模板 + */ + companion object { + fun opEditable(): List { + return listOf(EMAIL, RTX, WECHAT) + } + } } diff --git a/src/backend/ci/core/common/common-notify/src/main/kotlin/com/tencent/devops/common/notify/pojo/VoiceNotifyPost.kt b/src/backend/ci/core/common/common-notify/src/main/kotlin/com/tencent/devops/common/notify/pojo/VoiceNotifyPost.kt new file mode 100644 index 00000000000..a4390b66203 --- /dev/null +++ b/src/backend/ci/core/common/common-notify/src/main/kotlin/com/tencent/devops/common/notify/pojo/VoiceNotifyPost.kt @@ -0,0 +1,35 @@ +package com.tencent.devops.common.notify.pojo + +import com.fasterxml.jackson.annotation.JsonProperty + +/** + * 语音消息 + */ +class VoiceNotifyPost : BaseNotifyPost() { + @JsonProperty("Receiver") + var receiver = "" + + @JsonProperty("TaskName") + var taskName = "" + + @JsonProperty("Content") + var content = "" + + @JsonProperty("TransferReceiver") + var transferReceiver = "" + + @JsonProperty("Interval") + var interval = 0 + + @JsonProperty("RecallTimes") + var recallTimes = 0 + + @JsonProperty("TextNotify.EnableWorkwxNotify") + var workwxNotifyEnabled = false + + @JsonProperty("TextNotify.NotifyTitle") + var workwxNotifyTitle = "" + + @JsonProperty("TextNotify.NotifyContent") + var workwxNotifyContent = "" +} diff --git a/src/backend/ci/core/notify/api-notify/src/main/kotlin/com/tencent/devops/notify/Constants.kt b/src/backend/ci/core/notify/api-notify/src/main/kotlin/com/tencent/devops/notify/Constants.kt index 11afae3c4bd..12701f5c7e7 100644 --- a/src/backend/ci/core/notify/api-notify/src/main/kotlin/com/tencent/devops/notify/Constants.kt +++ b/src/backend/ci/core/notify/api-notify/src/main/kotlin/com/tencent/devops/notify/Constants.kt @@ -34,6 +34,7 @@ const val QUEUE_NOTIFY_EMAIL = "queue_notify_email" const val QUEUE_NOTIFY_SMS = "queue_notify_sms" const val QUEUE_NOTIFY_WEWORK = "queue_notify_wework" const val QUEUE_NOTIFY_PUSH = "queue_notify_push" +const val QUEUE_NOTIFY_VOICE = "queue_notify_voice" const val EXCHANGE_NOTIFY = "exchange_notify" const val ROUTE_RTX = "rtx" const val ROUTE_WECHAT = "wechat" @@ -41,6 +42,7 @@ const val ROUTE_EMAIL = "email" const val ROUTE_SMS = "sms" const val ROUTE_WEWORK = "wework" const val ROUTE_PUSH = "push" +const val ROUTE_VOICE = "voice" const val PIPELINE_QUALITY_AUDIT_NOTIFY_TEMPLATE = "QUALITY_AUDIT_NOTIFY_TEMPLATE" const val PIPELINE_QUALITY_END_NOTIFY_TEMPLATE = "QUALITY_END_NOTIFY_TEMPLATE" diff --git a/src/backend/ci/core/notify/api-notify/src/main/kotlin/com/tencent/devops/notify/pojo/VoiceMessage.kt b/src/backend/ci/core/notify/api-notify/src/main/kotlin/com/tencent/devops/notify/pojo/VoiceMessage.kt new file mode 100644 index 00000000000..61996ec3261 --- /dev/null +++ b/src/backend/ci/core/notify/api-notify/src/main/kotlin/com/tencent/devops/notify/pojo/VoiceMessage.kt @@ -0,0 +1,77 @@ +package com.tencent.devops.notify.pojo + +import com.tencent.devops.common.notify.pojo.VoiceNotifyPost +import io.swagger.annotations.ApiModel +import io.swagger.annotations.ApiModelProperty + +@ApiModel("语音信息") +open class VoiceMessage : BaseMessage() { + @ApiModelProperty("接收人(英文ID),支持多个") + var receivers = mutableSetOf() + + @ApiModelProperty("任务名称,不超过200字符") + var taskName = "" + + @ApiModelProperty("呼叫内容,建议只传简短的文字内容,详细信息通过企业微信提醒方式发送") + var content = "" + + @ApiModelProperty("转接责任人(英文ID),单人") + var transferReceiver = "" + + @ApiModelProperty("重呼间隔(秒),默认为0") + var interval = 0 + + @ApiModelProperty("最大重呼次数,默认为0") + var recallTime = 0 + + @ApiModelProperty("企业微信提醒") + var textNotify = TextNotify.DEFAULT + + @ApiModel("语音信息--企业微信提醒") + data class TextNotify( + @ApiModelProperty("是否开启企业微信提醒") + val enabled: Boolean = false, + @ApiModelProperty("提醒Title") + val title: String = "", + @ApiModelProperty("提醒内容") + val content: String = "" + ) { + companion object { + val DEFAULT = TextNotify(false, "", "") + } + } + + fun addAllReceivers(receiverSet: Set) { + receivers.addAll(receiverSet) + } + + fun clearReceivers() { + receivers.clear() + } + + @Throws(IllegalArgumentException::class) + fun asPost(): VoiceNotifyPost { + checkParams() + val post = VoiceNotifyPost() + post.receiver = this.receivers.joinToString(",") + post.taskName = this.taskName + post.content = this.content + post.transferReceiver = this.transferReceiver + post.interval = this.interval + post.recallTimes = this.recallTime + post.workwxNotifyEnabled = this.textNotify.enabled + post.workwxNotifyTitle = this.textNotify.title + post.workwxNotifyContent = this.textNotify.content + + return post + } + + private fun checkParams() { + if (receivers.isEmpty()) { + throw IllegalArgumentException("receiver can`t not empty") + } + if (taskName.length > 200) { + throw IllegalArgumentException("the length of task name can`t be greater then 200") + } + } +} diff --git a/src/backend/ci/core/notify/api-notify/src/main/kotlin/com/tencent/devops/notify/pojo/messageTemplate/MessageTemplate.kt b/src/backend/ci/core/notify/api-notify/src/main/kotlin/com/tencent/devops/notify/pojo/messageTemplate/MessageTemplate.kt index 6e46c610ac5..b1bd3a7fea8 100644 --- a/src/backend/ci/core/notify/api-notify/src/main/kotlin/com/tencent/devops/notify/pojo/messageTemplate/MessageTemplate.kt +++ b/src/backend/ci/core/notify/api-notify/src/main/kotlin/com/tencent/devops/notify/pojo/messageTemplate/MessageTemplate.kt @@ -55,6 +55,8 @@ data class MessageTemplate( var wechatTemplate: WechatMessageTemplate? = null, @ApiModelProperty("微信群模板", required = false) var weworkGroupTemplate: WeworkGroupMessageTemplate? = null, + @ApiModelProperty("语音模板", required = false) + val voiceTemplate: VoiceTemplate? = null, @ApiModelProperty("创建人", required = true) val creator: String, @ApiModelProperty("修改人", required = true) diff --git a/src/backend/ci/core/notify/api-notify/src/main/kotlin/com/tencent/devops/notify/pojo/messageTemplate/VoiceTemplate.kt b/src/backend/ci/core/notify/api-notify/src/main/kotlin/com/tencent/devops/notify/pojo/messageTemplate/VoiceTemplate.kt new file mode 100644 index 00000000000..18f30d0be34 --- /dev/null +++ b/src/backend/ci/core/notify/api-notify/src/main/kotlin/com/tencent/devops/notify/pojo/messageTemplate/VoiceTemplate.kt @@ -0,0 +1,14 @@ +package com.tencent.devops.notify.pojo.messageTemplate + +import io.swagger.annotations.ApiModel +import io.swagger.annotations.ApiModelProperty + +@ApiModel("语音模板信息") +data class VoiceTemplate( + @ApiModelProperty("模板ID", required = true) + val id: String, + @ApiModelProperty("任务名称", required = false) + var taskName: String, + @ApiModelProperty("内容", required = true) + var content: String +) diff --git a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/blueking/config/BluekingNotifyConfiguration.kt b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/blueking/config/BluekingNotifyConfiguration.kt index 6297282e6d0..2a5795f48ab 100644 --- a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/blueking/config/BluekingNotifyConfiguration.kt +++ b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/blueking/config/BluekingNotifyConfiguration.kt @@ -4,7 +4,6 @@ import com.tencent.devops.notify.blueking.sdk.CMSApi import com.tencent.devops.notify.blueking.sdk.pojo.NotifyProperties import com.tencent.devops.notify.blueking.service.inner.BlueKingWeworkServiceImpl import com.tencent.devops.notify.blueking.service.inner.EmailServiceImpl -import com.tencent.devops.notify.blueking.service.inner.OrgServiceImpl import com.tencent.devops.notify.blueking.service.inner.RtxServiceImpl import com.tencent.devops.notify.blueking.service.inner.SmsServiceImpl import com.tencent.devops.notify.blueking.service.inner.WechatServiceImpl @@ -14,7 +13,6 @@ import com.tencent.devops.notify.dao.RtxNotifyDao import com.tencent.devops.notify.dao.SmsNotifyDao import com.tencent.devops.notify.dao.WechatNotifyDao import com.tencent.devops.notify.service.EmailService -import com.tencent.devops.notify.service.OrgService import com.tencent.devops.notify.service.RtxService import com.tencent.devops.notify.service.SmsService import com.tencent.devops.notify.service.WechatService @@ -66,10 +64,6 @@ class BluekingNotifyConfiguration { @ConditionalOnMissingBean(WeworkService::class) fun weworkService() = BlueKingWeworkServiceImpl() - @Bean - @ConditionalOnMissingBean(OrgService::class) - fun orgService() = OrgServiceImpl() - @Bean @ConditionalOnMissingBean(RtxService::class) fun rtxService( diff --git a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/blueking/sdk/CMSApi.kt b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/blueking/sdk/CMSApi.kt index 8144a0e97e4..d530b3c724d 100644 --- a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/blueking/sdk/CMSApi.kt +++ b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/blueking/sdk/CMSApi.kt @@ -31,6 +31,7 @@ import com.tencent.devops.common.api.exception.ErrorCodeException import com.tencent.devops.common.notify.pojo.EmailNotifyPost import com.tencent.devops.common.notify.pojo.RtxNotifyPost import com.tencent.devops.common.notify.pojo.SmsNotifyPost +import com.tencent.devops.common.notify.pojo.VoiceNotifyPost import com.tencent.devops.common.notify.pojo.WechatNotifyPost import com.tencent.devops.common.web.utils.I18nUtil import com.tencent.devops.notify.blueking.sdk.pojo.ApiReq @@ -40,11 +41,13 @@ import com.tencent.devops.notify.blueking.sdk.pojo.NotifyProperties import com.tencent.devops.notify.blueking.sdk.pojo.SendMailReq import com.tencent.devops.notify.blueking.sdk.pojo.SendQyWxReq import com.tencent.devops.notify.blueking.sdk.pojo.SendSmsReq +import com.tencent.devops.notify.blueking.sdk.pojo.SendVoiceReq import com.tencent.devops.notify.blueking.sdk.pojo.SendWxReq import com.tencent.devops.notify.blueking.utils.NotifyService.Companion.EMAIL_URL import com.tencent.devops.notify.blueking.utils.NotifyService.Companion.NOC_NOTICE_URL import com.tencent.devops.notify.blueking.utils.NotifyService.Companion.RTX_URL import com.tencent.devops.notify.blueking.utils.NotifyService.Companion.SMS_URL +import com.tencent.devops.notify.blueking.utils.NotifyService.Companion.VOICE_URL import com.tencent.devops.notify.blueking.utils.NotifyService.Companion.WECHAT_URL import com.tencent.devops.notify.constant.NotifyMessageCode import com.tencent.devops.notify.constant.NotifyMessageCode.BK_NOTIFY_MESSAGES @@ -140,6 +143,17 @@ class CMSApi(private val notifyProperties: NotifyProperties) { return doPostRequest(WECHAT_URL, wechatReq) } + fun sendVoice(voiceNotifyPost: VoiceNotifyPost): ApiResp { + val voiceReq = with(voiceNotifyPost) { + SendVoiceReq( + auto_read_message = content, + receiver__username = receiver, + bk_username = "蓝鲸助手" + ) + } + return doPostRequest(VOICE_URL, voiceReq) + } + private val logger = LoggerFactory.getLogger(CMSApi::class.java) /** diff --git a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/blueking/sdk/pojo/SendVoiceReq.kt b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/blueking/sdk/pojo/SendVoiceReq.kt new file mode 100644 index 00000000000..c918c76b4d2 --- /dev/null +++ b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/blueking/sdk/pojo/SendVoiceReq.kt @@ -0,0 +1,35 @@ +package com.tencent.devops.notify.blueking.sdk.pojo + +import io.swagger.annotations.ApiModel +import io.swagger.annotations.ApiModelProperty + +@ApiModel("发送语音请求参数") +data class SendVoiceReq( + @ApiModelProperty("自动语音读字信息") + var auto_read_message: String, + @ApiModelProperty( + "待通知的用户列表,自动语音通知列表,若user_list_information、receiver__username同时存在," + + "以user_list_information为准" + ) + var user_list_information: Collection? = null, + @ApiModelProperty( + "待通知的用户列表,包含用户名,用户需在蓝鲸平台注册,多个以逗号分隔," + + "若user_list_information、receiver__username同时存在,以user_list_information为准" + ) + var receiver__username: String? = null, + + override var bk_app_code: String? = "", + override var bk_app_secret: String? = "", + override var bk_token: String? = "", + override var bk_username: String? = "" +) : ApiReq( + bk_app_code, bk_app_secret, bk_token, bk_username +) { + @ApiModel("用户信息") + data class UserListInformation( + @ApiModelProperty("被通知人") + var username: String, + @ApiModelProperty("被通知人手机号") + var mobile_phone: String? = null + ) +} diff --git a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/blueking/service/inner/OrgServiceImpl.kt b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/blueking/service/inner/OrgServiceImpl.kt deleted file mode 100644 index cbc22d1a547..00000000000 --- a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/blueking/service/inner/OrgServiceImpl.kt +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. - * - * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. - * - * BK-CI 蓝鲸持续集成平台 is licensed under the MIT license. - * - * A copy of the MIT License is included in this file. - * - * - * Terms of the MIT License: - * --------------------------------------------------- - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated - * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or substantial portions of - * the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT - * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN - * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, - * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ -package com.tencent.devops.notify.blueking.service.inner - -import com.tencent.devops.notify.service.OrgService - -@Suppress("ALL") -class OrgServiceImpl : OrgService { - - override fun parseStaff(staffs: Set): Set { - val result = LinkedHashSet() - val staffIds = LinkedHashSet() - if (staffs.isNotEmpty()) { - staffs.forEach { staff -> - try { - val staffId = Integer.parseInt(staff, 10) - if (staffId > 10) { - staffIds.add(staffId) - } - } catch (ignore: NumberFormatException) { - result.add(staff) - } - } - } - return result - } -} diff --git a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/blueking/utils/NotifyService.kt b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/blueking/utils/NotifyService.kt index a939fa29569..b6c1d1d7107 100644 --- a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/blueking/utils/NotifyService.kt +++ b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/blueking/utils/NotifyService.kt @@ -29,6 +29,7 @@ package com.tencent.devops.notify.blueking.utils import com.tencent.devops.common.notify.pojo.EmailNotifyPost import com.tencent.devops.common.notify.pojo.RtxNotifyPost import com.tencent.devops.common.notify.pojo.SmsNotifyPost +import com.tencent.devops.common.notify.pojo.VoiceNotifyPost import com.tencent.devops.common.notify.pojo.WechatNotifyPost import com.tencent.devops.notify.blueking.sdk.CMSApi @@ -41,6 +42,7 @@ class NotifyService(private val cmsApi: CMSApi) { const val SMS_URL = "/api/c/compapi/cmsi/send_sms/" const val WECHAT_URL = "/api/c/compapi/cmsi/send_weixin/" const val NOC_NOTICE_URL = "/api/c/compapi/cmsi/noc_notice/" + const val VOICE_URL = "/api/c/compapi/cmsi/send_voice_msg/" } fun post(url: String, postData: Any, tofConf: Map? = null): NotifyResult { @@ -63,6 +65,10 @@ class NotifyService(private val cmsApi: CMSApi) { cmsApi.sendWeixin(postData as WechatNotifyPost) } + VOICE_URL -> { + cmsApi.sendVoice(postData as VoiceNotifyPost) + } + else -> { throw IllegalArgumentException("Unknown message type") } diff --git a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/consumer/NotifyMessageConsumer.kt b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/consumer/NotifyMessageConsumer.kt index 7a055f3238b..9e258020837 100644 --- a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/consumer/NotifyMessageConsumer.kt +++ b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/consumer/NotifyMessageConsumer.kt @@ -32,23 +32,26 @@ import com.tencent.devops.notify.EXCHANGE_NOTIFY import com.tencent.devops.notify.QUEUE_NOTIFY_EMAIL import com.tencent.devops.notify.QUEUE_NOTIFY_RTX import com.tencent.devops.notify.QUEUE_NOTIFY_SMS +import com.tencent.devops.notify.QUEUE_NOTIFY_VOICE import com.tencent.devops.notify.QUEUE_NOTIFY_WECHAT import com.tencent.devops.notify.QUEUE_NOTIFY_WEWORK import com.tencent.devops.notify.ROUTE_EMAIL import com.tencent.devops.notify.ROUTE_RTX import com.tencent.devops.notify.ROUTE_SMS +import com.tencent.devops.notify.ROUTE_VOICE import com.tencent.devops.notify.ROUTE_WECHAT import com.tencent.devops.notify.ROUTE_WEWORK import com.tencent.devops.notify.model.EmailNotifyMessageWithOperation import com.tencent.devops.notify.model.RtxNotifyMessageWithOperation import com.tencent.devops.notify.model.SmsNotifyMessageWithOperation +import com.tencent.devops.notify.model.VoiceNotifyMessageWithOperation import com.tencent.devops.notify.model.WechatNotifyMessageWithOperation import com.tencent.devops.notify.model.WeworkNotifyMessageWithOperation import com.tencent.devops.notify.pojo.WeworkNotifyTextMessage import com.tencent.devops.notify.service.EmailService -import com.tencent.devops.notify.service.OrgService import com.tencent.devops.notify.service.RtxService import com.tencent.devops.notify.service.SmsService +import com.tencent.devops.notify.service.VoiceService import com.tencent.devops.notify.service.WechatService import com.tencent.devops.notify.service.WeworkService import org.slf4j.LoggerFactory @@ -66,7 +69,7 @@ class NotifyMessageConsumer @Autowired constructor( private val smsService: SmsService, private val wechatService: WechatService, private val weworkService: WeworkService, - private val orgService: OrgService + private val voiceService: VoiceService ) { companion object { private val logger = LoggerFactory.getLogger(NotifyMessageConsumer::class.java) @@ -84,9 +87,6 @@ class NotifyMessageConsumer @Autowired constructor( ) fun onReceiveRtxMessage(rtxNotifyMessageWithOperation: RtxNotifyMessageWithOperation) { try { - val parseStaff = orgService.parseStaff(rtxNotifyMessageWithOperation.getReceivers()) - rtxNotifyMessageWithOperation.clearReceivers() - rtxNotifyMessageWithOperation.addAllReceivers(parseStaff) rtxService.sendMessage(rtxNotifyMessageWithOperation) } catch (ignored: Exception) { logger.warn("Failed process received RTX message", ignored) @@ -105,17 +105,6 @@ class NotifyMessageConsumer @Autowired constructor( ) fun onReceiveEmailMessage(emailNotifyMessageWithOperation: EmailNotifyMessageWithOperation) { try { - val parseStaff = orgService.parseStaff(emailNotifyMessageWithOperation.getReceivers()) - val parseBcc = orgService.parseStaff(emailNotifyMessageWithOperation.getBcc()) - val parseCc = orgService.parseStaff(emailNotifyMessageWithOperation.getCc()) - - emailNotifyMessageWithOperation.clearReceivers() - emailNotifyMessageWithOperation.clearBcc() - emailNotifyMessageWithOperation.clearCc() - - emailNotifyMessageWithOperation.addAllBccs(parseBcc) - emailNotifyMessageWithOperation.addAllCcs(parseCc) - emailNotifyMessageWithOperation.addAllReceivers(parseStaff) emailService.sendMessage(emailNotifyMessageWithOperation) } catch (ignored: Exception) { logger.warn("Failed process received Email message", ignored) @@ -134,15 +123,30 @@ class NotifyMessageConsumer @Autowired constructor( ) fun onReceiveSmsMessage(smsNotifyMessageWithOperation: SmsNotifyMessageWithOperation) { try { - val parseStaff = orgService.parseStaff(smsNotifyMessageWithOperation.getReceivers()) - smsNotifyMessageWithOperation.clearReceivers() - smsNotifyMessageWithOperation.addAllReceivers(parseStaff) smsService.sendMessage(smsNotifyMessageWithOperation) } catch (ignored: Exception) { logger.warn("Failed process received SMS message", ignored) } } + @RabbitListener( + containerFactory = "rabbitListenerContainerFactory", + bindings = [ + QueueBinding( + key = [ROUTE_VOICE], + value = Queue(value = QUEUE_NOTIFY_VOICE, durable = "true"), + exchange = Exchange(value = EXCHANGE_NOTIFY, durable = "true", delayed = "true", type = "topic") + ) + ] + ) + fun onReceiveVoiceMessage(voiceNotifyMessageWithOperation: VoiceNotifyMessageWithOperation) { + try { + voiceService.sendMessage(voiceNotifyMessageWithOperation) + } catch (e: Exception) { + logger.warn("Failed process received voice message", e) + } + } + @RabbitListener( containerFactory = "rabbitListenerContainerFactory", bindings = [ @@ -155,9 +159,6 @@ class NotifyMessageConsumer @Autowired constructor( ) fun onReceiveWechatMessage(wechatNotifyMessageWithOperation: WechatNotifyMessageWithOperation) { try { - val parseStaff = orgService.parseStaff(wechatNotifyMessageWithOperation.getReceivers()) - wechatNotifyMessageWithOperation.clearReceivers() - wechatNotifyMessageWithOperation.addAllReceivers(parseStaff) wechatService.sendMessage(wechatNotifyMessageWithOperation) } catch (ignored: Exception) { logger.warn("Failed process received Wechat message", ignored) @@ -176,11 +177,8 @@ class NotifyMessageConsumer @Autowired constructor( ) fun onReceiveWeworkMessage(weworkNotifyMessageWithOperation: WeworkNotifyMessageWithOperation) { try { - val parseStaff = orgService.parseStaff(weworkNotifyMessageWithOperation.getReceivers()) - weworkNotifyMessageWithOperation.clearReceivers() - weworkNotifyMessageWithOperation.addAllReceivers(parseStaff) val weworkNotifyTextMessage = WeworkNotifyTextMessage( - receivers = parseStaff, + receivers = weworkNotifyMessageWithOperation.getReceivers(), receiverType = WeworkReceiverType.single, textType = if (weworkNotifyMessageWithOperation.markdownContent) { WeworkTextType.markdown diff --git a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/dao/MessageTemplateDao.kt b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/dao/MessageTemplateDao.kt index 7e977d78acf..bf7d2e4f4a2 100644 --- a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/dao/MessageTemplateDao.kt +++ b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/dao/MessageTemplateDao.kt @@ -28,11 +28,13 @@ package com.tencent.devops.notify.dao import com.tencent.devops.model.notify.tables.TCommonNotifyMessageTemplate import com.tencent.devops.model.notify.tables.TEmailsNotifyMessageTemplate +import com.tencent.devops.model.notify.tables.TVoiceNotifyMessageTemplate import com.tencent.devops.model.notify.tables.TWechatNotifyMessageTemplate import com.tencent.devops.model.notify.tables.TWeworkGroupNotifyMessageTemplate import com.tencent.devops.model.notify.tables.TWeworkNotifyMessageTemplate import com.tencent.devops.model.notify.tables.records.TCommonNotifyMessageTemplateRecord import com.tencent.devops.model.notify.tables.records.TEmailsNotifyMessageTemplateRecord +import com.tencent.devops.model.notify.tables.records.TVoiceNotifyMessageTemplateRecord import com.tencent.devops.model.notify.tables.records.TWechatNotifyMessageTemplateRecord import com.tencent.devops.model.notify.tables.records.TWeworkGroupNotifyMessageTemplateRecord import com.tencent.devops.model.notify.tables.records.TWeworkNotifyMessageTemplateRecord @@ -43,17 +45,18 @@ import org.springframework.stereotype.Repository @Suppress("ALL") class MessageTemplateDao { - fun crateCommonNotifyMessageTemplate(dslContext: DSLContext, template: TCommonNotifyMessageTemplateRecord) { + fun createCommonNotifyMessageTemplate(dslContext: DSLContext, template: TCommonNotifyMessageTemplateRecord) { with(TCommonNotifyMessageTemplate.T_COMMON_NOTIFY_MESSAGE_TEMPLATE) { dslContext.insertInto(this) .set(template) .onDuplicateKeyUpdate() .set(TEMPLATE_NAME, template.templateName) + .set(NOTIFY_TYPE_SCOPE, template.notifyTypeScope) .execute() } } - fun crateWechatNotifyMessageTemplate(dslContext: DSLContext, template: TWechatNotifyMessageTemplateRecord) { + fun createWechatNotifyMessageTemplate(dslContext: DSLContext, template: TWechatNotifyMessageTemplateRecord) { with(TWechatNotifyMessageTemplate.T_WECHAT_NOTIFY_MESSAGE_TEMPLATE) { dslContext.insertInto(this) .set(template) @@ -66,7 +69,7 @@ class MessageTemplateDao { } } - fun crateWeworkGroupNotifyMessageTemplate( + fun createWeworkGroupNotifyMessageTemplate( dslContext: DSLContext, template: TWeworkGroupNotifyMessageTemplateRecord ) { @@ -82,7 +85,7 @@ class MessageTemplateDao { } } - fun crateWeworkNotifyMessageTemplate(dslContext: DSLContext, template: TWeworkNotifyMessageTemplateRecord) { + fun createWeworkNotifyMessageTemplate(dslContext: DSLContext, template: TWeworkNotifyMessageTemplateRecord) { with(TWeworkNotifyMessageTemplate.T_WEWORK_NOTIFY_MESSAGE_TEMPLATE) { dslContext.insertInto(this) .set(template) @@ -95,7 +98,7 @@ class MessageTemplateDao { } } - fun crateEmailsNotifyMessageTemplate(dslContext: DSLContext, template: TEmailsNotifyMessageTemplateRecord) { + fun createEmailsNotifyMessageTemplate(dslContext: DSLContext, template: TEmailsNotifyMessageTemplateRecord) { with(TEmailsNotifyMessageTemplate.T_EMAILS_NOTIFY_MESSAGE_TEMPLATE) { dslContext.insertInto(this) .set(template) @@ -107,4 +110,17 @@ class MessageTemplateDao { .execute() } } + + fun createVoiceNotifyMessageTemplate(dslContext: DSLContext, template: TVoiceNotifyMessageTemplateRecord) { + with(TVoiceNotifyMessageTemplate.T_VOICE_NOTIFY_MESSAGE_TEMPLATE) { + dslContext.insertInto(this) + .set(template) + .onDuplicateKeyUpdate() + .set(TASK_NAME, template.taskName) + .set(CONTENT, template.content) + .set(MODIFIOR, template.modifior) + .set(UPDATE_TIME, template.updateTime) + .execute() + } + } } diff --git a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/dao/NotifyMessageTemplateDao.kt b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/dao/NotifyMessageTemplateDao.kt index bbd1a135f04..ca6747f8f01 100644 --- a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/dao/NotifyMessageTemplateDao.kt +++ b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/dao/NotifyMessageTemplateDao.kt @@ -30,11 +30,13 @@ import com.tencent.devops.common.api.util.JsonUtil import com.tencent.devops.model.notify.tables.TCommonNotifyMessageTemplate import com.tencent.devops.model.notify.tables.TEmailsNotifyMessageTemplate import com.tencent.devops.model.notify.tables.TRtxNotifyMessageTemplate +import com.tencent.devops.model.notify.tables.TVoiceNotifyMessageTemplate import com.tencent.devops.model.notify.tables.TWechatNotifyMessageTemplate import com.tencent.devops.model.notify.tables.TWeworkNotifyMessageTemplate import com.tencent.devops.model.notify.tables.records.TCommonNotifyMessageTemplateRecord import com.tencent.devops.model.notify.tables.records.TEmailsNotifyMessageTemplateRecord import com.tencent.devops.model.notify.tables.records.TRtxNotifyMessageTemplateRecord +import com.tencent.devops.model.notify.tables.records.TVoiceNotifyMessageTemplateRecord import com.tencent.devops.model.notify.tables.records.TWechatNotifyMessageTemplateRecord import com.tencent.devops.model.notify.tables.records.TWeworkNotifyMessageTemplateRecord import com.tencent.devops.notify.pojo.NotifyTemplateMessage @@ -108,6 +110,22 @@ class NotifyMessageTemplateDao { } } + /** + * 获取语音消息模板 + * @param dslContext + * @param commonTemplateId + */ + fun getVoiceNotifyMessageTemplate( + dslContext: DSLContext, + commonTemplateId: String + ): TVoiceNotifyMessageTemplateRecord? { + with(TVoiceNotifyMessageTemplate.T_VOICE_NOTIFY_MESSAGE_TEMPLATE) { + return dslContext.selectFrom(this) + .where(COMMON_TEMPLATE_ID.eq(commonTemplateId)) + .fetchOne() + } + } + /** * 获取企业微信消息模板 * @param dslContext 数据库操作对象 @@ -306,6 +324,38 @@ class NotifyMessageTemplateDao { } } + fun addVoiceNotifyMessageTemplate( + dslContext: DSLContext, + commonTemplateId: String, + id: String, + userId: String, + notifyTemplateMessage: NotifyTemplateMessage + ) { + with(TVoiceNotifyMessageTemplate.T_VOICE_NOTIFY_MESSAGE_TEMPLATE) { + val now = LocalDateTime.now() + dslContext.insertInto( + this, + ID, + COMMON_TEMPLATE_ID, + CREATOR, + MODIFIOR, + TASK_NAME, + CONTENT, + CREATE_TIME, + UPDATE_TIME + ).values( + id, + commonTemplateId, + userId, + userId, + notifyTemplateMessage.title, + notifyTemplateMessage.body, + now, + now + ).execute() + } + } + fun addWECHATNotifyMessageTemplate( dslContext: DSLContext, id: String, @@ -369,6 +419,14 @@ class NotifyMessageTemplateDao { } } + fun deleteVoiceNotifyMessageTemplate(dslContext: DSLContext, commonTemplateId: String): Int { + with(TVoiceNotifyMessageTemplate.T_VOICE_NOTIFY_MESSAGE_TEMPLATE) { + return dslContext.deleteFrom(this) + .where(COMMON_TEMPLATE_ID.eq(commonTemplateId)) + .execute() + } + } + /** * 删除企业微信类型的消息通知模板信息 */ @@ -414,6 +472,23 @@ class NotifyMessageTemplateDao { } } + fun updateVoiceNotifyMessageTemplate( + dslContext: DSLContext, + userId: String, + templateId: String, + notifyTemplateMessage: NotifyTemplateMessage + ): Int { + with(TVoiceNotifyMessageTemplate.T_VOICE_NOTIFY_MESSAGE_TEMPLATE) { + return dslContext.update(this) + .set(MODIFIOR, userId) + .set(TASK_NAME, notifyTemplateMessage.title) + .set(CONTENT, notifyTemplateMessage.body) + .set(UPDATE_TIME, LocalDateTime.now()) + .where(COMMON_TEMPLATE_ID.eq(templateId)) + .execute() + } + } + /** * 根据模板ID,更新企业微信消息模板信息 * @param userId 更新用户ID @@ -520,4 +595,13 @@ class NotifyMessageTemplateDao { .fetchOne(0, Int::class.java)!! } } + + fun countVoiceMessageTemplate(dslContext: DSLContext, templateId: String): Int { + with(TVoiceNotifyMessageTemplate.T_VOICE_NOTIFY_MESSAGE_TEMPLATE) { + return dslContext.selectCount() + .from(this) + .where(COMMON_TEMPLATE_ID.eq(templateId)) + .fetchOne(0, Int::class.java)!! + } + } } diff --git a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/dao/VoiceNotifyDao.kt b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/dao/VoiceNotifyDao.kt new file mode 100644 index 00000000000..359359cf5e0 --- /dev/null +++ b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/dao/VoiceNotifyDao.kt @@ -0,0 +1,60 @@ +package com.tencent.devops.notify.dao + +import com.tencent.devops.model.notify.tables.TNotifyVoice +import org.jooq.DSLContext +import org.springframework.stereotype.Repository +import java.time.LocalDateTime + +@Repository +class VoiceNotifyDao { + fun insertOrUpdate( + dslContext: DSLContext, + id: String, + success: Boolean, + receivers: String, + taskName: String, + content: String, + transferReceiver: String, + retryCount: Int, + lastError: String?, + tofSysId: String?, + fromSysId: String? + ) { + val now = LocalDateTime.now() + with(TNotifyVoice.T_NOTIFY_VOICE) { + dslContext.insertInto( + this, + ID, + SUCCESS, + RECEIVERS, + TASK_NAME, + CONTENT, + TRANSFER_RECEIVER, + RETRY_COUNT, + LAST_ERROR, + TOF_SYS_ID, + FROM_SYS_ID, + CREATED_TIME, + UPDATED_TIME + ).values( + id, + success, + receivers, + taskName, + content, + transferReceiver, + retryCount, + lastError, + tofSysId, + fromSysId, + now, + now + ).onDuplicateKeyUpdate() + .set(SUCCESS, success) + .set(UPDATED_TIME, now) + .set(LAST_ERROR, lastError) + .set(RETRY_COUNT, retryCount) + .execute() + } + } +} diff --git a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/model/VoiceNotifyMessageWithOperation.kt b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/model/VoiceNotifyMessageWithOperation.kt new file mode 100644 index 00000000000..184842c9b05 --- /dev/null +++ b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/model/VoiceNotifyMessageWithOperation.kt @@ -0,0 +1,16 @@ +package com.tencent.devops.notify.model + +import com.tencent.devops.notify.pojo.VoiceMessage + +class VoiceNotifyMessageWithOperation : VoiceMessage() { + var id: String? = null + var retryCount: Int = 0 + var lastError: String? = null + + override fun toString(): String { + return String.format( + "id(%s), retryCount(%s), message(%s) ", + id, retryCount, super.toString() + ) + } +} diff --git a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/NotifyMessageTemplateServiceImpl.kt b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/NotifyMessageTemplateServiceImpl.kt index 183cfc27721..f3c5e460f90 100644 --- a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/NotifyMessageTemplateServiceImpl.kt +++ b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/NotifyMessageTemplateServiceImpl.kt @@ -35,50 +35,41 @@ import com.tencent.devops.common.api.util.PageUtil import com.tencent.devops.common.api.util.UUIDUtil import com.tencent.devops.common.api.util.YamlUtil import com.tencent.devops.common.api.util.timestampmilli -import com.tencent.devops.common.notify.enums.EnumEmailFormat -import com.tencent.devops.common.notify.enums.EnumEmailType -import com.tencent.devops.common.notify.enums.EnumNotifyPriority -import com.tencent.devops.common.notify.enums.EnumNotifySource import com.tencent.devops.common.notify.enums.NotifyType -import com.tencent.devops.common.notify.utils.NotifyUtils import com.tencent.devops.common.redis.RedisLock import com.tencent.devops.common.redis.RedisOperation import com.tencent.devops.common.service.config.CommonConfig +import com.tencent.devops.common.service.utils.SpringContextUtil import com.tencent.devops.common.web.utils.I18nUtil -import com.tencent.devops.common.wechatwork.WechatWorkRobotService -import com.tencent.devops.common.wechatwork.WechatWorkService import com.tencent.devops.model.notify.tables.records.TCommonNotifyMessageTemplateRecord import com.tencent.devops.model.notify.tables.records.TEmailsNotifyMessageTemplateRecord +import com.tencent.devops.model.notify.tables.records.TVoiceNotifyMessageTemplateRecord import com.tencent.devops.model.notify.tables.records.TWechatNotifyMessageTemplateRecord import com.tencent.devops.model.notify.tables.records.TWeworkGroupNotifyMessageTemplateRecord import com.tencent.devops.model.notify.tables.records.TWeworkNotifyMessageTemplateRecord import com.tencent.devops.notify.dao.CommonNotifyMessageTemplateDao import com.tencent.devops.notify.dao.MessageTemplateDao import com.tencent.devops.notify.dao.NotifyMessageTemplateDao -import com.tencent.devops.notify.model.WeworkNotifyMessageWithOperation -import com.tencent.devops.notify.pojo.EmailNotifyMessage import com.tencent.devops.notify.pojo.NotifyContext import com.tencent.devops.notify.pojo.NotifyMessageCommonTemplate import com.tencent.devops.notify.pojo.NotifyMessageContextRequest import com.tencent.devops.notify.pojo.NotifyTemplateMessage import com.tencent.devops.notify.pojo.NotifyTemplateMessageRequest -import com.tencent.devops.notify.pojo.RtxNotifyMessage import com.tencent.devops.notify.pojo.SendNotifyMessageTemplateRequest import com.tencent.devops.notify.pojo.SubNotifyMessageTemplate -import com.tencent.devops.notify.pojo.WechatNotifyMessage import com.tencent.devops.notify.pojo.messageTemplate.MessageTemplate -import java.io.File -import java.time.LocalDateTime -import java.util.concurrent.Executors -import java.util.regex.Pattern -import javax.annotation.PostConstruct +import com.tencent.devops.notify.service.notifier.INotifier +import com.tencent.devops.notify.service.notifier.NotifierUtils import org.jooq.DSLContext import org.jooq.impl.DSL import org.slf4j.LoggerFactory import org.springframework.beans.factory.annotation.Autowired -import org.springframework.beans.factory.annotation.Value import org.springframework.core.io.ClassPathResource import org.springframework.stereotype.Service +import java.io.File +import java.time.LocalDateTime +import java.util.concurrent.Executors +import javax.annotation.PostConstruct @Service @Suppress("ALL") @@ -86,12 +77,6 @@ class NotifyMessageTemplateServiceImpl @Autowired constructor( private val dslContext: DSLContext, private val notifyMessageTemplateDao: NotifyMessageTemplateDao, private val commonNotifyMessageTemplateDao: CommonNotifyMessageTemplateDao, - private val emailService: EmailService, - private val rtxService: RtxService, - private val wechatService: WechatService, - private val weworkService: WeworkService, - private val wechatWorkService: WechatWorkService, - private val wechatWorkRobotService: WechatWorkRobotService, private val redisOperation: RedisOperation, private val messageTemplateDao: MessageTemplateDao, private val commonConfig: CommonConfig @@ -99,12 +84,8 @@ class NotifyMessageTemplateServiceImpl @Autowired constructor( companion object { private val logger = LoggerFactory.getLogger(NotifyMessageTemplateServiceImpl::class.java) - private const val chatPatten = "^[A-Za-z0-9_-]+\$" // 数字和字母组成的群chatId正则表达式 } - @Value("\${wework.domain}") - private val userUseDomain: Boolean? = true - @PostConstruct fun init() { val redisLock = RedisLock( @@ -143,7 +124,7 @@ class NotifyMessageTemplateServiceImpl @Autowired constructor( tCommonNotifyMessageTemplateRecord.notifyTypeScope = JsonUtil.toJson(template.notifyTypeScope) tCommonNotifyMessageTemplateRecord.priority = template.priority.ordinal.toByte() tCommonNotifyMessageTemplateRecord.source = template.source.getValue().toByte() - messageTemplateDao.crateCommonNotifyMessageTemplate( + messageTemplateDao.createCommonNotifyMessageTemplate( dslContext, tCommonNotifyMessageTemplateRecord ) @@ -203,24 +184,40 @@ class NotifyMessageTemplateServiceImpl @Autowired constructor( this.updateTime = LocalDateTime.now() } } + val tVoiceNotifyMessageTemplateRecord = template.voiceTemplate?.let { + val voiceTemplate = template.voiceTemplate!! + TVoiceNotifyMessageTemplateRecord().apply { + this.id = voiceTemplate.id + this.commonTemplateId = template.id + this.creator = template.creator + this.modifior = template.modifior + this.taskName = voiceTemplate.taskName + this.content = voiceTemplate.content + this.createTime = LocalDateTime.now() + this.updateTime = LocalDateTime.now() + } + } - dslContext.transaction { configuratin -> - val transactionContext = DSL.using(configuratin) - messageTemplateDao.crateCommonNotifyMessageTemplate( + dslContext.transaction { configuration -> + val transactionContext = DSL.using(configuration) + messageTemplateDao.createCommonNotifyMessageTemplate( transactionContext, tCommonNotifyMessageTemplateRecord ) tWechatNotifyMessageTemplateRecord?.let { record -> - messageTemplateDao.crateWechatNotifyMessageTemplate(transactionContext, record) + messageTemplateDao.createWechatNotifyMessageTemplate(transactionContext, record) } tWeworkNotifyMessageTemplateRecord?.let { record -> - messageTemplateDao.crateWeworkNotifyMessageTemplate(transactionContext, record) + messageTemplateDao.createWeworkNotifyMessageTemplate(transactionContext, record) } tWeworkGroupNotifyMessageTemplateRecord?.let { record -> - messageTemplateDao.crateWeworkGroupNotifyMessageTemplate(transactionContext, record) + messageTemplateDao.createWeworkGroupNotifyMessageTemplate(transactionContext, record) } tEmailsNotifyMessageTemplateRecord?.let { record -> - messageTemplateDao.crateEmailsNotifyMessageTemplate(transactionContext, record) + messageTemplateDao.createEmailsNotifyMessageTemplate(transactionContext, record) + } + tVoiceNotifyMessageTemplateRecord?.let { record -> + messageTemplateDao.createVoiceNotifyMessageTemplate(transactionContext, record) } } } @@ -456,6 +453,15 @@ class NotifyMessageTemplateServiceImpl @Autowired constructor( notifyTemplateMessage = it ) } + if (it.notifyTypeScope.contains(NotifyType.VOICE.name)) { + notifyMessageTemplateDao.addVoiceNotifyMessageTemplate( + dslContext = context, + commonTemplateId = id, + id = UUIDUtil.generate(), + userId = userId, + notifyTemplateMessage = it + ) + } } } return Result(true) @@ -472,46 +478,22 @@ class NotifyMessageTemplateServiceImpl @Autowired constructor( templateId: String, notifyMessageTemplateRequest: NotifyTemplateMessageRequest ): Result { - var hasEmail = false - var hasRtx = false - var hasWechat = false val notifyTypeScopeSet = mutableSetOf() // 判断提交的数据中是否存在同样类型的 notifyMessageTemplateRequest.msg.forEach { - if (it.notifyTypeScope.contains(NotifyType.EMAIL.name) && !hasEmail) { - hasEmail = true - notifyTypeScopeSet.add(NotifyType.EMAIL.name) - } else if (it.notifyTypeScope.contains(NotifyType.EMAIL.name) && hasEmail) { - return I18nUtil.generateResponseDataObject( - messageCode = CommonMessageCode.PARAMETER_IS_INVALID, - params = arrayOf("notifyType"), - data = false, - language = I18nUtil.getLanguage(userId) - ) - } - - if (it.notifyTypeScope.contains(NotifyType.RTX.name) && !hasRtx) { - hasRtx = true - notifyTypeScopeSet.add(NotifyType.RTX.name) - } else if (it.notifyTypeScope.contains(NotifyType.RTX.name) && hasRtx) { - return I18nUtil.generateResponseDataObject( - messageCode = CommonMessageCode.PARAMETER_IS_INVALID, - params = arrayOf("notifyType"), - data = false, - language = I18nUtil.getLanguage(userId) - ) - } - - if (it.notifyTypeScope.contains(NotifyType.WECHAT.name) && !hasWechat) { - hasWechat = true - notifyTypeScopeSet.add(NotifyType.WECHAT.name) - } else if (it.notifyTypeScope.contains(NotifyType.WECHAT.name) && hasWechat) { - return I18nUtil.generateResponseDataObject( - messageCode = CommonMessageCode.PARAMETER_IS_INVALID, - params = arrayOf("notifyType"), - data = false, - language = I18nUtil.getLanguage(userId) - ) + for (notifyType in NotifyType.opEditable()) { + if (it.notifyTypeScope.contains(notifyType.name)) { + if (notifyTypeScopeSet.contains(notifyType.name)) { + logger.warn("${notifyType.name} has set.") + return I18nUtil.generateResponseDataObject( + messageCode = CommonMessageCode.PARAMETER_IS_INVALID, + params = arrayOf("notifyType"), + data = false, + language = I18nUtil.getLanguage(userId) + ) + } + notifyTypeScopeSet.add(notifyType.name) + } } } @@ -537,61 +519,16 @@ class NotifyMessageTemplateServiceImpl @Autowired constructor( // 根据模板类型向消息模板信息表中添加信息 notifyMessageTemplateRequest.msg.forEach { if (it.notifyTypeScope.contains(NotifyType.WECHAT.name)) { - val num = notifyMessageTemplateDao.countWechatMessageTemplate(dslContext, templateId) - if (num > 0) { - notifyMessageTemplateDao.updateWechatNotifyMessageTemplate( - dslContext = dslContext, - userId = userId, - templateId = templateId, - notifyMessageTemplate = it - ) - } else { - notifyMessageTemplateDao.addWECHATNotifyMessageTemplate( - dslContext = dslContext, - id = templateId, - newId = uid, - userId = userId, - notifyTemplateMessage = it - ) - } + upsetWechatTemplate(templateId, userId, it, uid) } if (it.notifyTypeScope.contains(NotifyType.RTX.name)) { - val num = notifyMessageTemplateDao.countRtxMessageTemplate(dslContext, templateId) - if (num > 0) { - notifyMessageTemplateDao.updateRtxNotifyMessageTemplate( - dslContext = dslContext, - userId = userId, - templateId = templateId, - notifyMessageTemplate = it - ) - } else { - notifyMessageTemplateDao.addRTXNotifyMessageTemplate( - dslContext = dslContext, - id = templateId, - newId = uid, - userId = userId, - notifyTemplateMessage = it - ) - } + upsetRtxTemplate(templateId, userId, it, uid) } if (it.notifyTypeScope.contains(NotifyType.EMAIL.name)) { - val num = notifyMessageTemplateDao.countEmailMessageTemplate(dslContext, templateId) - if (num > 0) { - notifyMessageTemplateDao.updateEmailsNotifyMessageTemplate( - dslContext = dslContext, - userId = userId, - templateId = templateId, - notifyTemplateMessage = it - ) - } else { - notifyMessageTemplateDao.addEmailsNotifyMessageTemplate( - dslContext = dslContext, - id = templateId, - newId = uid, - userId = userId, - addNotifyTemplateMessage = it - ) - } + upsetEmailTemplate(templateId, userId, it, uid) + } + if (it.notifyTypeScope.contains(NotifyType.VOICE.name)) { + upsetVoiceTemplate(templateId, userId, it, uid) } updateOtherSpecialTemplate(it, templateId, uid, userId) } @@ -641,12 +578,18 @@ class NotifyMessageTemplateServiceImpl @Autowired constructor( NotifyType.EMAIL.name -> { notifyMessageTemplateDao.deleteEmailsNotifyMessageTemplate(context, templateId) } + NotifyType.RTX.name -> { notifyMessageTemplateDao.deleteRtxNotifyMessageTemplate(context, templateId) } + NotifyType.WECHAT.name -> { notifyMessageTemplateDao.deleteWechatNotifyMessageTemplate(context, templateId) } + + NotifyType.VOICE.name -> { + notifyMessageTemplateDao.deleteVoiceNotifyMessageTemplate(context, templateId) + } } if (existsNotifyType.size == 1 && existsNotifyType[0] == notifyType) { @@ -672,6 +615,7 @@ class NotifyMessageTemplateServiceImpl @Autowired constructor( notifyMessageTemplateDao.deleteEmailsNotifyMessageTemplate(dsl, templateId) notifyMessageTemplateDao.deleteRtxNotifyMessageTemplate(dsl, templateId) notifyMessageTemplateDao.deleteWechatNotifyMessageTemplate(dsl, templateId) + notifyMessageTemplateDao.deleteVoiceNotifyMessageTemplate(dsl, templateId) } return Result(true) } @@ -690,124 +634,17 @@ class NotifyMessageTemplateServiceImpl @Autowired constructor( val sendAllNotify = request.notifyType == null val notifyTypeScope = commonNotifyMessageTemplateRecord.notifyTypeScope - // 邮件消息 - if (sendAllNotify || request.notifyType?.contains(NotifyType.EMAIL.name) == true) { - if (!notifyTypeScope.contains(NotifyType.EMAIL.name)) { - logger.warn( - "COMMON_NOTIFY_MESSAGE_TEMPLATE_NOT_FOUND|If needed, add on the OP" + - "|type=${NotifyType.EMAIL}|template=${request.templateCode}" - ) - } else { - val emailTplRecord = notifyMessageTemplateDao.getEmailNotifyMessageTemplate( - dslContext, - commonNotifyMessageTemplateRecord.id - )!! - // 替换标题里的动态参数 - val title = replaceContentParams(request.titleParams, emailTplRecord.title) - // 替换内容里的动态参数 - val body = replaceContentEmailParams(request.bodyParams, emailTplRecord.body) - sendEmailNotifyMessage( - commonNotifyMessageTemplate = commonNotifyMessageTemplateRecord, - sendNotifyMessageTemplateRequest = request, - title = title, - body = body, - sender = emailTplRecord.sender, - variables = request.titleParams?.plus(request.bodyParams ?: emptyMap()) ?: emptyMap(), - tencentCloudTemplateId = emailTplRecord.tencentCloudTemplateId - ) - } - } - - // 企业微信消息 - if (sendAllNotify || request.notifyType?.contains(NotifyType.RTX.name) == true) { - if (!notifyTypeScope.contains(NotifyType.RTX.name)) { - logger.warn( - "COMMON_NOTIFY_MESSAGE_TEMPLATE_NOT_FOUND|If needed, add on the OP" + - "|type=${NotifyType.RTX}|template=${request.templateCode}" - ) - } else { - logger.info("send wework msg: ${commonNotifyMessageTemplateRecord.id}") - val weworkTplRecord = - notifyMessageTemplateDao.getRtxNotifyMessageTemplate( - dslContext = dslContext, - commonTemplateId = commonNotifyMessageTemplateRecord.id - )!! - val title = replaceContentParams(request.titleParams, weworkTplRecord.title) - // 替换内容里的动态参数 - val body = replaceContentParams( - request.bodyParams, - if (request.markdownContent == true) { - weworkTplRecord.bodyMd ?: weworkTplRecord.body - } else { - weworkTplRecord.body - } - ) - logger.info("send wework msg: $body ${weworkTplRecord.sender}") - sendWeworkNotifyMessage( - commonNotifyMessageTemplate = commonNotifyMessageTemplateRecord, - sendNotifyMessageTemplateRequest = request, - body = "$title" + "\n\n" + "$body", - sender = weworkTplRecord.sender - ) - } - } - - // 微信消息 - if (sendAllNotify || request.notifyType?.contains(NotifyType.WECHAT.name) == true) { - if (!notifyTypeScope.contains(NotifyType.WECHAT.name)) { - logger.warn( - "COMMON_NOTIFY_MESSAGE_TEMPLATE_NOT_FOUND|If needed, add on the OP" + - "|type=${NotifyType.WECHAT}|template=${request.templateCode}" - ) - } else { - val wechatTplRecord = notifyMessageTemplateDao.getWechatNotifyMessageTemplate( - dslContext = dslContext, - commonTemplateId = commonNotifyMessageTemplateRecord.id - )!! - // 替换内容里的动态参数 - val body = replaceContentParams(request.bodyParams, wechatTplRecord.body) - sendWechatNotifyMessage( - commonNotifyMessageTemplate = commonNotifyMessageTemplateRecord, - sendNotifyMessageTemplateRequest = request, - body = body, - sender = wechatTplRecord.sender - ) - } - } - - // 新企业微信实现 - if (sendAllNotify || request.notifyType?.contains(NotifyType.WEWORK.name) == true) { - if (!notifyTypeScope.contains(NotifyType.WEWORK.name)) { - logger.warn( - "COMMON_NOTIFY_MESSAGE_TEMPLATE_NOT_FOUND|If needed, add on the OP" + - "|type=${NotifyType.WEWORK}|template=${request.templateCode}" - ) - } else { - val weworkTplRecord = notifyMessageTemplateDao.getWeworkNotifyMessageTemplate( - dslContext = dslContext, - commonTemplateId = commonNotifyMessageTemplateRecord.id - )!! - // 替换内容里的动态参数 - val title = replaceContentParams(request.titleParams, weworkTplRecord.title) - val body = replaceContentParams(request.bodyParams, weworkTplRecord.body) - sendWeworkNotifyMessage( - commonNotifyMessageTemplate = commonNotifyMessageTemplateRecord, - sendNotifyMessageTemplateRequest = request, - body = "$title" + "\n\n" + "$body", - sender = weworkTplRecord.sender - ) - } - } - - if (sendAllNotify || request.notifyType?.contains(NotifyType.WEWORK_GROUP.name) == true) { - if (!notifyTypeScope.contains("WEWORK_GROUP")) { - logger.warn( - "COMMON_NOTIFY_MESSAGE_TEMPLATE_NOT_FOUND|If needed, add on the OP" + - "|type=WEWORK_GROUP|template=${request.templateCode}" - ) - } else { - logger.info("send WEWORK_GROUP msg: $commonNotifyMessageTemplateRecord.id") - sendWeworkGroupNotifyMessage(request, commonNotifyMessageTemplateRecord.id) + val notifiers = SpringContextUtil.getBeansWithClass(INotifier::class.java) + for (notifier in notifiers) { + if (sendAllNotify || request.notifyType?.contains(notifier.type().name) == true) { + if (!notifyTypeScope.contains(notifier.type().name)) { + logger.warn( + "COMMON_NOTIFY_MESSAGE_TEMPLATE_NOT_FOUND|If needed, add on the OP" + + "|type=${notifier.type()}|template=${request.templateCode}" + ) + } else { + notifier.send(request, commonNotifyMessageTemplateRecord) + } } } @@ -833,8 +670,8 @@ class NotifyMessageTemplateServiceImpl @Autowired constructor( override fun getNotifyMessageByTemplate(request: NotifyMessageContextRequest): Result { logger.info( "getNotifyMessageByTemplate|templateCode=${request.templateCode}|" + - "notifyTypeEnum=${request.notifyType.name}|" + - "titleParams=${request.titleParams}|bodyParams=${request.bodyParams}" + "notifyTypeEnum=${request.notifyType.name}|" + + "titleParams=${request.titleParams}|bodyParams=${request.bodyParams}" ) // 1.查出消息模板 val commonNotifyMessageTemplateRecord = @@ -851,206 +688,143 @@ class NotifyMessageTemplateServiceImpl @Autowired constructor( dslContext, commonNotifyMessageTemplateRecord.id )!! - val title = replaceContentParams(request.titleParams, emailTplRecord.title) - val body = replaceContentParams(request.bodyParams, emailTplRecord.body) + val title = NotifierUtils.replaceContentParams(request.titleParams, emailTplRecord.title) + val body = NotifierUtils.replaceContentParams(request.bodyParams, emailTplRecord.body) NotifyContext(title, body) } + NotifyType.RTX.name -> { val rtxTplRecord = notifyMessageTemplateDao.getRtxNotifyMessageTemplate( dslContext = dslContext, commonTemplateId = commonNotifyMessageTemplateRecord.id )!! - val title = replaceContentParams(request.titleParams, rtxTplRecord.title) - val body = replaceContentParams(request.bodyParams, rtxTplRecord.body) + val title = NotifierUtils.replaceContentParams(request.titleParams, rtxTplRecord.title) + val body = NotifierUtils.replaceContentParams(request.bodyParams, rtxTplRecord.body) NotifyContext(title, body) } + NotifyType.WECHAT.name -> { val wechatTplRecord = notifyMessageTemplateDao.getWechatNotifyMessageTemplate( dslContext = dslContext, commonTemplateId = commonNotifyMessageTemplateRecord.id )!! - val title = replaceContentParams(request.titleParams, wechatTplRecord.title) - val body = replaceContentParams(request.bodyParams, wechatTplRecord.body) + val title = NotifierUtils.replaceContentParams(request.titleParams, wechatTplRecord.title) + val body = NotifierUtils.replaceContentParams(request.bodyParams, wechatTplRecord.body) + NotifyContext(title, body) + } + + NotifyType.VOICE.name -> { + val voiceTplRecord = notifyMessageTemplateDao.getVoiceNotifyMessageTemplate( + dslContext = dslContext, + commonTemplateId = commonNotifyMessageTemplateRecord.id + )!! + val title = NotifierUtils.replaceContentParams(request.titleParams, voiceTplRecord.taskName) + val body = NotifierUtils.replaceContentParams(request.bodyParams, voiceTplRecord.content) NotifyContext(title, body) } + else -> null } return Result(notifyContext) } - private fun sendRtxNotifyMessage( - commonNotifyMessageTemplate: TCommonNotifyMessageTemplateRecord, - sendNotifyMessageTemplateRequest: SendNotifyMessageTemplateRequest, - title: String, - body: String, - sender: String + private fun upsetEmailTemplate( + templateId: String, + userId: String, + it: NotifyTemplateMessage, + uid: String ) { - logger.info("sendRtxNotifyMessage:\ntitle:$title,\nbody:$body") - val rtxNotifyMessage = RtxNotifyMessage() - rtxNotifyMessage.sender = sender - rtxNotifyMessage.addAllReceivers(sendNotifyMessageTemplateRequest.receivers) - // 企业微信通知触发人 - val triggerUserId = sendNotifyMessageTemplateRequest.bodyParams?.get("cc") - if (null != triggerUserId && "" != triggerUserId && - !sendNotifyMessageTemplateRequest.receivers.contains(triggerUserId) - ) { - rtxNotifyMessage.addReceiver(triggerUserId) + val num = notifyMessageTemplateDao.countEmailMessageTemplate(dslContext, templateId) + if (num > 0) { + notifyMessageTemplateDao.updateEmailsNotifyMessageTemplate( + dslContext = dslContext, + userId = userId, + templateId = templateId, + notifyTemplateMessage = it + ) + } else { + notifyMessageTemplateDao.addEmailsNotifyMessageTemplate( + dslContext = dslContext, + id = templateId, + newId = uid, + userId = userId, + addNotifyTemplateMessage = it + ) } - rtxNotifyMessage.title = title - rtxNotifyMessage.body = body - rtxNotifyMessage.priority = EnumNotifyPriority.parse(commonNotifyMessageTemplate.priority.toString()) - rtxNotifyMessage.source = EnumNotifySource.parse(commonNotifyMessageTemplate.source.toInt()) - ?: EnumNotifySource.BUSINESS_LOGIC - rtxService.sendMqMsg(rtxNotifyMessage) } - private fun sendWechatNotifyMessage( - commonNotifyMessageTemplate: TCommonNotifyMessageTemplateRecord, - sendNotifyMessageTemplateRequest: SendNotifyMessageTemplateRequest, - body: String, - sender: String - ) { - logger.info("sendWechatNotifyMessage:\nbody:$body") - val wechatNotifyMessage = WechatNotifyMessage() - wechatNotifyMessage.sender = sender - wechatNotifyMessage.addAllReceivers(sendNotifyMessageTemplateRequest.receivers) - wechatNotifyMessage.body = body - wechatNotifyMessage.priority = EnumNotifyPriority.parse(commonNotifyMessageTemplate.priority.toString()) - wechatNotifyMessage.source = EnumNotifySource.parse(commonNotifyMessageTemplate.source.toInt()) - ?: EnumNotifySource.BUSINESS_LOGIC - wechatService.sendMqMsg(wechatNotifyMessage) - } - - private fun sendEmailNotifyMessage( - commonNotifyMessageTemplate: TCommonNotifyMessageTemplateRecord, - sendNotifyMessageTemplateRequest: SendNotifyMessageTemplateRequest, - title: String, - body: String, - sender: String, - variables: Map, - tencentCloudTemplateId: Int? + private fun upsetVoiceTemplate( + templateId: String, + userId: String, + it: NotifyTemplateMessage, + uid: String ) { - logger.info("sendEmailNotifyMessage:\ntitle:$title,\nbody:$body") - val commonTemplateId = commonNotifyMessageTemplate.id - val emailNotifyMessageTemplate = - notifyMessageTemplateDao.getEmailNotifyMessageTemplate(dslContext, commonTemplateId) - val emailNotifyMessage = EmailNotifyMessage() - emailNotifyMessage.sender = sender - emailNotifyMessage.addAllReceivers(sendNotifyMessageTemplateRequest.receivers) - val cc = sendNotifyMessageTemplateRequest.cc - if (null != cc) { - emailNotifyMessage.addAllCcs(cc) - } - val bcc = sendNotifyMessageTemplateRequest.bcc - if (null != bcc) { - emailNotifyMessage.addAllBccs(bcc) + val num = notifyMessageTemplateDao.countVoiceMessageTemplate(dslContext, templateId) + if (num > 0) { + notifyMessageTemplateDao.updateVoiceNotifyMessageTemplate( + dslContext = dslContext, + userId = userId, + templateId = templateId, + notifyTemplateMessage = it + ) + } else { + notifyMessageTemplateDao.addVoiceNotifyMessageTemplate( + dslContext = dslContext, + commonTemplateId = templateId, + id = uid, + userId = userId, + notifyTemplateMessage = it + ) } - emailNotifyMessage.title = title - emailNotifyMessage.body = body - emailNotifyMessage.variables = variables - emailNotifyMessage.tencentCloudTemplateId = tencentCloudTemplateId - emailNotifyMessage.priority = EnumNotifyPriority.parse(commonNotifyMessageTemplate.priority.toString()) - emailNotifyMessage.source = EnumNotifySource.parse(commonNotifyMessageTemplate.source.toInt()) - ?: EnumNotifySource.BUSINESS_LOGIC - emailNotifyMessage.format = EnumEmailFormat.parse(emailNotifyMessageTemplate!!.bodyFormat.toInt()) - emailNotifyMessage.type = EnumEmailType.parse(emailNotifyMessageTemplate.emailType.toInt()) - emailService.sendMqMsg(emailNotifyMessage) } - private fun sendWeworkNotifyMessage( - commonNotifyMessageTemplate: TCommonNotifyMessageTemplateRecord, - sendNotifyMessageTemplateRequest: SendNotifyMessageTemplateRequest, - body: String, - sender: String - ) { - val wechatNotifyMessage = WeworkNotifyMessageWithOperation() - wechatNotifyMessage.sender = sender - wechatNotifyMessage.addAllReceivers(findWeworkUser(sendNotifyMessageTemplateRequest.receivers)) - wechatNotifyMessage.body = body - wechatNotifyMessage.priority = EnumNotifyPriority.parse(commonNotifyMessageTemplate.priority.toString()) - wechatNotifyMessage.source = EnumNotifySource.parse(commonNotifyMessageTemplate.source.toInt()) - ?: EnumNotifySource.BUSINESS_LOGIC - wechatNotifyMessage.markdownContent = sendNotifyMessageTemplateRequest.markdownContent ?: false - weworkService.sendMqMsg(wechatNotifyMessage) - } - - private fun sendWeworkGroupNotifyMessage( - request: SendNotifyMessageTemplateRequest, - commonTemplateId: String + private fun upsetRtxTemplate( + templateId: String, + userId: String, + it: NotifyTemplateMessage, + uid: String ) { - val groups = request.bodyParams?.get(NotifyUtils.WEWORK_GROUP_KEY)?.split(",") - if (groups.isNullOrEmpty()) { - logger.info("wework group is empty, so return.") - return - } - val weworkTplRecord = - notifyMessageTemplateDao.getRtxNotifyMessageTemplate( + val num = notifyMessageTemplateDao.countRtxMessageTemplate(dslContext, templateId) + if (num > 0) { + notifyMessageTemplateDao.updateRtxNotifyMessageTemplate( dslContext = dslContext, - commonTemplateId = commonTemplateId - )!! - val title = replaceContentParams(request.titleParams, weworkTplRecord.title) - // 替换内容里的动态参数 - val body = replaceContentParams( - request.bodyParams, - if (request.markdownContent == true) { - weworkTplRecord.bodyMd ?: weworkTplRecord.body - } else { - weworkTplRecord.body - } - ) - - val content = title + "\n\n" + body - - groups.forEach { - if (it.startsWith("ww")) { // 应用号逻辑 - wechatWorkService.sendByApp( - chatId = it, - content = content, - markerDownFlag = request.markdownContent ?: false - ) - } else if (Pattern.matches(chatPatten, it)) { // 机器人逻辑 - wechatWorkRobotService.sendByRobot( - chatId = it, - content = content, - markerDownFlag = request.markdownContent ?: false - ) - } - } - } - - protected fun replaceContentParams(params: Map?, content: String): String { - var content1 = content - params?.forEach { (paramName, paramValue) -> - content1 = content1.replace("\${$paramName}", paramValue).replace("#{$paramName}", paramValue) - .replace("{{$paramName}}", paramValue) - } - return content1 - } - - protected fun replaceContentEmailParams(params: Map?, content: String): String { - var content1 = content - params?.forEach { (paramName, paramValue) -> - val replaceValue = paramValue.replace("\n", "
") - content1 = content1.replace("\${$paramName}", replaceValue).replace("#{$paramName}", replaceValue) - .replace("{{$paramName}}", replaceValue) + userId = userId, + templateId = templateId, + notifyMessageTemplate = it + ) + } else { + notifyMessageTemplateDao.addRTXNotifyMessageTemplate( + dslContext = dslContext, + id = templateId, + newId = uid, + userId = userId, + notifyTemplateMessage = it + ) } - return content1 } - // #5318 为解决使用蓝鲸用户中心生成了带域名的用户名无法与企业微信账号对齐问题 - private fun findWeworkUser(userSet: Set): Set { - if (userUseDomain!!) { - val weworkUserSet = mutableSetOf() - userSet.forEach { - // 若用户名包含域,取域前的用户名. - if (it.contains("@")) { - weworkUserSet.add(it.substringBefore("@")) - } else { - weworkUserSet.add(it) - } - } - return weworkUserSet + private fun upsetWechatTemplate( + templateId: String, + userId: String, + it: NotifyTemplateMessage, + uid: String + ) { + val num = notifyMessageTemplateDao.countWechatMessageTemplate(dslContext, templateId) + if (num > 0) { + notifyMessageTemplateDao.updateWechatNotifyMessageTemplate( + dslContext = dslContext, + userId = userId, + templateId = templateId, + notifyMessageTemplate = it + ) + } else { + notifyMessageTemplateDao.addWECHATNotifyMessageTemplate( + dslContext = dslContext, + id = templateId, + newId = uid, + userId = userId, + notifyTemplateMessage = it + ) } - return userSet } } diff --git a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/OrgService.kt b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/OrgService.kt deleted file mode 100644 index 67d9f777a2e..00000000000 --- a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/OrgService.kt +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available. - * - * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. - * - * BK-CI 蓝鲸持续集成平台 is licensed under the MIT license. - * - * A copy of the MIT License is included in this file. - * - * - * Terms of the MIT License: - * --------------------------------------------------- - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated - * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the - * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to - * permit persons to whom the Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or substantial portions of - * the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT - * LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN - * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, - * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE - * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ -package com.tencent.devops.notify.service - -interface OrgService { - fun parseStaff(staffs: Set): Set -} diff --git a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/VoiceService.kt b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/VoiceService.kt new file mode 100644 index 00000000000..c21b557c27b --- /dev/null +++ b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/VoiceService.kt @@ -0,0 +1,13 @@ +package com.tencent.devops.notify.service + +import com.tencent.devops.notify.model.VoiceNotifyMessageWithOperation +import com.tencent.devops.notify.pojo.VoiceMessage + +/** + * 语音服务 + */ +interface VoiceService { + fun sendMqMsg(message: VoiceMessage) + + fun sendMessage(voiceNotifyMessageWithOperation: VoiceNotifyMessageWithOperation) +} diff --git a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/BaseNotifier.kt b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/BaseNotifier.kt new file mode 100644 index 00000000000..b395b224fe4 --- /dev/null +++ b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/BaseNotifier.kt @@ -0,0 +1,23 @@ +package com.tencent.devops.notify.service.notifier + +import com.tencent.devops.common.notify.enums.NotifyType +import com.tencent.devops.model.notify.tables.records.TCommonNotifyMessageTemplateRecord +import com.tencent.devops.notify.pojo.SendNotifyMessageTemplateRequest + +/** + * 通知器 + */ +abstract class BaseNotifier { + /** + * 支持类型 + */ + abstract fun type(): NotifyType + + /** + * 发送 + */ + abstract fun send( + request: SendNotifyMessageTemplateRequest, // 请求 + commonNotifyMessageTemplateRecord: TCommonNotifyMessageTemplateRecord // 模板 + ) +} diff --git a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/EmailNotifier.kt b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/EmailNotifier.kt new file mode 100644 index 00000000000..7947fdd8417 --- /dev/null +++ b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/EmailNotifier.kt @@ -0,0 +1,89 @@ +package com.tencent.devops.notify.service.notifier + +import com.tencent.devops.common.notify.enums.EnumEmailFormat +import com.tencent.devops.common.notify.enums.EnumEmailType +import com.tencent.devops.common.notify.enums.EnumNotifyPriority +import com.tencent.devops.common.notify.enums.EnumNotifySource +import com.tencent.devops.common.notify.enums.NotifyType +import com.tencent.devops.model.notify.tables.records.TCommonNotifyMessageTemplateRecord +import com.tencent.devops.notify.dao.NotifyMessageTemplateDao +import com.tencent.devops.notify.pojo.EmailNotifyMessage +import com.tencent.devops.notify.pojo.SendNotifyMessageTemplateRequest +import com.tencent.devops.notify.service.EmailService +import org.jooq.DSLContext +import org.slf4j.LoggerFactory +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.stereotype.Component + +@Component +class EmailNotifier @Autowired constructor( + private val emailService: EmailService, + private val notifyMessageTemplateDao: NotifyMessageTemplateDao, + private val dslContext: DSLContext +) : INotifier { + override fun type(): NotifyType = NotifyType.EMAIL + override fun send( + request: SendNotifyMessageTemplateRequest, + commonNotifyMessageTemplateRecord: TCommonNotifyMessageTemplateRecord + ) { + val emailTplRecord = notifyMessageTemplateDao.getEmailNotifyMessageTemplate( + dslContext, + commonNotifyMessageTemplateRecord.id + )!! + // 替换标题里的动态参数 + val title = NotifierUtils.replaceContentParams(request.titleParams, emailTplRecord.title) + // 替换内容里的动态参数 + val body = NotifierUtils.replaceContentParams(request.bodyParams, emailTplRecord.body) { + it.replace("\n", "
") + } + sendEmailNotifyMessage( + commonNotifyMessageTemplate = commonNotifyMessageTemplateRecord, + sendNotifyMessageTemplateRequest = request, + title = title, + body = body, + sender = emailTplRecord.sender, + variables = request.titleParams?.plus(request.bodyParams ?: emptyMap()) ?: emptyMap(), + tencentCloudTemplateId = emailTplRecord.tencentCloudTemplateId + ) + } + + private fun sendEmailNotifyMessage( + commonNotifyMessageTemplate: TCommonNotifyMessageTemplateRecord, + sendNotifyMessageTemplateRequest: SendNotifyMessageTemplateRequest, + title: String, + body: String, + sender: String, + variables: Map, + tencentCloudTemplateId: Int? + ) { + logger.info("sendEmailNotifyMessage:\ntitle:$title,\nbody:$body") + val commonTemplateId = commonNotifyMessageTemplate.id + val emailNotifyMessageTemplate = + notifyMessageTemplateDao.getEmailNotifyMessageTemplate(dslContext, commonTemplateId) + val emailNotifyMessage = EmailNotifyMessage() + emailNotifyMessage.sender = sender + emailNotifyMessage.addAllReceivers(sendNotifyMessageTemplateRequest.receivers) + val cc = sendNotifyMessageTemplateRequest.cc + if (null != cc) { + emailNotifyMessage.addAllCcs(cc) + } + val bcc = sendNotifyMessageTemplateRequest.bcc + if (null != bcc) { + emailNotifyMessage.addAllBccs(bcc) + } + emailNotifyMessage.title = title + emailNotifyMessage.body = body + emailNotifyMessage.variables = variables + emailNotifyMessage.tencentCloudTemplateId = tencentCloudTemplateId + emailNotifyMessage.priority = EnumNotifyPriority.parse(commonNotifyMessageTemplate.priority.toString()) + emailNotifyMessage.source = EnumNotifySource.parse(commonNotifyMessageTemplate.source.toInt()) + ?: EnumNotifySource.BUSINESS_LOGIC + emailNotifyMessage.format = EnumEmailFormat.parse(emailNotifyMessageTemplate!!.bodyFormat.toInt()) + emailNotifyMessage.type = EnumEmailType.parse(emailNotifyMessageTemplate.emailType.toInt()) + emailService.sendMqMsg(emailNotifyMessage) + } + + companion object { + private val logger = LoggerFactory.getLogger(EmailNotifier::class.java) + } +} diff --git a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/INotifier.kt b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/INotifier.kt new file mode 100644 index 00000000000..5fa57fc729e --- /dev/null +++ b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/INotifier.kt @@ -0,0 +1,23 @@ +package com.tencent.devops.notify.service.notifier + +import com.tencent.devops.common.notify.enums.NotifyType +import com.tencent.devops.model.notify.tables.records.TCommonNotifyMessageTemplateRecord +import com.tencent.devops.notify.pojo.SendNotifyMessageTemplateRequest + +/** + * 通知器 + */ +interface INotifier { + /** + * 支持类型 + */ + fun type(): NotifyType + + /** + * 发送 + */ + fun send( + request: SendNotifyMessageTemplateRequest, // 请求 + commonNotifyMessageTemplateRecord: TCommonNotifyMessageTemplateRecord // 模板 + ) +} diff --git a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/NotifierUtils.kt b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/NotifierUtils.kt new file mode 100644 index 00000000000..4840dc43023 --- /dev/null +++ b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/NotifierUtils.kt @@ -0,0 +1,60 @@ +package com.tencent.devops.notify.service.notifier + +import com.tencent.devops.common.notify.enums.EnumNotifyPriority +import com.tencent.devops.common.notify.enums.EnumNotifySource +import com.tencent.devops.model.notify.tables.records.TCommonNotifyMessageTemplateRecord +import com.tencent.devops.notify.model.WeworkNotifyMessageWithOperation +import com.tencent.devops.notify.pojo.SendNotifyMessageTemplateRequest +import com.tencent.devops.notify.service.WeworkService + +object NotifierUtils { + fun sendWeworkNotifyMessage( + commonNotifyMessageTemplate: TCommonNotifyMessageTemplateRecord, + sendNotifyMessageTemplateRequest: SendNotifyMessageTemplateRequest, + body: String, + sender: String, + weworkService: WeworkService, + userUseDomain: Boolean + ) { + val wechatNotifyMessage = WeworkNotifyMessageWithOperation() + wechatNotifyMessage.sender = sender + wechatNotifyMessage.addAllReceivers(findWeworkUser(sendNotifyMessageTemplateRequest.receivers, userUseDomain)) + wechatNotifyMessage.body = body + wechatNotifyMessage.priority = EnumNotifyPriority.parse(commonNotifyMessageTemplate.priority.toString()) + wechatNotifyMessage.source = EnumNotifySource.parse(commonNotifyMessageTemplate.source.toInt()) + ?: EnumNotifySource.BUSINESS_LOGIC + wechatNotifyMessage.markdownContent = sendNotifyMessageTemplateRequest.markdownContent ?: false + weworkService.sendMqMsg(wechatNotifyMessage) + } + + fun replaceContentParams( + params: Map?, + content: String, + action: (String) -> String = { it } + ): String { + var contentTmp = content + params?.forEach { (paramName, paramValue) -> + val replaceValue = action(paramValue) + contentTmp = contentTmp.replace("\${$paramName}", replaceValue).replace("#{$paramName}", replaceValue) + .replace("{{$paramName}}", replaceValue) + } + return contentTmp + } + + // #5318 为解决使用蓝鲸用户中心生成了带域名的用户名无法与企业微信账号对齐问题 + private fun findWeworkUser(userSet: Set, userUseDomain: Boolean): Set { + if (userUseDomain) { + val weworkUserSet = mutableSetOf() + userSet.forEach { + // 若用户名包含域,取域前的用户名. + if (it.contains("@")) { + weworkUserSet.add(it.substringBefore("@")) + } else { + weworkUserSet.add(it) + } + } + return weworkUserSet + } + return userSet + } +} diff --git a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/RtxNotifier.kt b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/RtxNotifier.kt new file mode 100644 index 00000000000..80fe6894e0f --- /dev/null +++ b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/RtxNotifier.kt @@ -0,0 +1,57 @@ +package com.tencent.devops.notify.service.notifier + +import com.tencent.devops.common.notify.enums.NotifyType +import com.tencent.devops.model.notify.tables.records.TCommonNotifyMessageTemplateRecord +import com.tencent.devops.notify.dao.NotifyMessageTemplateDao +import com.tencent.devops.notify.pojo.SendNotifyMessageTemplateRequest +import com.tencent.devops.notify.service.WeworkService +import org.jooq.DSLContext +import org.slf4j.LoggerFactory +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.beans.factory.annotation.Value +import org.springframework.stereotype.Component + +@Component +class RtxNotifier @Autowired constructor( + private val weworkService: WeworkService, + private val notifyMessageTemplateDao: NotifyMessageTemplateDao, + private val dslContext: DSLContext +) : INotifier { + @Value("\${wework.domain}") + private val userUseDomain: Boolean = true + override fun type(): NotifyType = NotifyType.RTX + override fun send( + request: SendNotifyMessageTemplateRequest, + commonNotifyMessageTemplateRecord: TCommonNotifyMessageTemplateRecord + ) { + logger.info("send wework msg: ${commonNotifyMessageTemplateRecord.id}") + val weworkTplRecord = + notifyMessageTemplateDao.getRtxNotifyMessageTemplate( + dslContext = dslContext, + commonTemplateId = commonNotifyMessageTemplateRecord.id + )!! + val title = NotifierUtils.replaceContentParams(request.titleParams, weworkTplRecord.title) + // 替换内容里的动态参数 + val body = NotifierUtils.replaceContentParams( + request.bodyParams, + if (request.markdownContent == true) { + weworkTplRecord.bodyMd ?: weworkTplRecord.body + } else { + weworkTplRecord.body + } + ) + logger.info("send wework msg: $body ${weworkTplRecord.sender}") + NotifierUtils.sendWeworkNotifyMessage( + commonNotifyMessageTemplate = commonNotifyMessageTemplateRecord, + sendNotifyMessageTemplateRequest = request, + body = "$title\n\n$body", + sender = weworkTplRecord.sender, + weworkService = weworkService, + userUseDomain = userUseDomain + ) + } + + companion object { + private val logger = LoggerFactory.getLogger(RtxNotifier::class.java) + } +} diff --git a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/VoiceNotifier.kt b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/VoiceNotifier.kt new file mode 100644 index 00000000000..b8caa4f0df0 --- /dev/null +++ b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/VoiceNotifier.kt @@ -0,0 +1,44 @@ +package com.tencent.devops.notify.service.notifier + +import com.tencent.devops.common.notify.enums.NotifyType +import com.tencent.devops.model.notify.tables.records.TCommonNotifyMessageTemplateRecord +import com.tencent.devops.notify.dao.NotifyMessageTemplateDao +import com.tencent.devops.notify.pojo.SendNotifyMessageTemplateRequest +import com.tencent.devops.notify.pojo.VoiceMessage +import com.tencent.devops.notify.service.VoiceService +import org.jooq.DSLContext +import org.slf4j.LoggerFactory +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.stereotype.Component + +@Component +class VoiceNotifier @Autowired constructor( + private val voiceService: VoiceService, + private val notifyMessageTemplateDao: NotifyMessageTemplateDao, + private val dslContext: DSLContext +) : INotifier { + override fun type(): NotifyType = NotifyType.VOICE + + override fun send( + request: SendNotifyMessageTemplateRequest, + commonNotifyMessageTemplateRecord: TCommonNotifyMessageTemplateRecord + ) { + val voiceTplRecord = notifyMessageTemplateDao.getVoiceNotifyMessageTemplate( + dslContext = dslContext, + commonTemplateId = commonNotifyMessageTemplateRecord.id + )!! + val taskName = NotifierUtils.replaceContentParams(request.titleParams, voiceTplRecord.taskName) + val content = NotifierUtils.replaceContentParams(request.bodyParams, voiceTplRecord.content) + logger.info("send voice msg , ${commonNotifyMessageTemplateRecord.id} , taskName:$taskName , content:$content") + + val message = VoiceMessage() + message.receivers = request.receivers + message.taskName = taskName + message.content = content + voiceService.sendMqMsg(message) + } + + companion object { + private val logger = LoggerFactory.getLogger(VoiceNotifier::class.java) + } +} diff --git a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/WechatNotifier.kt b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/WechatNotifier.kt new file mode 100644 index 00000000000..3b6ad964ec0 --- /dev/null +++ b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/WechatNotifier.kt @@ -0,0 +1,64 @@ +package com.tencent.devops.notify.service.notifier + +import com.tencent.devops.common.notify.enums.EnumNotifyPriority +import com.tencent.devops.common.notify.enums.EnumNotifySource +import com.tencent.devops.common.notify.enums.NotifyType +import com.tencent.devops.model.notify.tables.records.TCommonNotifyMessageTemplateRecord +import com.tencent.devops.notify.dao.NotifyMessageTemplateDao +import com.tencent.devops.notify.pojo.SendNotifyMessageTemplateRequest +import com.tencent.devops.notify.pojo.WechatNotifyMessage +import com.tencent.devops.notify.service.WechatService +import org.jooq.DSLContext +import org.slf4j.LoggerFactory +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.beans.factory.annotation.Value +import org.springframework.stereotype.Component + +@Component +class WechatNotifier @Autowired constructor( + private val wechatService: WechatService, + private val notifyMessageTemplateDao: NotifyMessageTemplateDao, + private val dslContext: DSLContext +) : INotifier { + @Value("\${wework.domain}") + private val userUseDomain: Boolean = true + override fun type(): NotifyType = NotifyType.WECHAT + override fun send( + request: SendNotifyMessageTemplateRequest, + commonNotifyMessageTemplateRecord: TCommonNotifyMessageTemplateRecord + ) { + val wechatTplRecord = notifyMessageTemplateDao.getWechatNotifyMessageTemplate( + dslContext = dslContext, + commonTemplateId = commonNotifyMessageTemplateRecord.id + )!! + // 替换内容里的动态参数 + val body = NotifierUtils.replaceContentParams(request.bodyParams, wechatTplRecord.body) + sendWechatNotifyMessage( + commonNotifyMessageTemplate = commonNotifyMessageTemplateRecord, + sendNotifyMessageTemplateRequest = request, + body = body, + sender = wechatTplRecord.sender + ) + } + + private fun sendWechatNotifyMessage( + commonNotifyMessageTemplate: TCommonNotifyMessageTemplateRecord, + sendNotifyMessageTemplateRequest: SendNotifyMessageTemplateRequest, + body: String, + sender: String + ) { + logger.info("sendWechatNotifyMessage:\nbody:$body") + val wechatNotifyMessage = WechatNotifyMessage() + wechatNotifyMessage.sender = sender + wechatNotifyMessage.addAllReceivers(sendNotifyMessageTemplateRequest.receivers) + wechatNotifyMessage.body = body + wechatNotifyMessage.priority = EnumNotifyPriority.parse(commonNotifyMessageTemplate.priority.toString()) + wechatNotifyMessage.source = EnumNotifySource.parse(commonNotifyMessageTemplate.source.toInt()) + ?: EnumNotifySource.BUSINESS_LOGIC + wechatService.sendMqMsg(wechatNotifyMessage) + } + + companion object { + private val logger = LoggerFactory.getLogger(WechatNotifier::class.java) + } +} diff --git a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/WeworkGroupNotifier.kt b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/WeworkGroupNotifier.kt new file mode 100644 index 00000000000..2aac7ff87ee --- /dev/null +++ b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/WeworkGroupNotifier.kt @@ -0,0 +1,77 @@ +package com.tencent.devops.notify.service.notifier + +import com.tencent.devops.common.notify.enums.NotifyType +import com.tencent.devops.common.notify.utils.NotifyUtils +import com.tencent.devops.common.wechatwork.WechatWorkRobotService +import com.tencent.devops.common.wechatwork.WechatWorkService +import com.tencent.devops.model.notify.tables.records.TCommonNotifyMessageTemplateRecord +import com.tencent.devops.notify.dao.NotifyMessageTemplateDao +import com.tencent.devops.notify.pojo.SendNotifyMessageTemplateRequest +import org.jooq.DSLContext +import org.slf4j.LoggerFactory +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.beans.factory.annotation.Value +import org.springframework.stereotype.Component +import java.util.regex.Pattern + +@Component +class WeworkGroupNotifier @Autowired constructor( + private val wechatWorkService: WechatWorkService, + private val wechatWorkRobotService: WechatWorkRobotService, + private val notifyMessageTemplateDao: NotifyMessageTemplateDao, + private val dslContext: DSLContext +) : INotifier { + @Value("\${wework.domain}") + private val userUseDomain: Boolean = true + + private val chatPatten = "^[A-Za-z0-9_-]+\$" // 数字和字母组成的群chatId正则表达式 + override fun type(): NotifyType = NotifyType.WEWORK_GROUP + override fun send( + request: SendNotifyMessageTemplateRequest, + commonNotifyMessageTemplateRecord: TCommonNotifyMessageTemplateRecord + ) { + logger.info("send WEWORK_GROUP msg: $commonNotifyMessageTemplateRecord.id") + val groups = request.bodyParams?.get(NotifyUtils.WEWORK_GROUP_KEY)?.split(",") + if (groups.isNullOrEmpty()) { + logger.info("wework group is empty, so return.") + return + } + val weworkTplRecord = + notifyMessageTemplateDao.getRtxNotifyMessageTemplate( + dslContext = dslContext, + commonTemplateId = commonNotifyMessageTemplateRecord.id + )!! + val title = NotifierUtils.replaceContentParams(request.titleParams, weworkTplRecord.title) + // 替换内容里的动态参数 + val body = NotifierUtils.replaceContentParams( + request.bodyParams, + if (request.markdownContent == true) { + weworkTplRecord.bodyMd ?: weworkTplRecord.body + } else { + weworkTplRecord.body + } + ) + + val content = title + "\n\n" + body + + groups.forEach { + if (it.startsWith("ww")) { // 应用号逻辑 + wechatWorkService.sendByApp( + chatId = it, + content = content, + markerDownFlag = request.markdownContent ?: false + ) + } else if (Pattern.matches(chatPatten, it)) { // 机器人逻辑 + wechatWorkRobotService.sendByRobot( + chatId = it, + content = content, + markerDownFlag = request.markdownContent ?: false + ) + } + } + } + + companion object { + private val logger = LoggerFactory.getLogger(WeworkGroupNotifier::class.java) + } +} diff --git a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/WeworkNotifier.kt b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/WeworkNotifier.kt new file mode 100644 index 00000000000..7d935efd3f7 --- /dev/null +++ b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/WeworkNotifier.kt @@ -0,0 +1,47 @@ +package com.tencent.devops.notify.service.notifier + +import com.tencent.devops.common.notify.enums.NotifyType +import com.tencent.devops.model.notify.tables.records.TCommonNotifyMessageTemplateRecord +import com.tencent.devops.notify.dao.NotifyMessageTemplateDao +import com.tencent.devops.notify.pojo.SendNotifyMessageTemplateRequest +import com.tencent.devops.notify.service.WeworkService +import org.jooq.DSLContext +import org.slf4j.LoggerFactory +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.beans.factory.annotation.Value +import org.springframework.stereotype.Component + +@Component +class WeworkNotifier @Autowired constructor( + private val weworkService: WeworkService, + private val notifyMessageTemplateDao: NotifyMessageTemplateDao, + private val dslContext: DSLContext +) : INotifier { + @Value("\${wework.domain}") + private val userUseDomain: Boolean = true + override fun type(): NotifyType = NotifyType.WEWORK + override fun send( + request: SendNotifyMessageTemplateRequest, + commonNotifyMessageTemplateRecord: TCommonNotifyMessageTemplateRecord + ) { + val weworkTplRecord = notifyMessageTemplateDao.getWeworkNotifyMessageTemplate( + dslContext = dslContext, + commonTemplateId = commonNotifyMessageTemplateRecord.id + )!! + // 替换内容里的动态参数 + val title = NotifierUtils.replaceContentParams(request.titleParams, weworkTplRecord.title) + val body = NotifierUtils.replaceContentParams(request.bodyParams, weworkTplRecord.body) + NotifierUtils.sendWeworkNotifyMessage( + commonNotifyMessageTemplate = commonNotifyMessageTemplateRecord, + sendNotifyMessageTemplateRequest = request, + body = "$title\n\n$body", + sender = weworkTplRecord.sender, + weworkService = weworkService, + userUseDomain = userUseDomain + ) + } + + companion object { + private val logger = LoggerFactory.getLogger(WeworkNotifier::class.java) + } +} diff --git a/src/backend/ci/core/process/api-process/src/main/kotlin/com/tencent/devops/process/pojo/pipeline/PipelineSubscriptionType.kt b/src/backend/ci/core/process/api-process/src/main/kotlin/com/tencent/devops/process/pojo/pipeline/PipelineSubscriptionType.kt index d26c1c68c12..2b20c8d2da2 100644 --- a/src/backend/ci/core/process/api-process/src/main/kotlin/com/tencent/devops/process/pojo/pipeline/PipelineSubscriptionType.kt +++ b/src/backend/ci/core/process/api-process/src/main/kotlin/com/tencent/devops/process/pojo/pipeline/PipelineSubscriptionType.kt @@ -35,5 +35,6 @@ enum class PipelineSubscriptionType { RTX, WECHAT, SMS, - WEWORK + WEWORK, + VOICE } diff --git a/support-files/i18n/notify/template_en_US.yaml b/support-files/i18n/notify/template_en_US.yaml index e045c2550ab..c1053ed7280 100644 --- a/support-files/i18n/notify/template_en_US.yaml +++ b/support-files/i18n/notify/template_en_US.yaml @@ -132,6 +132,7 @@ notifyTypeScope: - "EMAIL" - "WEWORK" + - "VOICE" priority: 1 source: 0 emailTemplate: @@ -146,6 +147,10 @@ sender: "DevOps" title: "BK-CI pipeline [${pipelineName}] # ${buildNum} built successfully" body: "✔️ ${successContent}\nView details: ${detailUrl}" + voiceTemplate: + id: "jgj46v0knzlkhcah57rcutao30tqza5v" + taskName: "Build Success" + content: "${successContent}" creator: "system" modifior: "system" - index: 7 @@ -331,6 +336,7 @@ notifyTypeScope: - "EMAIL" - "WEWORK" + - "VOICE" priority: 1 source: 0 emailTemplate: @@ -345,6 +351,10 @@ sender: "DevOps" title: "Failed to build Blue Shield pipeline [${pipelineName}] # ${buildNum}" body: "❌${failContent}" + voiceTemplate: + id: "qlzxaotr2zvr8gnj1dkpj2x5lx19wkpt" + taskName: "Build Failure" + content: "${failContent}" creator: "system" modifior: "system" - index: 17 @@ -449,6 +459,7 @@ notifyTypeScope: - "EMAIL" - "WEWORK" + - "VOICE" priority: 1 source: 0 emailTemplate: @@ -463,6 +474,10 @@ sender: "DevOps" title: "Failed to build Blue Shield pipeline [${pipelineName}] # ${buildNum}" body: "❌ ${failContent}\n\nView details: ${detailUrl}" + voiceTemplate: + id: "x79oetbqquqfjhep7zifheqe9b3tkwvx" + taskName: "Build Failure" + content: "${failContent}" creator: "system" modifior: "system" - index: 23 @@ -587,6 +602,7 @@ notifyTypeScope: - "EMAIL" - "WEWORK" + - "VOICE" priority: 1 source: 0 emailTemplate: @@ -601,6 +617,10 @@ sender: "DevOps" title: "Blue Shield pipeline [${pipelineName}] # ${buildNum} built successfully" body: "✔️${successContent}" + voiceTemplate: + id: "zjeufw8tfnf53tnibu4s43leju8xgnor" + taskName: "Build Success" + content: "${successContent}" creator: "system" modifior: "system" - index: 29 diff --git a/support-files/i18n/notify/template_zh_CN.yaml b/support-files/i18n/notify/template_zh_CN.yaml index b0b886f839b..18481133360 100644 --- a/support-files/i18n/notify/template_zh_CN.yaml +++ b/support-files/i18n/notify/template_zh_CN.yaml @@ -132,6 +132,7 @@ notifyTypeScope: - "EMAIL" - "WEWORK" + - "VOICE" priority: 1 source: 0 emailTemplate: @@ -146,6 +147,10 @@ sender: "DevOps" title: "蓝盾流水线【${pipelineName}】#${buildNum} 构建成功" body: "✔️${successContent}\n查看详情:${detailUrl}" + voiceTemplate: + id: "jgj46v0knzlkhcah57rcutao30tqza5v" + taskName: "构建成功" + content: "${successContent}" creator: "system" modifior: "system" - index: 7 @@ -331,6 +336,7 @@ notifyTypeScope: - "EMAIL" - "WEWORK" + - "VOICE" priority: 1 source: 0 emailTemplate: @@ -345,6 +351,10 @@ sender: "DevOps" title: "蓝盾流水线【${pipelineName}】#${buildNum} 构建失败" body: "❌${failContent}" + voiceTemplate: + id: "qlzxaotr2zvr8gnj1dkpj2x5lx19wkpt" + taskName: "构建失败" + content: "${failContent}" creator: "system" modifior: "system" - index: 17 @@ -449,6 +459,7 @@ notifyTypeScope: - "EMAIL" - "WEWORK" + - "VOICE" priority: 1 source: 0 emailTemplate: @@ -463,6 +474,10 @@ sender: "DevOps" title: "蓝盾流水线【${pipelineName}】#${buildNum} 构建失败" body: "❌${failContent}\n\n查看详情:${detailUrl}" + voiceTemplate: + id: "x79oetbqquqfjhep7zifheqe9b3tkwvx" + taskName: "构建失败" + content: "${failContent}" creator: "system" modifior: "system" - index: 23 @@ -587,6 +602,7 @@ notifyTypeScope: - "EMAIL" - "WEWORK" + - "VOICE" priority: 1 source: 0 emailTemplate: @@ -601,6 +617,10 @@ sender: "DevOps" title: "蓝盾流水线【${pipelineName}】#${buildNum} 构建成功" body: "✔️${successContent}" + voiceTemplate: + id: "zjeufw8tfnf53tnibu4s43leju8xgnor" + taskName: "构建成功" + content: "${successContent}" creator: "system" modifior: "system" - index: 29 diff --git a/support-files/sql/1001_ci_notify_ddl_mysql.sql b/support-files/sql/1001_ci_notify_ddl_mysql.sql index 54c71ea5a18..b92a1aa3beb 100644 --- a/support-files/sql/1001_ci_notify_ddl_mysql.sql +++ b/support-files/sql/1001_ci_notify_ddl_mysql.sql @@ -176,6 +176,21 @@ CREATE TABLE IF NOT EXISTS `T_COMMON_NOTIFY_MESSAGE_TEMPLATE` ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COMMENT='email模板表'; +CREATE TABLE IF NOT EXISTS `T_VOICE_NOTIFY_MESSAGE_TEMPLATE` +( + `ID` varchar(32) NOT NULL COMMENT '主键ID', + `COMMON_TEMPLATE_ID` varchar(32) NOT NULL COMMENT '模板ID', + `CREATOR` varchar(50) NOT NULL COMMENT '创建者', + `MODIFIOR` varchar(50) NOT NULL COMMENT '修改者', + `TASK_NAME` varchar(256) DEFAULT NULL COMMENT '任务名称', + `CONTENT` text NOT NULL COMMENT '语音内容', + `CREATE_TIME` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', + `UPDATE_TIME` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间', + PRIMARY KEY (`ID`), + KEY `idx_templateId` (`COMMON_TEMPLATE_ID`) +) ENGINE = InnoDB + DEFAULT CHARSET = utf8mb4 COMMENT='语音模板表'; + -- ---------------------------- -- Table structure for T_NOTIFY_WEWORK -- ---------------------------- @@ -223,4 +238,20 @@ CREATE TABLE IF NOT EXISTS `T_WEWORK_GROUP_NOTIFY_MESSAGE_TEMPLATE` ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COMMENT='企业微信群模板表'; +CREATE TABLE IF NOT EXISTS `T_NOTIFY_VOICE` ( + `ID` varchar(32) NOT NULL DEFAULT '' COMMENT '主键ID', + `SUCCESS` bit(1) NOT NULL COMMENT '是否成功', + `RECEIVERS` text NOT NULL COMMENT '语音接收者', + `TASK_NAME` varchar(255) NOT NULL COMMENT '任务名称', + `CONTENT` text NOT NULL COMMENT '呼叫内容', + `TRANSFER_RECEIVER` varchar(50) NOT NULL COMMENT '转接责任人', + `RETRY_COUNT` int(11) NOT NULL COMMENT '重试次数', + `LAST_ERROR` text COMMENT '最后错误内容', + `CREATED_TIME` datetime NOT NULL COMMENT '创建时间', + `UPDATED_TIME` datetime NOT NULL COMMENT '更新时间', + `TOF_SYS_id` varchar(20) DEFAULT NULL COMMENT 'tof系统id', + `FROM_SYS_ID` varchar(20) DEFAULT NULL COMMENT '发送消息的系统id', + PRIMARY KEY (`ID`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='语音流水表'; + SET FOREIGN_KEY_CHECKS = 1; From 570f86122d5fe66b7c0980666ca55263f50ada00 Mon Sep 17 00:00:00 2001 From: stubenhuang Date: Wed, 15 Nov 2023 11:07:14 +0800 Subject: [PATCH 2/5] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E8=AF=AD?= =?UTF-8?q?=E9=9F=B3=E9=80=9A=E7=9F=A5=20#9686?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../notify/service/notifier/BaseNotifier.kt | 23 ------------------- 1 file changed, 23 deletions(-) delete mode 100644 src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/BaseNotifier.kt diff --git a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/BaseNotifier.kt b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/BaseNotifier.kt deleted file mode 100644 index b395b224fe4..00000000000 --- a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/BaseNotifier.kt +++ /dev/null @@ -1,23 +0,0 @@ -package com.tencent.devops.notify.service.notifier - -import com.tencent.devops.common.notify.enums.NotifyType -import com.tencent.devops.model.notify.tables.records.TCommonNotifyMessageTemplateRecord -import com.tencent.devops.notify.pojo.SendNotifyMessageTemplateRequest - -/** - * 通知器 - */ -abstract class BaseNotifier { - /** - * 支持类型 - */ - abstract fun type(): NotifyType - - /** - * 发送 - */ - abstract fun send( - request: SendNotifyMessageTemplateRequest, // 请求 - commonNotifyMessageTemplateRecord: TCommonNotifyMessageTemplateRecord // 模板 - ) -} From 0152f7dfa91914db93beb413b521a7ad3303216d Mon Sep 17 00:00:00 2001 From: stubenhuang Date: Mon, 20 Nov 2023 09:46:56 +0800 Subject: [PATCH 3/5] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E8=AF=AD?= =?UTF-8?q?=E9=9F=B3=E9=80=9A=E7=9F=A5=20#9686?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...{VoiceMessage.kt => VoiceNotifyMessage.kt} | 2 +- .../service/inner/VoiceServiceImpl.kt | 88 +++++++++++++++++++ .../model/VoiceNotifyMessageWithOperation.kt | 4 +- .../devops/notify/service/VoiceService.kt | 4 +- .../notify/service/notifier/VoiceNotifier.kt | 4 +- 5 files changed, 95 insertions(+), 7 deletions(-) rename src/backend/ci/core/notify/api-notify/src/main/kotlin/com/tencent/devops/notify/pojo/{VoiceMessage.kt => VoiceNotifyMessage.kt} (98%) create mode 100644 src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/blueking/service/inner/VoiceServiceImpl.kt diff --git a/src/backend/ci/core/notify/api-notify/src/main/kotlin/com/tencent/devops/notify/pojo/VoiceMessage.kt b/src/backend/ci/core/notify/api-notify/src/main/kotlin/com/tencent/devops/notify/pojo/VoiceNotifyMessage.kt similarity index 98% rename from src/backend/ci/core/notify/api-notify/src/main/kotlin/com/tencent/devops/notify/pojo/VoiceMessage.kt rename to src/backend/ci/core/notify/api-notify/src/main/kotlin/com/tencent/devops/notify/pojo/VoiceNotifyMessage.kt index 61996ec3261..bad5676fb63 100644 --- a/src/backend/ci/core/notify/api-notify/src/main/kotlin/com/tencent/devops/notify/pojo/VoiceMessage.kt +++ b/src/backend/ci/core/notify/api-notify/src/main/kotlin/com/tencent/devops/notify/pojo/VoiceNotifyMessage.kt @@ -5,7 +5,7 @@ import io.swagger.annotations.ApiModel import io.swagger.annotations.ApiModelProperty @ApiModel("语音信息") -open class VoiceMessage : BaseMessage() { +open class VoiceNotifyMessage : BaseMessage() { @ApiModelProperty("接收人(英文ID),支持多个") var receivers = mutableSetOf() diff --git a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/blueking/service/inner/VoiceServiceImpl.kt b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/blueking/service/inner/VoiceServiceImpl.kt new file mode 100644 index 00000000000..c41d2531479 --- /dev/null +++ b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/blueking/service/inner/VoiceServiceImpl.kt @@ -0,0 +1,88 @@ +package com.tencent.devops.notify.blueking.service.inner + +import com.tencent.devops.common.api.util.JsonUtil.deepCopy +import com.tencent.devops.common.api.util.UUIDUtil +import com.tencent.devops.common.notify.utils.Configuration +import com.tencent.devops.notify.EXCHANGE_NOTIFY +import com.tencent.devops.notify.ROUTE_VOICE +import com.tencent.devops.notify.blueking.utils.NotifyService +import com.tencent.devops.notify.blueking.utils.NotifyService.Companion.VOICE_URL +import com.tencent.devops.notify.dao.VoiceNotifyDao +import com.tencent.devops.notify.model.VoiceNotifyMessageWithOperation +import com.tencent.devops.notify.pojo.VoiceNotifyMessage +import com.tencent.devops.notify.service.VoiceService +import org.jooq.DSLContext +import org.slf4j.LoggerFactory +import org.springframework.amqp.rabbit.core.RabbitTemplate +import org.springframework.beans.factory.annotation.Autowired + +class VoiceServiceImpl @Autowired constructor( + private val notifyService: NotifyService, + private val voiceNotifyDao: VoiceNotifyDao, + private val rabbitTemplate: RabbitTemplate, + private val configuration: Configuration, + private val dslContext: DSLContext, +) : VoiceService { + override fun sendMqMsg(message: VoiceNotifyMessage) { + rabbitTemplate.convertAndSend(EXCHANGE_NOTIFY, ROUTE_VOICE, message) + } + + /** + * 发送语音消息 + */ + override fun sendMessage(voiceNotifyMessageWithOperation: VoiceNotifyMessageWithOperation) { + val voiceNotifyPost = try { + voiceNotifyMessageWithOperation.asPost() + } catch (e: Exception) { + logger.warn("send message failed.", e) + return + } + + val tofConfig = configuration.getConfigurations(voiceNotifyMessageWithOperation.tofSysId) + if (null == tofConfig) { + logger.warn("tofConfig is null , $voiceNotifyMessageWithOperation") + return + } + val tofResult = notifyService.post(VOICE_URL, voiceNotifyPost, tofConfig) + val retryCount = voiceNotifyMessageWithOperation.retryCount + val tofSuccess = tofResult.Ret == 0 + val id = voiceNotifyMessageWithOperation.id ?: UUIDUtil.generate() + + voiceNotifyDao.insertOrUpdate( + dslContext = dslContext, + id = id, + success = tofSuccess, + receivers = voiceNotifyPost.receiver, + taskName = voiceNotifyPost.taskName, + content = voiceNotifyPost.content, + transferReceiver = voiceNotifyPost.transferReceiver, + retryCount = retryCount, + lastError = if (tofSuccess) null else tofResult.ErrMsg, + tofSysId = tofConfig["sys-id"], + fromSysId = voiceNotifyPost.fromSysId + ) + + + // 失败重试 , 最多3次 + if (!tofSuccess && retryCount < 3) { + val deepCopyMessage = voiceNotifyMessageWithOperation.deepCopy() + deepCopyMessage.id = id + deepCopyMessage.retryCount = retryCount + 1 + rabbitTemplate.convertAndSend(EXCHANGE_NOTIFY, ROUTE_VOICE, deepCopyMessage) { message -> + var delayTime = 0 + when (retryCount) { + 1 -> delayTime = 30000 + 2 -> delayTime = 120000 + } + if (delayTime > 0) { + message.messageProperties.setHeader("x-delay", delayTime) + } + message + } + } + } + + companion object { + private val logger = LoggerFactory.getLogger(VoiceServiceImpl::class.java) + } +} diff --git a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/model/VoiceNotifyMessageWithOperation.kt b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/model/VoiceNotifyMessageWithOperation.kt index 184842c9b05..0951be23b50 100644 --- a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/model/VoiceNotifyMessageWithOperation.kt +++ b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/model/VoiceNotifyMessageWithOperation.kt @@ -1,8 +1,8 @@ package com.tencent.devops.notify.model -import com.tencent.devops.notify.pojo.VoiceMessage +import com.tencent.devops.notify.pojo.VoiceNotifyMessage -class VoiceNotifyMessageWithOperation : VoiceMessage() { +class VoiceNotifyMessageWithOperation : VoiceNotifyMessage() { var id: String? = null var retryCount: Int = 0 var lastError: String? = null diff --git a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/VoiceService.kt b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/VoiceService.kt index c21b557c27b..2c6d3c0180e 100644 --- a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/VoiceService.kt +++ b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/VoiceService.kt @@ -1,13 +1,13 @@ package com.tencent.devops.notify.service import com.tencent.devops.notify.model.VoiceNotifyMessageWithOperation -import com.tencent.devops.notify.pojo.VoiceMessage +import com.tencent.devops.notify.pojo.VoiceNotifyMessage /** * 语音服务 */ interface VoiceService { - fun sendMqMsg(message: VoiceMessage) + fun sendMqMsg(message: VoiceNotifyMessage) fun sendMessage(voiceNotifyMessageWithOperation: VoiceNotifyMessageWithOperation) } diff --git a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/VoiceNotifier.kt b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/VoiceNotifier.kt index b8caa4f0df0..7ab2a69b80b 100644 --- a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/VoiceNotifier.kt +++ b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/service/notifier/VoiceNotifier.kt @@ -4,7 +4,7 @@ import com.tencent.devops.common.notify.enums.NotifyType import com.tencent.devops.model.notify.tables.records.TCommonNotifyMessageTemplateRecord import com.tencent.devops.notify.dao.NotifyMessageTemplateDao import com.tencent.devops.notify.pojo.SendNotifyMessageTemplateRequest -import com.tencent.devops.notify.pojo.VoiceMessage +import com.tencent.devops.notify.pojo.VoiceNotifyMessage import com.tencent.devops.notify.service.VoiceService import org.jooq.DSLContext import org.slf4j.LoggerFactory @@ -31,7 +31,7 @@ class VoiceNotifier @Autowired constructor( val content = NotifierUtils.replaceContentParams(request.bodyParams, voiceTplRecord.content) logger.info("send voice msg , ${commonNotifyMessageTemplateRecord.id} , taskName:$taskName , content:$content") - val message = VoiceMessage() + val message = VoiceNotifyMessage() message.receivers = request.receivers message.taskName = taskName message.content = content From c2747b1679ee1692e50c86e3bfa7f147b8e564c5 Mon Sep 17 00:00:00 2001 From: stubenhuang Date: Mon, 20 Nov 2023 10:14:27 +0800 Subject: [PATCH 4/5] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E8=AF=AD?= =?UTF-8?q?=E9=9F=B3=E9=80=9A=E7=9F=A5=20#9686?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../devops/notify/blueking/service/inner/VoiceServiceImpl.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/blueking/service/inner/VoiceServiceImpl.kt b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/blueking/service/inner/VoiceServiceImpl.kt index c41d2531479..ac71ed70856 100644 --- a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/blueking/service/inner/VoiceServiceImpl.kt +++ b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/blueking/service/inner/VoiceServiceImpl.kt @@ -21,7 +21,7 @@ class VoiceServiceImpl @Autowired constructor( private val voiceNotifyDao: VoiceNotifyDao, private val rabbitTemplate: RabbitTemplate, private val configuration: Configuration, - private val dslContext: DSLContext, + private val dslContext: DSLContext ) : VoiceService { override fun sendMqMsg(message: VoiceNotifyMessage) { rabbitTemplate.convertAndSend(EXCHANGE_NOTIFY, ROUTE_VOICE, message) From 084898363474e3bd5ba7f0073d69257bc1c5cf5d Mon Sep 17 00:00:00 2001 From: stubenhuang Date: Mon, 20 Nov 2023 10:19:01 +0800 Subject: [PATCH 5/5] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E8=AF=AD?= =?UTF-8?q?=E9=9F=B3=E9=80=9A=E7=9F=A5=20#9686?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../devops/notify/blueking/service/inner/VoiceServiceImpl.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/blueking/service/inner/VoiceServiceImpl.kt b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/blueking/service/inner/VoiceServiceImpl.kt index ac71ed70856..d193e354339 100644 --- a/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/blueking/service/inner/VoiceServiceImpl.kt +++ b/src/backend/ci/core/notify/biz-notify/src/main/kotlin/com/tencent/devops/notify/blueking/service/inner/VoiceServiceImpl.kt @@ -62,7 +62,6 @@ class VoiceServiceImpl @Autowired constructor( fromSysId = voiceNotifyPost.fromSysId ) - // 失败重试 , 最多3次 if (!tofSuccess && retryCount < 3) { val deepCopyMessage = voiceNotifyMessageWithOperation.deepCopy()