forked from chanxuehong/wechat
-
Notifications
You must be signed in to change notification settings - Fork 1
/
msg.go
121 lines (106 loc) · 2.38 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
121
package mass2all
import (
"github.com/charsunny/wechat/mp/core"
)
const (
MsgTypeText core.MsgType = "text"
MsgTypeImage core.MsgType = "image"
MsgTypeVoice core.MsgType = "voice"
MsgTypeVideo core.MsgType = "mpvideo"
MsgTypeNews core.MsgType = "mpnews"
MsgTypeWxCard core.MsgType = "wxcard"
)
type MsgHeader struct {
Filter struct {
IsToAll bool `json:"is_to_all"`
} `json:"filter"`
MsgType core.MsgType `json:"msgtype"`
}
type Text struct {
MsgHeader
Text struct {
Content string `json:"content"`
} `json:"text"`
}
func NewText(content string) *Text {
var msg Text
msg.MsgType = MsgTypeText
msg.Filter.IsToAll = true
msg.Text.Content = content
return &msg
}
type Image struct {
MsgHeader
Image struct {
MediaId string `json:"media_id"`
} `json:"image"`
}
func NewImage(mediaId string) *Image {
var msg Image
msg.MsgType = MsgTypeImage
msg.Filter.IsToAll = true
msg.Image.MediaId = mediaId
return &msg
}
type Voice struct {
MsgHeader
Voice struct {
MediaId string `json:"media_id"`
} `json:"voice"`
}
func NewVoice(mediaId string) *Voice {
var msg Voice
msg.MsgType = MsgTypeVoice
msg.Filter.IsToAll = true
msg.Voice.MediaId = mediaId
return &msg
}
type Video struct {
MsgHeader
Video struct {
MediaId string `json:"media_id"`
} `json:"mpvideo"`
}
// 新建视频消息
// NOTE: 对于临时素材, mediaId 应该通过 media.UploadVideo2 得到
func NewVideo(mediaId string) *Video {
var msg Video
msg.MsgType = MsgTypeVideo
msg.Filter.IsToAll = true
msg.Video.MediaId = mediaId
return &msg
}
// 图文消息
type News struct {
MsgHeader
News struct {
MediaId string `json:"media_id"`
} `json:"mpnews"`
}
// 新建图文消息
// NOTE: 对于临时素材, mediaId 应该通过 media.UploadNews 得到
func NewNews(mediaId string) *News {
var msg News
msg.MsgType = MsgTypeNews
msg.Filter.IsToAll = true
msg.News.MediaId = mediaId
return &msg
}
// 卡券消息
type WxCard struct {
MsgHeader
WxCard struct {
CardId string `json:"card_id"`
CardExt string `json:"card_ext,omitempty"`
} `json:"wxcard"`
}
// 新建卡券, 特别注意: 目前该接口仅支持填入非自定义code的卡券和预存模式的自定义code卡券.
// cardExt 可以为空
func NewWxCard(cardId, cardExt string) *WxCard {
var msg WxCard
msg.MsgType = MsgTypeWxCard
msg.Filter.IsToAll = true
msg.WxCard.CardId = cardId
msg.WxCard.CardExt = cardExt
return &msg
}