Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support Ding talk Notification #231

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ The Connector plugin helps us to implement third-party login functionality. For

The Storage plugin helps us to upload files to third-party storage. For example: Aliyun OSS or AWS S3.

- [x] [Aliyun](https://github.com/apache/incubator-answer-plugins/tree/main/storage-aliyunoss)
- [x] [Aliyun OSS](https://github.com/apache/incubator-answer-plugins/tree/main/storage-aliyunoss)
- [x] [Tencentyun COS](https://github.com/apache/incubator-answer-plugins/tree/main/storage-tencentyuncos)
- [x] [S3](https://github.com/apache/incubator-answer-plugins/tree/main/storage-s3)

### Cache
Expand Down Expand Up @@ -53,6 +54,8 @@ Using the third-party user system to manage users. For example: WeCom
The Notification plugin helps us to send messages to third-party notification systems. For example: Slack.

- [x] [Slack](https://github.com/apache/incubator-answer-plugins/tree/main/notification-slack)
- [x] [Lark](https://github.com/apache/incubator-answer-plugins/tree/main/notification-lark)
- [x] [Ding talk](https://github.com/apache/incubator-answer-plugins/tree/main/notification-dingtalk)

### Route

Expand Down
20 changes: 20 additions & 0 deletions notification-dingtalk/README.md
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
53 changes: 53 additions & 0 deletions notification-dingtalk/config.go
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
}
166 changes: 166 additions & 0 deletions notification-dingtalk/dingtalk_notification.go
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 "", ""
}
Binary file added notification-dingtalk/docs/dingtalk-config.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 47 additions & 0 deletions notification-dingtalk/go.mod
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
)
Loading