forked from chanxuehong/wechat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msg.go
120 lines (105 loc) · 2.62 KB
/
msg.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
// @description wechat 是腾讯微信公众平台 api 的 golang 语言封装
// @link https://gopkg.in/chanxuehong/wechat.v1 for the canonical source repository
// @license https://github.com/chanxuehong/wechat/blob/v1/LICENSE
// @authors chanxuehong(chanxuehong@gmail.com)
// 群发给分组的消息数据结构.
package mass2group
const (
MsgTypeText = "text"
MsgTypeImage = "image"
MsgTypeVoice = "voice"
MsgTypeVideo = "mpvideo"
MsgTypeNews = "mpnews"
MsgTypeWxCard = "wxcard"
)
type MessageHeader struct {
Filter struct {
GroupId int64 `json:"group_id"`
} `json:"filter"`
MsgType string `json:"msgtype"`
}
type Text struct {
MessageHeader
Text struct {
Content string `json:"content"`
} `json:"text"`
}
func NewText(groupId int64, content string) *Text {
var msg Text
msg.MsgType = MsgTypeText
msg.Filter.GroupId = groupId
msg.Text.Content = content
return &msg
}
type Image struct {
MessageHeader
Image struct {
MediaId string `json:"media_id"`
} `json:"image"`
}
func NewImage(groupId int64, mediaId string) *Image {
var msg Image
msg.MsgType = MsgTypeImage
msg.Filter.GroupId = groupId
msg.Image.MediaId = mediaId
return &msg
}
type Voice struct {
MessageHeader
Voice struct {
MediaId string `json:"media_id"`
} `json:"voice"`
}
func NewVoice(groupId int64, mediaId string) *Voice {
var msg Voice
msg.MsgType = MsgTypeVoice
msg.Filter.GroupId = groupId
msg.Voice.MediaId = mediaId
return &msg
}
type Video struct {
MessageHeader
Video struct {
MediaId string `json:"media_id"`
} `json:"mpvideo"`
}
// 新建视频消息
// NOTE: 对于临时素材, mediaId 应该通过 media.Client.CreateVideo 得到
func NewVideo(groupId int64, mediaId string) *Video {
var msg Video
msg.MsgType = MsgTypeVideo
msg.Filter.GroupId = groupId
msg.Video.MediaId = mediaId
return &msg
}
// 图文消息
type News struct {
MessageHeader
News struct {
MediaId string `json:"media_id"`
} `json:"mpnews"`
}
// 新建图文消息
// NOTE: 对于临时素材, mediaId 应该通过 media.Client.CreateNews 得到
func NewNews(groupId int64, mediaId string) *News {
var msg News
msg.MsgType = MsgTypeNews
msg.Filter.GroupId = groupId
msg.News.MediaId = mediaId
return &msg
}
// 卡券消息
type WxCard struct {
MessageHeader
WxCard struct {
CardId string `json:"card_id"`
} `json:"wxcard"`
}
// 新建卡券, 特别注意: 目前该接口仅支持填入非自定义code的卡券和预存模式的自定义code卡券.
func NewWxCard(groupId int64, cardId string) *WxCard {
var msg WxCard
msg.MsgType = MsgTypeWxCard
msg.Filter.GroupId = groupId
msg.WxCard.CardId = cardId
return &msg
}