-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
customer_message.go
125 lines (109 loc) · 3.31 KB
/
customer_message.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
package message
import (
"context"
"fmt"
context2 "github.com/donetkit/wechat/miniprogram/context"
"github.com/donetkit/wechat/util"
)
const (
customerSendMessage = "https://api.weixin.qq.com/cgi-bin/message/custom/send"
)
// Manager 消息管理者,可以发送消息
type Manager struct {
*context2.Context
}
// NewCustomerMessageManager 实例化消息管理者
func NewCustomerMessageManager(context *context2.Context) *Manager {
return &Manager{
context,
}
}
// MediaText 文本消息的文字
type MediaText struct {
Content string `json:"content"`
}
// MediaResource 消息使用的临时素材id
type MediaResource struct {
MediaID string `json:"media_id"`
}
// MediaMiniprogrampage 小程序卡片
type MediaMiniprogrampage struct {
Title string `json:"title"`
Appid string `json:"appid"`
Pagepath string `json:"pagepath"`
ThumbMediaID string `json:"thumb_media_id"`
}
// MediaLink 发送图文链接
type MediaLink struct {
Title string `json:"title"`
Description string `json:"description"`
URL string `json:"url"`
ThumbURL string `json:"thumb_url"`
}
// CustomerMessage 客服消息
type CustomerMessage struct {
ToUser string `json:"touser"` //接受者OpenID
Msgtype MsgType `json:"msgtype"` //客服消息类型
Text *MediaText `json:"text,omitempty"` //可选
Image *MediaResource `json:"image,omitempty"` //可选
Link *MediaLink `json:"link,omitempty"` //可选
Miniprogrampage *MediaMiniprogrampage `json:"miniprogrampage,omitempty"` //可选
}
// NewCustomerTextMessage 文本消息结构体构造方法
func NewCustomerTextMessage(toUser, text string) *CustomerMessage {
return &CustomerMessage{
ToUser: toUser,
Msgtype: MsgTypeText,
Text: &MediaText{
Content: text,
},
}
}
// NewCustomerImgMessage 图片消息的构造方法
func NewCustomerImgMessage(toUser, mediaID string) *CustomerMessage {
return &CustomerMessage{
ToUser: toUser,
Msgtype: MsgTypeImage,
Image: &MediaResource{
MediaID: mediaID,
},
}
}
// NewCustomerLinkMessage 图文链接消息的构造方法
func NewCustomerLinkMessage(toUser, title, description, url, thumbURL string) *CustomerMessage {
return &CustomerMessage{
ToUser: toUser,
Msgtype: MsgTypeLink,
Link: &MediaLink{
Title: title,
Description: description,
URL: url,
ThumbURL: thumbURL,
},
}
}
// NewCustomerMiniprogrampageMessage 小程序卡片消息的构造方法
func NewCustomerMiniprogrampageMessage(toUser, title, pagepath, thumbMediaID string) *CustomerMessage {
return &CustomerMessage{
ToUser: toUser,
Msgtype: MsgTypeMiniProgramPage,
Miniprogrampage: &MediaMiniprogrampage{
Title: title,
Pagepath: pagepath,
ThumbMediaID: thumbMediaID,
},
}
}
// Send 发送客服消息
func (manager *Manager) Send(ctx context.Context, msg *CustomerMessage) error {
accessToken, err := manager.Context.GetAccessToken(ctx)
if err != nil {
return err
}
uri := fmt.Sprintf("%s?access_token=%s", customerSendMessage, accessToken)
response, err := util.PostJSONContext(ctx, uri, msg)
if err != nil {
return err
}
return util.DecodeWithCommonError(response, "SendCustomerMessage")
}