-
Notifications
You must be signed in to change notification settings - Fork 159
/
client.go
117 lines (98 loc) · 3.4 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
package groupRobot
import (
"context"
"errors"
"github.com/ArtisanCloud/PowerLibs/v3/object"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel"
"github.com/ArtisanCloud/PowerWeChat/v3/src/kernel/power"
"github.com/ArtisanCloud/PowerWeChat/v3/src/work/groupRobot/request"
"github.com/ArtisanCloud/PowerWeChat/v3/src/work/groupRobot/response"
)
type Client struct {
BaseClient *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
}
func (comp *Client) Message(message interface{}) (*Messager, error) {
return NewMessager(comp).Message(message)
}
// 群机器人配置说明
// https://developer.work.weixin.qq.com/document/path/91770#文本类型
func (comp *Client) UploadMedia(ctx context.Context, key string, path string) (*response.ResponseGroupRobotUploadMedia, error) {
result := &response.ResponseGroupRobotUploadMedia{}
comp.BaseClient.Token = nil
var files *object.HashMap
if path != "" {
files = &object.HashMap{
"media": path,
}
} else {
return nil, errors.New("path is empty")
}
_, err := comp.BaseClient.HttpUpload(ctx, "cgi-bin/webhook/upload_media", files, nil, &object.StringMap{
"key": key,
"type": "file",
}, nil, result)
return result, err
}
// https://developer.work.weixin.qq.com/document/path/91770#文本类型
func (comp *Client) Send(ctx context.Context, key string, message *power.HashMap) (*response.ResponseGroupRobotSend, error) {
result := &response.ResponseGroupRobotSend{}
comp.BaseClient.Token = nil
_, err := comp.BaseClient.HttpPostJson(ctx, "cgi-bin/webhook/send", message.ToHashMap(), &object.StringMap{
"key": key,
}, nil, result)
return result, err
}
func (comp *Client) SendText(ctx context.Context, key string, message *request.GroupRobotMsgText) (*response.ResponseGroupRobotSend, error) {
options := &power.HashMap{
"msgtype": "text",
"text": message,
}
return comp.Send(ctx, key, options)
}
func (comp *Client) SendMarkdown(ctx context.Context, key string, message *request.GroupRobotMsgMarkdown) (*response.ResponseGroupRobotSend, error) {
options := &power.HashMap{
"msgtype": "markdown",
"markdown": message,
}
return comp.Send(ctx, key, options)
}
func (comp *Client) SendImage(ctx context.Context, key string, message *request.GroupRobotMsgImage) (*response.ResponseGroupRobotSend, error) {
options := &power.HashMap{
"msgtype": "image",
"image": message,
}
return comp.Send(ctx, key, options)
}
// SendNewsArticles 图文类型
func (comp *Client) SendNewsArticles(ctx context.Context, key string, message []*request.GroupRobotMsgNewsArticles) (*response.ResponseGroupRobotSend, error) {
options := &power.HashMap{
"msgtype": "news",
"news": power.HashMap{
"articles": message,
},
}
return comp.Send(ctx, key, options)
}
func (comp *Client) SendFile(ctx context.Context, key string, message *request.GroupRobotMsgFile) (*response.ResponseGroupRobotSend, error) {
options := &power.HashMap{
"msgtype": "file",
"file": message,
}
return comp.Send(ctx, key, options)
}
func (comp *Client) SendTemplateCard(ctx context.Context, key string, message *request.GroupRobotMsgTemplateCard) (*response.ResponseGroupRobotSend, error) {
options := &power.HashMap{
"msgtype": "template_card",
"template_card": message,
}
return comp.Send(ctx, key, options)
}