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