github.com/chanxuehong/wechat@v0.0.0-20230222024006-36f0325263cd/mp/message/custom/msg.go (about) 1 package custom 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 = "video" // 视频消息 12 MsgTypeMusic core.MsgType = "music" // 音乐消息 13 MsgTypeNews core.MsgType = "news" // 图文消息 14 MsgTypeMPNews core.MsgType = "mpnews" // 图文消息, 发送已经创建好的图文 15 MsgTypeWxCard core.MsgType = "wxcard" // 卡卷消息 16 MsgTypeMenu core.MsgType = "msgmenu" // 小程序客服消息:菜单 17 MsgTypeWxMiniLink core.MsgType = "link" // 小程序客服消息:图文链接 18 ) 19 20 type MsgHeader struct { 21 ToUser string `json:"touser"` // 接收方 OpenID 22 MsgType core.MsgType `json:"msgtype"` 23 } 24 25 type CustomService struct { 26 KfAccount string `json:"kf_account"` 27 } 28 29 // 文本消息 30 type Text struct { 31 MsgHeader 32 Text struct { 33 Content string `json:"content"` // 支持换行符 34 } `json:"text"` 35 CustomService *CustomService `json:"customservice,omitempty"` 36 } 37 38 // 新建文本消息. 39 // 40 // 如果不指定客服则 kfAccount 留空. 41 func NewText(toUser, content, kfAccount string) (text *Text) { 42 text = &Text{ 43 MsgHeader: MsgHeader{ 44 ToUser: toUser, 45 MsgType: MsgTypeText, 46 }, 47 } 48 text.Text.Content = content 49 50 if kfAccount != "" { 51 text.CustomService = &CustomService{ 52 KfAccount: kfAccount, 53 } 54 } 55 return 56 } 57 58 // 图片消息 59 type Image struct { 60 MsgHeader 61 Image struct { 62 MediaId string `json:"media_id"` // 通过素材管理接口上传多媒体文件得到 MediaId 63 } `json:"image"` 64 CustomService *CustomService `json:"customservice,omitempty"` 65 } 66 67 // 新建图片消息. 68 // 69 // 如果不指定客服则 kfAccount 留空. 70 func NewImage(toUser, mediaId, kfAccount string) (image *Image) { 71 image = &Image{ 72 MsgHeader: MsgHeader{ 73 ToUser: toUser, 74 MsgType: MsgTypeImage, 75 }, 76 } 77 image.Image.MediaId = mediaId 78 79 if kfAccount != "" { 80 image.CustomService = &CustomService{ 81 KfAccount: kfAccount, 82 } 83 } 84 return 85 } 86 87 // 语音消息 88 type Voice struct { 89 MsgHeader 90 Voice struct { 91 MediaId string `json:"media_id"` // 通过素材管理接口上传多媒体文件得到 MediaId 92 } `json:"voice"` 93 CustomService *CustomService `json:"customservice,omitempty"` 94 } 95 96 // 新建语音消息. 97 // 98 // 如果不指定客服则 kfAccount 留空. 99 func NewVoice(toUser, mediaId, kfAccount string) (voice *Voice) { 100 voice = &Voice{ 101 MsgHeader: MsgHeader{ 102 ToUser: toUser, 103 MsgType: MsgTypeVoice, 104 }, 105 } 106 voice.Voice.MediaId = mediaId 107 108 if kfAccount != "" { 109 voice.CustomService = &CustomService{ 110 KfAccount: kfAccount, 111 } 112 } 113 return 114 } 115 116 // 视频消息 117 type Video struct { 118 MsgHeader 119 Video struct { 120 MediaId string `json:"media_id"` // 通过素材管理接口上传多媒体文件得到 MediaId 121 ThumbMediaId string `json:"thumb_media_id"` // 通过素材管理接口上传多媒体文件得到 ThumbMediaId 122 Title string `json:"title,omitempty"` // 视频消息的标题, 可以为 "" 123 Description string `json:"description,omitempty"` // 视频消息的描述, 可以为 "" 124 } `json:"video"` 125 CustomService *CustomService `json:"customservice,omitempty"` 126 } 127 128 // 新建视频消息. 129 // 130 // 如果不指定客服则 kfAccount 留空. 131 func NewVideo(toUser, mediaId, thumbMediaId, title, description, kfAccount string) (video *Video) { 132 video = &Video{ 133 MsgHeader: MsgHeader{ 134 ToUser: toUser, 135 MsgType: MsgTypeVideo, 136 }, 137 } 138 video.Video.MediaId = mediaId 139 video.Video.ThumbMediaId = thumbMediaId 140 video.Video.Title = title 141 video.Video.Description = description 142 143 if kfAccount != "" { 144 video.CustomService = &CustomService{ 145 KfAccount: kfAccount, 146 } 147 } 148 return 149 } 150 151 // 音乐消息 152 type Music struct { 153 MsgHeader 154 Music struct { 155 Title string `json:"title,omitempty"` // 音乐标题, 可以为 "" 156 Description string `json:"description,omitempty"` // 音乐描述, 可以为 "" 157 MusicURL string `json:"musicurl"` // 音乐链接 158 HQMusicURL string `json:"hqmusicurl"` // 高质量音乐链接, WIFI环境优先使用该链接播放音乐 159 ThumbMediaId string `json:"thumb_media_id"` // 通过素材管理接口上传多媒体文件得到 ThumbMediaId 160 } `json:"music"` 161 CustomService *CustomService `json:"customservice,omitempty"` 162 } 163 164 // 新建音乐消息. 165 // 166 // 如果不指定客服则 kfAccount 留空. 167 func NewMusic(toUser, thumbMediaId, musicURL, HQMusicURL, title, description, kfAccount string) (music *Music) { 168 music = &Music{ 169 MsgHeader: MsgHeader{ 170 ToUser: toUser, 171 MsgType: MsgTypeMusic, 172 }, 173 } 174 music.Music.ThumbMediaId = thumbMediaId 175 music.Music.MusicURL = musicURL 176 music.Music.HQMusicURL = HQMusicURL 177 music.Music.Title = title 178 music.Music.Description = description 179 180 if kfAccount != "" { 181 music.CustomService = &CustomService{ 182 KfAccount: kfAccount, 183 } 184 } 185 return 186 } 187 188 // 图文消息里的 Article 189 type Article struct { 190 Title string `json:"title,omitempty"` // 图文消息标题 191 Description string `json:"description,omitempty"` // 图文消息描述 192 URL string `json:"url,omitempty"` // 点击图文消息跳转链接 193 PicURL string `json:"picurl,omitempty"` // 图文消息的图片链接, 支持JPG, PNG格式, 较好的效果为大图640*320, 小图80*80 194 } 195 196 // 图文消息 197 type News struct { 198 MsgHeader 199 News struct { 200 Articles []Article `json:"articles,omitempty"` // 多条图文消息信息, 默认第一个item为大图, 注意, 如果图文数超过8, 则将会无响应 201 } `json:"news"` 202 CustomService *CustomService `json:"customservice,omitempty"` 203 } 204 205 // 新建图文消息. 206 // 207 // 如果不指定客服则 kfAccount 留空. 208 func NewNews(toUser string, articles []Article, kfAccount string) (news *News) { 209 news = &News{ 210 MsgHeader: MsgHeader{ 211 ToUser: toUser, 212 MsgType: MsgTypeNews, 213 }, 214 } 215 news.News.Articles = articles 216 217 if kfAccount != "" { 218 news.CustomService = &CustomService{ 219 KfAccount: kfAccount, 220 } 221 } 222 return 223 } 224 225 type MPNews struct { 226 MsgHeader 227 MPNews struct { 228 MediaId string `json:"media_id"` // 通过素材管理接口上传多媒体文件得到 MediaId 229 } `json:"mpnews"` 230 CustomService *CustomService `json:"customservice,omitempty"` 231 } 232 233 // 新建图文消息. 234 // 235 // 如果不指定客服则 kfAccount 留空. 236 func NewMPNews(toUser, mediaId, kfAccount string) (mpnews *MPNews) { 237 mpnews = &MPNews{ 238 MsgHeader: MsgHeader{ 239 ToUser: toUser, 240 MsgType: MsgTypeMPNews, 241 }, 242 } 243 mpnews.MPNews.MediaId = mediaId 244 245 if kfAccount != "" { 246 mpnews.CustomService = &CustomService{ 247 KfAccount: kfAccount, 248 } 249 } 250 return 251 } 252 253 // 卡券消息, 特别注意客服消息接口投放卡券仅支持非自定义Code码的卡券 254 type WxCard struct { 255 MsgHeader 256 WxCard struct { 257 CardId string `json:"card_id"` 258 CardExt string `json:"card_ext,omitempty"` 259 } `json:"wxcard"` 260 CustomService *CustomService `json:"customservice,omitempty"` 261 } 262 263 // 新建卡券消息. 264 // 265 // 如果不指定客服则 kfAccount 留空. 266 func NewWxCard(toUser, cardId, cardExt, kfAccount string) (card *WxCard) { 267 card = &WxCard{ 268 MsgHeader: MsgHeader{ 269 ToUser: toUser, 270 MsgType: MsgTypeWxCard, 271 }, 272 } 273 card.WxCard.CardId = cardId 274 card.WxCard.CardExt = cardExt 275 276 if kfAccount != "" { 277 card.CustomService = &CustomService{ 278 KfAccount: kfAccount, 279 } 280 } 281 return 282 } 283 284 type WxMiniLink struct { 285 MsgHeader 286 Link struct { 287 Title string `json:"title"` 288 Description string `json:"description"` 289 URL string `json:"url"` 290 ThumbURL string `json:"thumb_url"` 291 } `json:"link"` 292 CustomService *CustomService `json:"customservice,omitempty"` 293 } 294 295 func NewMiniLink(toUser, title, desc, url, thumbUrl, kfAccount string) (link *WxMiniLink) { 296 link = &WxMiniLink{ 297 MsgHeader: MsgHeader{ 298 ToUser: toUser, 299 MsgType: MsgTypeWxMiniLink, 300 }, 301 } 302 303 link.Link.Title = title 304 link.Link.Description = desc 305 link.Link.URL = url 306 link.Link.ThumbURL = thumbUrl 307 308 if kfAccount != "" { 309 link.CustomService = &CustomService{ 310 KfAccount: kfAccount, 311 } 312 } 313 return 314 } 315 316 type WxMiniPage struct { 317 MsgHeader 318 MiniProgramPage struct { 319 Title string `json:"title"` 320 PagePath string `json:"pagepath"` 321 ThumbMediaId string `json:"thumb_media_id"` 322 } `json:"miniprogrampage"` 323 CustomService *CustomService `json:"customservice,omitempty"` 324 } 325 326 func NewMiniPage(toUser, title, pagePath, thumbMediaId, kfAccount string) (page *WxMiniPage) { 327 page = &WxMiniPage{ 328 MsgHeader: MsgHeader{ 329 ToUser: toUser, 330 MsgType: MsgTypeWxMiniLink, 331 }, 332 } 333 334 page.MiniProgramPage.Title = title 335 page.MiniProgramPage.PagePath = pagePath 336 page.MiniProgramPage.ThumbMediaId = thumbMediaId 337 if kfAccount != "" { 338 page.CustomService = &CustomService{ 339 KfAccount: kfAccount, 340 } 341 } 342 return 343 } 344 345 type Menu struct { 346 MsgHeader 347 MsgMenu struct { 348 HeadContent string `json:"head_content"` 349 MenuItemList []MenuItem `json:"list"` 350 TailContent string `json:"tail_content"` 351 } `json:"msgmenu"` 352 CustomService *CustomService `json:"customservice,omitempty"` 353 } 354 355 type MenuItem struct { 356 Id int64 `json:"id"` 357 Content string `json:"content"` 358 } 359 360 func NewMenu(toUser, headContent, tailContent, kfAccount string, list []MenuItem) (menu *Menu) { 361 menu = &Menu{ 362 MsgHeader: MsgHeader{ 363 ToUser: toUser, 364 MsgType: MsgTypeMenu, 365 }, 366 } 367 368 menu.MsgMenu.HeadContent = headContent 369 menu.MsgMenu.TailContent = tailContent 370 menu.MsgMenu.MenuItemList = list 371 if kfAccount != "" { 372 menu.CustomService = &CustomService{ 373 KfAccount: kfAccount, 374 } 375 } 376 return 377 }