|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package dingtalk |
| 21 | + |
| 22 | +import ( |
| 23 | + "embed" |
| 24 | + "github.com/apache/incubator-answer-plugins/util" |
| 25 | + "github.com/go-resty/resty/v2" |
| 26 | + "strings" |
| 27 | + |
| 28 | + dingtalkI18n "github.com/apache/incubator-answer-plugins/notification-dingtalk/i18n" |
| 29 | + "github.com/apache/incubator-answer/plugin" |
| 30 | + "github.com/segmentfault/pacman/i18n" |
| 31 | + "github.com/segmentfault/pacman/log" |
| 32 | +) |
| 33 | + |
| 34 | +//go:embed info.yaml |
| 35 | +var Info embed.FS |
| 36 | + |
| 37 | +type Notification struct { |
| 38 | + Config *NotificationConfig |
| 39 | + UserConfigCache *UserConfigCache |
| 40 | +} |
| 41 | + |
| 42 | +func init() { |
| 43 | + uc := &Notification{ |
| 44 | + Config: &NotificationConfig{}, |
| 45 | + UserConfigCache: NewUserConfigCache(), |
| 46 | + } |
| 47 | + plugin.Register(uc) |
| 48 | +} |
| 49 | + |
| 50 | +func (n *Notification) Info() plugin.Info { |
| 51 | + info := &util.Info{} |
| 52 | + info.GetInfo(Info) |
| 53 | + |
| 54 | + return plugin.Info{ |
| 55 | + Name: plugin.MakeTranslator(dingtalkI18n.InfoName), |
| 56 | + SlugName: info.SlugName, |
| 57 | + Description: plugin.MakeTranslator(dingtalkI18n.InfoDescription), |
| 58 | + Author: info.Author, |
| 59 | + Version: info.Version, |
| 60 | + Link: info.Link, |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +// GetNewQuestionSubscribers returns the subscribers of the new question notification |
| 65 | +func (n *Notification) GetNewQuestionSubscribers() (userIDs []string) { |
| 66 | + for userID, conf := range n.UserConfigCache.userConfigMapping { |
| 67 | + if conf.AllNewQuestions { |
| 68 | + userIDs = append(userIDs, userID) |
| 69 | + } |
| 70 | + } |
| 71 | + return userIDs |
| 72 | +} |
| 73 | + |
| 74 | +// Notify sends a notification to the user |
| 75 | +func (n *Notification) Notify(msg plugin.NotificationMessage) { |
| 76 | + log.Debugf("try to send notification %+v", msg) |
| 77 | + |
| 78 | + if !n.Config.Notification { |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + // get user config |
| 83 | + userConfig, err := n.getUserConfig(msg.ReceiverUserID) |
| 84 | + if err != nil { |
| 85 | + log.Errorf("get user config failed: %v", err) |
| 86 | + return |
| 87 | + } |
| 88 | + if userConfig == nil { |
| 89 | + log.Debugf("user %s has no config", msg.ReceiverUserID) |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + // check if the notification is enabled |
| 94 | + switch msg.Type { |
| 95 | + case plugin.NotificationNewQuestion: |
| 96 | + if !userConfig.AllNewQuestions { |
| 97 | + log.Debugf("user %s not config the new question", msg.ReceiverUserID) |
| 98 | + return |
| 99 | + } |
| 100 | + case plugin.NotificationNewQuestionFollowedTag: |
| 101 | + if !userConfig.NewQuestionsForFollowingTags { |
| 102 | + log.Debugf("user %s not config the new question followed tag", msg.ReceiverUserID) |
| 103 | + return |
| 104 | + } |
| 105 | + default: |
| 106 | + if !userConfig.InboxNotifications { |
| 107 | + log.Debugf("user %s not config the inbox notification", msg.ReceiverUserID) |
| 108 | + return |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + log.Debugf("user %s config the notification", msg.ReceiverUserID) |
| 113 | + |
| 114 | + if len(userConfig.WebhookURL) == 0 { |
| 115 | + log.Errorf("user %s has no webhook url", msg.ReceiverUserID) |
| 116 | + return |
| 117 | + } |
| 118 | + |
| 119 | + notificationMsg, notificationTitle := renderNotification(msg) |
| 120 | + // no need to send empty message |
| 121 | + if len(notificationMsg) == 0 { |
| 122 | + log.Debugf("this type of notification will be drop, the type is %s", msg.Type) |
| 123 | + return |
| 124 | + } |
| 125 | + |
| 126 | + // Create a Resty Client |
| 127 | + client := resty.New() |
| 128 | + resp, err := client.R(). |
| 129 | + SetHeader("Content-Type", "application/json"). |
| 130 | + SetBody(NewWebhookReq(notificationMsg, notificationTitle)). |
| 131 | + Post(userConfig.WebhookURL) |
| 132 | + |
| 133 | + if err != nil { |
| 134 | + log.Errorf("send message failed: %v %v", err, resp) |
| 135 | + } else { |
| 136 | + log.Infof("send message to %s success, resp: %s", msg.ReceiverUserID, resp.String()) |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +func renderNotification(msg plugin.NotificationMessage) (string, string) { |
| 141 | + lang := i18n.Language(msg.ReceiverLang) |
| 142 | + switch msg.Type { |
| 143 | + case plugin.NotificationUpdateQuestion: |
| 144 | + return plugin.TranslateWithData(lang, dingtalkI18n.TplUpdateQuestion, msg), plugin.TranslateWithData(lang, dingtalkI18n.TplUpdateQuestionTitle, nil) |
| 145 | + case plugin.NotificationAnswerTheQuestion: |
| 146 | + return plugin.TranslateWithData(lang, dingtalkI18n.TplAnswerTheQuestion, msg), plugin.TranslateWithData(lang, dingtalkI18n.TplAnswerTheQuestionTitle, nil) |
| 147 | + case plugin.NotificationUpdateAnswer: |
| 148 | + return plugin.TranslateWithData(lang, dingtalkI18n.TplUpdateAnswer, msg), plugin.TranslateWithData(lang, dingtalkI18n.TplUpdateAnswerTitle, nil) |
| 149 | + case plugin.NotificationAcceptAnswer: |
| 150 | + return plugin.TranslateWithData(lang, dingtalkI18n.TplAcceptAnswer, msg), plugin.TranslateWithData(lang, dingtalkI18n.TplAcceptAnswerTitle, nil) |
| 151 | + case plugin.NotificationCommentQuestion: |
| 152 | + return plugin.TranslateWithData(lang, dingtalkI18n.TplCommentQuestion, msg), plugin.TranslateWithData(lang, dingtalkI18n.TplCommentQuestionTitle, nil) |
| 153 | + case plugin.NotificationCommentAnswer: |
| 154 | + return plugin.TranslateWithData(lang, dingtalkI18n.TplCommentAnswer, msg), plugin.TranslateWithData(lang, dingtalkI18n.TplCommentAnswerTitle, nil) |
| 155 | + case plugin.NotificationReplyToYou: |
| 156 | + return plugin.TranslateWithData(lang, dingtalkI18n.TplReplyToYou, msg), plugin.TranslateWithData(lang, dingtalkI18n.TplReplyToYouTitle, nil) |
| 157 | + case plugin.NotificationMentionYou: |
| 158 | + return plugin.TranslateWithData(lang, dingtalkI18n.TplMentionYou, msg), plugin.TranslateWithData(lang, dingtalkI18n.TplMentionYouTitle, nil) |
| 159 | + case plugin.NotificationInvitedYouToAnswer: |
| 160 | + return plugin.TranslateWithData(lang, dingtalkI18n.TplInvitedYouToAnswer, msg), plugin.TranslateWithData(lang, dingtalkI18n.TplInvitedYouToAnswerTitle, nil) |
| 161 | + case plugin.NotificationNewQuestion, plugin.NotificationNewQuestionFollowedTag: |
| 162 | + msg.QuestionTags = strings.Join(strings.Split(msg.QuestionTags, ","), ", ") |
| 163 | + return plugin.TranslateWithData(lang, dingtalkI18n.TplNewQuestion, msg), plugin.TranslateWithData(lang, dingtalkI18n.TplNewQuestionTitle, nil) |
| 164 | + } |
| 165 | + return "", "" |
| 166 | +} |
0 commit comments