github.com/chanxuehong/wechat@v0.0.0-20230222024006-36f0325263cd/mp/message/mass/preview/msg.go (about) 1 package preview 2 3 import ( 4 "github.com/chanxuehong/wechat/mp/core" 5 ) 6 7 const ( 8 MsgTypeText core.MsgType = "text" 9 MsgTypeImage core.MsgType = "image" 10 MsgTypeVoice core.MsgType = "voice" 11 MsgTypeVideo core.MsgType = "mpvideo" 12 MsgTypeNews core.MsgType = "mpnews" 13 MsgTypeWxCard core.MsgType = "wxcard" 14 ) 15 16 type MsgHeader struct { 17 ToWxName string `json:"towxname,omitempty"` 18 ToUser string `json:"touser,omitempty"` 19 MsgType core.MsgType `json:"msgtype"` 20 } 21 22 type Text struct { 23 MsgHeader 24 Text struct { 25 Content string `json:"content"` 26 } `json:"text"` 27 } 28 29 func NewText(touser, content string) *Text { 30 var msg Text 31 msg.MsgType = MsgTypeText 32 msg.ToUser = touser 33 msg.Text.Content = content 34 return &msg 35 } 36 37 func NewText2(towxname, content string) *Text { 38 var msg Text 39 msg.MsgType = MsgTypeText 40 msg.ToWxName = towxname 41 msg.Text.Content = content 42 return &msg 43 } 44 45 type Image struct { 46 MsgHeader 47 Image struct { 48 MediaId string `json:"media_id"` 49 } `json:"image"` 50 } 51 52 func NewImage(touser, mediaId string) *Image { 53 var msg Image 54 msg.MsgType = MsgTypeImage 55 msg.ToUser = touser 56 msg.Image.MediaId = mediaId 57 return &msg 58 } 59 60 func NewImage2(towxname, mediaId string) *Image { 61 var msg Image 62 msg.MsgType = MsgTypeImage 63 msg.ToWxName = towxname 64 msg.Image.MediaId = mediaId 65 return &msg 66 } 67 68 type Voice struct { 69 MsgHeader 70 Voice struct { 71 MediaId string `json:"media_id"` 72 } `json:"voice"` 73 } 74 75 func NewVoice(touser, mediaId string) *Voice { 76 var msg Voice 77 msg.MsgType = MsgTypeVoice 78 msg.ToUser = touser 79 msg.Voice.MediaId = mediaId 80 return &msg 81 } 82 83 func NewVoice2(towxname, mediaId string) *Voice { 84 var msg Voice 85 msg.MsgType = MsgTypeVoice 86 msg.ToWxName = towxname 87 msg.Voice.MediaId = mediaId 88 return &msg 89 } 90 91 type Video struct { 92 MsgHeader 93 Video struct { 94 MediaId string `json:"media_id"` 95 } `json:"mpvideo"` 96 } 97 98 // 新建视频消息 99 // 100 // NOTE: 对于临时素材, mediaId 应该通过 media.UploadVideo2 得到 101 func NewVideo(touser, mediaId string) *Video { 102 var msg Video 103 msg.MsgType = MsgTypeVideo 104 msg.ToUser = touser 105 msg.Video.MediaId = mediaId 106 return &msg 107 } 108 109 // 新建视频消息 110 // 111 // NOTE: 对于临时素材, mediaId 应该通过 media.UploadVideo2 得到 112 func NewVideo2(towxname, mediaId string) *Video { 113 var msg Video 114 msg.MsgType = MsgTypeVideo 115 msg.ToWxName = towxname 116 msg.Video.MediaId = mediaId 117 return &msg 118 } 119 120 // 图文消息 121 type News struct { 122 MsgHeader 123 News struct { 124 MediaId string `json:"media_id"` 125 } `json:"mpnews"` 126 } 127 128 // 新建图文消息 129 // 130 // NOTE: 对于临时素材, mediaId 应该通过 media.UploadNews 得到 131 func NewNews(touser, mediaId string) *News { 132 var msg News 133 msg.MsgType = MsgTypeNews 134 msg.ToUser = touser 135 msg.News.MediaId = mediaId 136 return &msg 137 } 138 139 // 新建图文消息 140 // 141 // NOTE: 对于临时素材, mediaId 应该通过 media.UploadNews 得到 142 func NewNews2(towxname, mediaId string) *News { 143 var msg News 144 msg.MsgType = MsgTypeNews 145 msg.ToWxName = towxname 146 msg.News.MediaId = mediaId 147 return &msg 148 } 149 150 // 卡券消息 151 type WxCard struct { 152 MsgHeader 153 WxCard struct { 154 CardId string `json:"card_id"` 155 CardExt string `json:"card_ext,omitempty"` 156 } `json:"wxcard"` 157 } 158 159 // 新建卡券, 特别注意: 目前该接口仅支持填入非自定义code的卡券和预存模式的自定义code卡券. 160 func NewWxCard(toUser, cardId, cardExt string) *WxCard { 161 var msg WxCard 162 msg.MsgType = MsgTypeWxCard 163 msg.ToUser = toUser 164 msg.WxCard.CardId = cardId 165 msg.WxCard.CardExt = cardExt 166 return &msg 167 } 168 169 // 新建卡券, 特别注意: 目前该接口仅支持填入非自定义code的卡券和预存模式的自定义code卡券. 170 // 171 // cardExt 可以为空 172 func NewWxCard2(towxname, cardId, cardExt string) *WxCard { 173 var msg WxCard 174 msg.MsgType = MsgTypeWxCard 175 msg.ToWxName = towxname 176 msg.WxCard.CardId = cardId 177 msg.WxCard.CardExt = cardExt 178 return &msg 179 }