-
Notifications
You must be signed in to change notification settings - Fork 152
/
client.go
186 lines (142 loc) · 5.74 KB
/
client.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package customerService
import (
"errors"
"github.com/ArtisanCloud/PowerLibs/v2/object"
"github.com/ArtisanCloud/PowerWeChat/v2/src/kernel"
"github.com/ArtisanCloud/PowerWeChat/v2/src/kernel/contract"
response2 "github.com/ArtisanCloud/PowerWeChat/v2/src/kernel/response"
"github.com/ArtisanCloud/PowerWeChat/v2/src/officialAccount/customerService/request"
"github.com/ArtisanCloud/PowerWeChat/v2/src/officialAccount/customerService/response"
)
type Client struct {
*kernel.BaseClient
}
func NewClient(app kernel.ApplicationInterface) (*Client, error) {
baseClient, err := kernel.NewBaseClient(&app, nil)
if err != nil {
return nil, err
}
return &Client{
baseClient,
}, nil
}
// 获取客服基本信息
// https://developers.weixin.qq.com/doc/offiaccount/Customer_Service/Customer_Service_Management.html
func (comp *Client) List() (*response.ResponseList, error) {
result := &response.ResponseList{}
_, err := comp.HttpGet("cgi-bin/customservice/getkflist", nil, nil, &result)
return result, err
}
// 获取在线客服基本信息
// https://developers.weixin.qq.com/doc/offiaccount/Customer_Service/Customer_Service_Management.html
func (comp *Client) Online() (*response.ResponseKFOnlineList, error) {
result := &response.ResponseKFOnlineList{}
_, err := comp.HttpGet("cgi-bin/customservice/getonlinekflist", nil, nil, &result)
return result, err
}
// 添加客服帐号
// https://developers.weixin.qq.com/doc/offiaccount/Customer_Service/Customer_Service_Management.html
func (comp *Client) Create(account string, nickname string) (*response2.ResponseOfficialAccount, error) {
result := &response2.ResponseOfficialAccount{}
params := &object.HashMap{
"kf_account": account,
"nickname": nickname,
}
_, err := comp.HttpPostJson("customservice/kfaccount/add", params, nil, nil, &result)
return result, err
}
// 邀请绑定客服帐号
// https://developers.weixin.qq.com/doc/offiaccount/Customer_Service/Customer_Service_Management.html
func (comp *Client) Update(account string, nickname string) (*response2.ResponseOfficialAccount, error) {
result := &response2.ResponseOfficialAccount{}
params := &object.HashMap{
"kf_account": account,
"nickname": nickname,
}
_, err := comp.HttpPostJson("customservice/kfaccount/update", params, nil, nil, &result)
return result, err
}
// 删除客服帐号
// https://developers.weixin.qq.com/doc/offiaccount/Customer_Service/Customer_Service_Management.html
func (comp *Client) Delete(account string) (*response2.ResponseOfficialAccount, error) {
result := &response2.ResponseOfficialAccount{}
query := &object.StringMap{
"kf_account": account,
}
_, err := comp.HttpGet("customservice/kfaccount/delete", query, nil, &result)
return result, err
}
// 邀请绑定客服帐号
// https://developers.weixin.qq.com/doc/offiaccount/Customer_Service/Customer_Service_Management.html
func (comp *Client) Invite(account string, wechatID string) (*response2.ResponseOfficialAccount, error) {
result := &response2.ResponseOfficialAccount{}
params := &object.HashMap{
"kf_account": account,
"invite_wx": wechatID,
}
_, err := comp.HttpPostJson("customservice/kfaccount/inviteworker", params, nil, nil, &result)
return result, err
}
// 上传客服头像
// https://developers.weixin.qq.com/doc/offiaccount/Customer_Service/Customer_Service_Management.html
func (comp *Client) SetAvatar(account string, path string) (*response2.ResponseOfficialAccount, error) {
result := &response2.ResponseOfficialAccount{}
var files *object.HashMap
if path != "" {
files = &object.HashMap{
"media": path,
}
} else {
return nil, errors.New("path is empty")
}
params := &object.StringMap{
"kf_account": account,
}
_, err := comp.HttpUpload("customservice/kfaccount/uploadheadimg", files, nil, params, nil, &result)
return result, err
}
func (comp *Client) Message(message contract.MessageInterface) *Messenger {
messageBuilder := NewMessenger(comp)
return messageBuilder.SetMessage(&message)
}
// 客服接口 - 发消息
// https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Service_Center_messages.html
func (comp *Client) Send(message interface{}) (*response2.ResponseOfficialAccount, error) {
result := &response2.ResponseOfficialAccount{}
_, err := comp.HttpPostJson("cgi-bin/message/custom/send", message, nil, nil, result)
return result, err
}
// 显示收入状态给用户
// https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Service_Center_messages.html
func (comp *Client) ShowTypingStatusToUser(openID string) (*response2.ResponseOfficialAccount, error) {
result := &response2.ResponseOfficialAccount{}
params := &object.HashMap{
"touser": openID,
"command": "Typing",
}
_, err := comp.HttpPostJson("cgi-bin/message/custom/typing", params, nil, nil, &result)
return result, err
}
// 隐藏收入状态给用户
// https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Service_Center_messages.html
func (comp *Client) HideTypingStatusToUser(openID string) (*response2.ResponseOfficialAccount, error) {
result := &response2.ResponseOfficialAccount{}
params := &object.HashMap{
"touser": openID,
"command": "CancelTyping",
}
_, err := comp.HttpPostJson("cgi-bin/message/custom/typing", params, nil, nil, &result)
return result, err
}
// 获取聊天记录
// https://developers.weixin.qq.com/doc/offiaccount/Customer_Service/Obtain_chat_transcript.html
func (comp *Client) Messages(data *request.RequestMessages) (*response.ResponseMessages, error) {
result := &response.ResponseMessages{}
//params, err := object.StructToHashMapWithTag(data, "json")
params, err := object.StructToHashMap(data)
if err != nil {
return nil, err
}
_, err = comp.HttpPostJson("customservice/msgrecord/getmsglist", params, nil, nil, &result)
return result, err
}