-
Notifications
You must be signed in to change notification settings - Fork 0
/
dingtalk_handler.go
84 lines (73 loc) · 1.8 KB
/
dingtalk_handler.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
package delayed_job
import (
"errors"
"time"
"github.com/CodyGuo/dingtalk"
"github.com/CodyGuo/dingtalk/pkg/robot"
)
func newDingHandler(ctx, params map[string]interface{}) (Handler, error) {
if nil == params {
return nil, errors.New("params is nil")
}
webhook := stringWithDefault(params, "webhook", "")
if webhook == "" {
webhook = stringWithDefault(params, "web_hook", "")
}
secret := stringWithDefault(params, "secret", "")
content := stringWithDefault(params, "content", "")
if content == "" {
return nil, errors.New("content is missing")
}
var e error
if args, ok := params["arguments"]; ok {
args = preprocessArgs(args)
if props, ok := args.(map[string]interface{}); ok {
if _, ok := props["self"]; !ok {
props["self"] = params
defer delete(props, "self")
}
}
content, e = genText(content, args)
if nil != e {
return nil, e
}
}
targets := stringsWithDefault(params, "targets", ",", nil)
if len(targets) == 0 {
targets = stringsWithDefault(params, "userList", ",", nil)
}
return &dingHandler{
webhook: webhook,
secret: secret,
content: content,
targets: targets,
}, nil
}
type dingHandler struct {
webhook string
secret string
content string
targets []string
}
func (self *dingHandler) Perform() error {
client := dingtalk.New(self.webhook,
dingtalk.WithSecret(self.secret),
dingtalk.WithTimeout(30*time.Second))
// defer client.Close()
var opts = []robot.SendOption{}
if len(self.targets) > 0 {
opts = append(opts, robot.SendWithAtMobiles(self.targets))
}
err := client.RobotSendText(self.content, opts...)
if err != nil {
if e, ok := err.(*dingtalk.Error); ok {
return e.Unwrap()
}
}
return err
}
func init() {
Handlers["ding"] = newDingHandler
Handlers["ding_action"] = newDingHandler
Handlers["ding_command"] = newDingHandler
}