-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main' into dev
- Loading branch information
Showing
67 changed files
with
4,846 additions
and
564 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Ding talk Notification | ||
|
||
## Feature | ||
|
||
- Send message to Ding talk | ||
|
||
## Config | ||
|
||
> Config Webhook URL and open the notification | ||
- Webhook URL: such as `https://oapi.dingtalk.com/robot/send?access_token=xxxxxx` | ||
|
||
## Preview | ||
|
||
![Ding talk Config](./docs/dingtalk-config.png) | ||
|
||
## Document | ||
|
||
- https://open.dingtalk.com/document/robots/custom-robot-access | ||
- https://open.dingtalk.com/document/orgapp/custom-bot-send-message-type |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* 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 dingtalk | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/apache/incubator-answer-plugins/notification-dingtalk/i18n" | ||
"github.com/apache/incubator-answer/plugin" | ||
) | ||
|
||
type NotificationConfig struct { | ||
Notification bool `json:"notification"` | ||
} | ||
|
||
func (n *Notification) ConfigFields() []plugin.ConfigField { | ||
return []plugin.ConfigField{ | ||
{ | ||
Name: "notification", | ||
Type: plugin.ConfigTypeSwitch, | ||
Title: plugin.MakeTranslator(i18n.ConfigNotificationTitle), | ||
Description: plugin.MakeTranslator(i18n.ConfigNotificationDescription), | ||
UIOptions: plugin.ConfigFieldUIOptions{ | ||
Label: plugin.MakeTranslator(i18n.ConfigNotificationLabel), | ||
}, | ||
Value: n.Config.Notification, | ||
}, | ||
} | ||
} | ||
|
||
func (n *Notification) ConfigReceiver(config []byte) error { | ||
c := &NotificationConfig{} | ||
_ = json.Unmarshal(config, c) | ||
n.Config = c | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
/* | ||
* 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 dingtalk | ||
|
||
import ( | ||
"embed" | ||
"github.com/apache/incubator-answer-plugins/util" | ||
"github.com/go-resty/resty/v2" | ||
"strings" | ||
|
||
dingtalkI18n "github.com/apache/incubator-answer-plugins/notification-dingtalk/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(dingtalkI18n.InfoName), | ||
SlugName: info.SlugName, | ||
Description: plugin.MakeTranslator(dingtalkI18n.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, notificationTitle := renderNotification(msg) | ||
// no need to send empty message | ||
if len(notificationMsg) == 0 { | ||
log.Debugf("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, notificationTitle)). | ||
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, string) { | ||
lang := i18n.Language(msg.ReceiverLang) | ||
switch msg.Type { | ||
case plugin.NotificationUpdateQuestion: | ||
return plugin.TranslateWithData(lang, dingtalkI18n.TplUpdateQuestion, msg), plugin.TranslateWithData(lang, dingtalkI18n.TplUpdateQuestionTitle, nil) | ||
case plugin.NotificationAnswerTheQuestion: | ||
return plugin.TranslateWithData(lang, dingtalkI18n.TplAnswerTheQuestion, msg), plugin.TranslateWithData(lang, dingtalkI18n.TplAnswerTheQuestionTitle, nil) | ||
case plugin.NotificationUpdateAnswer: | ||
return plugin.TranslateWithData(lang, dingtalkI18n.TplUpdateAnswer, msg), plugin.TranslateWithData(lang, dingtalkI18n.TplUpdateAnswerTitle, nil) | ||
case plugin.NotificationAcceptAnswer: | ||
return plugin.TranslateWithData(lang, dingtalkI18n.TplAcceptAnswer, msg), plugin.TranslateWithData(lang, dingtalkI18n.TplAcceptAnswerTitle, nil) | ||
case plugin.NotificationCommentQuestion: | ||
return plugin.TranslateWithData(lang, dingtalkI18n.TplCommentQuestion, msg), plugin.TranslateWithData(lang, dingtalkI18n.TplCommentQuestionTitle, nil) | ||
case plugin.NotificationCommentAnswer: | ||
return plugin.TranslateWithData(lang, dingtalkI18n.TplCommentAnswer, msg), plugin.TranslateWithData(lang, dingtalkI18n.TplCommentAnswerTitle, nil) | ||
case plugin.NotificationReplyToYou: | ||
return plugin.TranslateWithData(lang, dingtalkI18n.TplReplyToYou, msg), plugin.TranslateWithData(lang, dingtalkI18n.TplReplyToYouTitle, nil) | ||
case plugin.NotificationMentionYou: | ||
return plugin.TranslateWithData(lang, dingtalkI18n.TplMentionYou, msg), plugin.TranslateWithData(lang, dingtalkI18n.TplMentionYouTitle, nil) | ||
case plugin.NotificationInvitedYouToAnswer: | ||
return plugin.TranslateWithData(lang, dingtalkI18n.TplInvitedYouToAnswer, msg), plugin.TranslateWithData(lang, dingtalkI18n.TplInvitedYouToAnswerTitle, nil) | ||
case plugin.NotificationNewQuestion, plugin.NotificationNewQuestionFollowedTag: | ||
msg.QuestionTags = strings.Join(strings.Split(msg.QuestionTags, ","), ", ") | ||
return plugin.TranslateWithData(lang, dingtalkI18n.TplNewQuestion, msg), plugin.TranslateWithData(lang, dingtalkI18n.TplNewQuestionTitle, nil) | ||
} | ||
return "", "" | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
module github.com/apache/incubator-answer-plugins/dingtalk | ||
|
||
go 1.21.3 | ||
|
||
require ( | ||
github.com/apache/incubator-answer v1.4.0 | ||
github.com/apache/incubator-answer-plugins/util v1.0.2 | ||
github.com/go-resty/resty/v2 v2.15.3 | ||
github.com/segmentfault/pacman v1.0.5-0.20230822083413-c0075a2d401f | ||
) | ||
|
||
require ( | ||
github.com/LinkinStars/go-i18n/v2 v2.2.2 // indirect | ||
github.com/aymerick/douceur v0.2.0 // indirect | ||
github.com/bytedance/sonic v1.9.1 // indirect | ||
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect | ||
github.com/gabriel-vasile/mimetype v1.4.2 // indirect | ||
github.com/gin-contrib/sse v0.1.0 // indirect | ||
github.com/gin-gonic/gin v1.9.1 // indirect | ||
github.com/go-playground/locales v0.14.1 // indirect | ||
github.com/go-playground/universal-translator v0.18.1 // indirect | ||
github.com/go-playground/validator/v10 v10.14.0 // indirect | ||
github.com/goccy/go-json v0.10.2 // indirect | ||
github.com/google/wire v0.5.0 // indirect | ||
github.com/gorilla/css v1.0.0 // indirect | ||
github.com/json-iterator/go v1.1.12 // indirect | ||
github.com/klauspost/cpuid/v2 v2.2.4 // indirect | ||
github.com/kr/text v0.2.0 // indirect | ||
github.com/leodido/go-urn v1.2.4 // indirect | ||
github.com/mattn/go-isatty v0.0.19 // indirect | ||
github.com/microcosm-cc/bluemonday v1.0.21 // indirect | ||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | ||
github.com/modern-go/reflect2 v1.0.2 // indirect | ||
github.com/pelletier/go-toml/v2 v2.0.8 // indirect | ||
github.com/segmentfault/pacman/contrib/i18n v0.0.0-20230516093754-b76aef1c1150 // indirect | ||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect | ||
github.com/ugorji/go/codec v1.2.11 // indirect | ||
golang.org/x/arch v0.3.0 // indirect | ||
golang.org/x/crypto v0.25.0 // indirect | ||
golang.org/x/net v0.27.0 // indirect | ||
golang.org/x/sys v0.22.0 // indirect | ||
golang.org/x/text v0.16.0 // indirect | ||
google.golang.org/protobuf v1.30.0 // indirect | ||
gopkg.in/yaml.v2 v2.4.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
sigs.k8s.io/yaml v1.3.0 // indirect | ||
) |
Oops, something went wrong.