-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathappchat.go
75 lines (62 loc) · 1.64 KB
/
appchat.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
package wechat
//https://work.weixin.qq.com/api/doc#10167
//https://work.weixin.qq.com/api/doc#13288
type AppChatMsg struct {
ChatId string `json:"chatid"`
MsgType MessageType `json:"msgtype"`
Safe int `json:"safe"`
Text *MsgText `json:"text, omitempty"`
Image *MsgImage `json:"image, omitempty"`
}
type AppChatRet struct {
ErrCode int `json:"errcode"`
ErrMsg string `json:"errmsg"`
ChatId string `json:"chatid"`
}
func (w *WechatWork) AppChatText(chatId, msgTxt string) (ret AppChatRet, err error) {
msg := AppChatMsg{
ChatId: chatId,
MsgType: MsgTypeText,
Safe: 0,
Text: &MsgText{msgTxt},
}
return w.AppChat(msg)
}
func (w *WechatWork) AppChat(msg AppChatMsg) (ret AppChatRet, err error) {
url1, err := w.appchatSendAddr()
if err != nil {
return
}
err = w.Client.CallWithJson(nil, &ret, "POST", url1, msg)
return
}
func (w *WechatWork) appchatSendAddr() (addr string, err error) {
token, err := w.GetAccessToken()
if err != nil {
return
}
addr = ApiHost + "/cgi-bin/appchat/send?access_token=" + token.AccessToken
return
}
func (w *WechatWork) NewChat(name, owner, chatId string, users []string) (ret AppChatRet, err error) {
param := map[string]interface{}{
"name": name,
"owner": owner,
"chatid": chatId,
"userlist": users,
}
url1, err := w.appchatCreateAddr()
if err != nil {
return
}
err = w.Client.CallWithJson(nil, &ret, "POST", url1, param)
return
}
func (w *WechatWork) appchatCreateAddr() (addr string, err error) {
token, err := w.GetAccessToken()
if err != nil {
return
}
addr = ApiHost + "/cgi-bin/appchat/create?access_token=" + token.AccessToken
return
}