-
Notifications
You must be signed in to change notification settings - Fork 0
/
weixin_handler.go
142 lines (121 loc) · 3.65 KB
/
weixin_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
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
package delayed_job
import (
"errors"
"fmt"
"strings"
"sync"
"gopkg.in/chanxuehong/wechat.v1/corp"
"gopkg.in/chanxuehong/wechat.v1/corp/message/send"
)
var weixin_lock sync.Mutex
var weixin_clients = map[string]*WeixinClient{}
type WeixinClient struct {
client *send.Client
mu sync.Mutex
}
func GetWeixinClient(corp_id, corp_secret string) *WeixinClient {
weixin_lock.Lock()
defer weixin_lock.Unlock()
cl, ok := weixin_clients[corp_id+"-"+corp_secret]
if ok {
return cl
}
cl = &WeixinClient{}
var accessTokenServer = corp.NewDefaultAccessTokenServer(corp_id, corp_secret, nil)
cl.client = send.NewClient(accessTokenServer, nil)
weixin_clients[corp_id+"-"+corp_secret] = cl
return cl
}
type weixinHandler struct {
corp_server_url string
corp_id string
corp_secret string
msg send.Text
}
func newWeixinHandler(ctx, params map[string]interface{}) (Handler, error) {
if nil == params {
return nil, errors.New("params is nil")
}
corp_server_url := stringWithDefault(params, "corp_server_url", "")
corp_id := stringWithDefault(params, "corp_id", "")
corp_secret := stringWithDefault(params, "corp_secret", "")
target_type := stringWithDefault(params, "target_type", "")
var msg send.Text
msg.MsgType = send.MsgTypeText
msg.AgentId = int64(intWithDefault(params, "agent_id", -1))
if -1 == msg.AgentId {
return nil, errors.New("agent_id is missing")
}
msg.Text.Content = stringWithDefault(params, "content", "")
if "" == msg.Text.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")
}
}
msg.Text.Content, e = genText(msg.Text.Content, args)
if nil != e {
return nil, e
}
}
switch strings.ToLower(target_type) {
case "department", "departments", "departmentList", "departmentlist", "party":
targets := stringOrArrayWithDefault(params, []string{"targets", "departmentList"}, "")
if "" == targets {
return nil, errors.New("department targets is empty")
}
msg.ToParty = strings.Replace(targets, ",", "|", -1)
case "tag", "tags", "tagList", "taglist":
targets := stringOrArrayWithDefault(params, []string{"targets", "tagList"}, "")
if "" == targets {
return nil, errors.New("tag targets is empty")
}
msg.ToTag = strings.Replace(targets, ",", "|", -1)
default:
targets := stringOrArrayWithDefault(params, []string{"targets", "userList"}, "")
if "" == targets {
return nil, errors.New(target_type + " targets is empty")
}
msg.ToUser = strings.Replace(targets, ",", "|", -1)
}
return &weixinHandler{
corp_server_url: corp_server_url,
corp_id: corp_id,
corp_secret: corp_secret,
msg: msg}, nil
}
func (self *weixinHandler) Perform() error {
if self.corp_server_url != "" {
old := corp.QyApiURL
corp.QyApiURL = self.corp_server_url
defer func() {
corp.QyApiURL = old
}()
}
ul := GetWeixinClient(self.corp_id, Decrypt(self.corp_secret))
ul.mu.Lock()
defer ul.mu.Unlock()
if r, err := ul.client.SendText(&self.msg); nil != err {
return err
} else if "" != r.InvalidUser {
return errors.New("invalid user - " + r.InvalidUser)
} else if "" != r.InvalidParty {
return errors.New("invalid party - " + r.InvalidParty)
} else if "" != r.InvalidTag {
return errors.New("invalid tag - " + r.InvalidUser)
} else {
fmt.Println(fmt.Sprintf("%#v", r))
}
return nil
}
func init() {
Handlers["weixin"] = newWeixinHandler
Handlers["weixin_action"] = newWeixinHandler
Handlers["weixin_command"] = newWeixinHandler
}