-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.go
207 lines (178 loc) · 6.32 KB
/
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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package wechat
import (
"strings"
)
//Message 定义了消息推送中的应用消息
type Message struct {
ToTag string `json:"totag"` //标签ID列表,“|”分隔
ToUser string `json:"touser"` //成员ID列表,“|”分隔
ToParty string `json:"toparty"` //部门ID列表,“|”分隔
Safe int `json:"safe"` //是否保密消息,缺省0为否,1表示是
AgentId int `json:"agentid"` //企业应用ID
MsgType string `json:"msgtype"` //消息类型
Text *TextMessage `json:"text,omitempty"`
Image *MediaMessage `json:"image,omitempty"`
Voice *MediaMessage `json:"voice,omitempty"`
File *MediaMessage `json:"file,omitempty"`
Video *VideoMessage `json:"video,omitempty"`
News *NewsMessage `json:"news,omitempty"`
MpNews *MpNewsMessage `json:"mpnews,omitempty"`
Markdown *MarkdownMessage `json:"markdown,omitempty"`
TextCard *TextCardMessage `json:"textcard,omitempty"`
TaskCard *TaskCardMessage `json:"taskcard,omitempty"`
MiniprogramNotice *MiniprogramNoticeMessage `json:"miniprogramnotice,omitempty"`
}
//SetUser 设置消息的收件人
func (msg *Message) SetUser(users []string) {
msg.ToUser = strings.Join(users, "|")
}
//TextMessage 定义了消息推送中的文本消息
type TextMessage struct {
Content string `json:"content"`
}
//NewTextMessage 创建一条文本消息并设置内容,内容支持换行、超链接
func NewTextMessage(content string) *Message {
return &Message{
MsgType: "text",
Text: &TextMessage{Content: content},
}
}
//NewTextCardMessage 创建一条文本卡片消息并设置内容,内容支持换行、超链接
func NewTextCardMessage(title, description, url string) *Message {
return &Message{
MsgType: "textcard",
TextCard: &TextCardMessage{
Title: title,
Description: description,
Url: url,
},
}
}
//MediaMessage 定义了消息推送中的多媒体消息
type MediaMessage struct {
MediaId string `json:"media_id"`
}
//NewMediaMessage 创建一条素材消息(image、voice、file)
func NewMediaMessage(mediaType, mediaId string) *Message {
message := Message{MsgType: mediaType}
mediaMessage := &MediaMessage{MediaId: mediaId}
switch mediaType {
case "image":
message.Image = mediaMessage
case "voice":
message.Voice = mediaMessage
case "file":
message.File = mediaMessage
}
return &message
}
//VideoMessage 定义了消息推送中的视频消息
type VideoMessage struct {
MediaMessage
Title string `json:"title"`
Description string `json:"description"`
}
//NewVideoMessage 创建一条视频消息(image、voice、file)
func NewVideoMessage(title, description, mediaId string) *Message {
videoMessage := VideoMessage{
Title: title,
Description: description,
}
videoMessage.MediaId = mediaId
return &Message{
MsgType: "video",
Video: &videoMessage,
}
}
//TextCardMessage 定义了消息推送中的卡片消息
type TextCardMessage struct {
Title string `json:"title"`
Description string `json:"description"`
Url string `json:"url"`
BtnTxt string `json:"btntxt"`
}
//NewsArticle 定义了消息推送中的图文消息
type NewsArticle struct {
Title string `json:"title"`
Description string `json:"description"`
Url string `json:"url"`
PicUrl string `json:"picurl"`
}
//MaxNewsArticles 定义了图文消息中最大的图文消息条数
const MaxNewsArticles = 8
//NewsMessage 定义了图文消息中的图文内容
type NewsMessage struct {
Articles []NewsArticle `json:"articles"`
}
//NewNewsMessage 用于创建一条图文消息
func NewNewsMessage() *NewsMessage {
return &NewsMessage{
Articles: []NewsArticle{},
}
}
//Append 向图文消息添加一条新文章
func (nm *NewsMessage) Append(title, url, description, picUrl string) {
article := NewsArticle{
Title: title,
Description: description,
Url: url,
PicUrl: picUrl,
}
nm.Articles = append(nm.Articles, article)
}
//MpNewsArticle 定义了消息推送中的微信托管图文消息文章
type MpNewsArticle struct {
Title string `json:"title"`
ThumbMediaId string `json:"thumb_media_id"`
Author string `json:"author"`
ContentSourceUrl string `json:"content_source_url"`
Content string `json:"content"`
Digest string `json:"digest"`
}
//MaxMpNewsArticles 代表微信托管图文消息的最大文章数量
const MaxMpNewsArticles = 8
//MpNewsMessage 定义了消息推送中的微信托管图文消息
type MpNewsMessage struct {
Articles []MpNewsArticle `json:"articles"`
}
//MarkdownMessage 定义了消息推送中的Markdown消息
type MarkdownMessage struct {
Content string `json:"content"`
}
//NewMarkdownMessage 用于创建一条Markdown消息
func NewMarkdownMessage(content string) *Message {
return &Message{
MsgType: "markdown",
Markdown: &MarkdownMessage{Content: content},
}
}
//MiniprogramNoticeContentItem 定义了消息推送中的小程序通知消息内容
type MiniprogramNoticeContentItem struct {
Key string `json:"key"`
Value string `json:"value"`
}
//MiniprogramNoticeMessage 定义了消息推送中的小程序通知消息
type MiniprogramNoticeMessage struct {
Appid string `json:"appid"`
Page string `json:"page"`
Title string `json:"title"`
Description string `json:"description"`
EmphasisFirstItem bool `json:"emphasis_first_item"`
ContentItem []MiniprogramNoticeContentItem `json:"content_item"`
}
//TaskCardButton 定义了消息推送中的任务卡片消息按钮
type TaskCardButton struct {
Key string `json:"key"`
Name string `json:"name"`
ReplaceName string `json:"replace_name"`
Color string `json:"color"`
IsBold bool `json:"is_bold"`
}
//TaskCardMessage 定义了消息推送中的任务卡片消息
type TaskCardMessage struct {
Title string `json:"title"`
Description string `json:"description"`
Url string `json:"url"`
TaskId string `json:"task_id"`
Btn []TaskCardButton `json:"btn"`
}