github.com/chanxuehong/wechat@v0.0.0-20230222024006-36f0325263cd/mp/message/callback/response/msg.go (about)

     1  // 被动回复的基本消息的数据结构定义, 更多的消息定义在对应的业务模块内.
     2  package response
     3  
     4  import (
     5  	"github.com/chanxuehong/wechat/mp/core"
     6  )
     7  
     8  const (
     9  	MsgTypeText                    core.MsgType = "text"                      // 文本消息
    10  	MsgTypeImage                   core.MsgType = "image"                     // 图片消息
    11  	MsgTypeVoice                   core.MsgType = "voice"                     // 语音消息
    12  	MsgTypeVideo                   core.MsgType = "video"                     // 视频消息
    13  	MsgTypeMusic                   core.MsgType = "music"                     // 音乐消息
    14  	MsgTypeNews                    core.MsgType = "news"                      // 图文消息
    15  	MsgTypeTransferCustomerService core.MsgType = "transfer_customer_service" // 将消息转发到多客服
    16  )
    17  
    18  // 文本消息
    19  type Text struct {
    20  	XMLName struct{} `xml:"xml" json:"-"`
    21  	core.MsgHeader
    22  	Content string `xml:"Content" json:"Content"` // 回复的消息内容(换行: 在content中能够换行, 微信客户端支持换行显示)
    23  }
    24  
    25  func NewText(to, from string, timestamp int64, content string) (text *Text) {
    26  	return &Text{
    27  		MsgHeader: core.MsgHeader{
    28  			ToUserName:   to,
    29  			FromUserName: from,
    30  			CreateTime:   timestamp,
    31  			MsgType:      MsgTypeText,
    32  		},
    33  		Content: content,
    34  	}
    35  }
    36  
    37  // 图片消息
    38  type Image struct {
    39  	XMLName struct{} `xml:"xml" json:"-"`
    40  	core.MsgHeader
    41  	Image struct {
    42  		MediaId string `xml:"MediaId" json:"MediaId"` // 通过素材管理接口上传多媒体文件得到 MediaId
    43  	} `xml:"Image" json:"Image"`
    44  }
    45  
    46  func NewImage(to, from string, timestamp int64, mediaId string) (image *Image) {
    47  	image = &Image{
    48  		MsgHeader: core.MsgHeader{
    49  			ToUserName:   to,
    50  			FromUserName: from,
    51  			CreateTime:   timestamp,
    52  			MsgType:      MsgTypeImage,
    53  		},
    54  	}
    55  	image.Image.MediaId = mediaId
    56  	return
    57  }
    58  
    59  // 语音消息
    60  type Voice struct {
    61  	XMLName struct{} `xml:"xml" json:"-"`
    62  	core.MsgHeader
    63  	Voice struct {
    64  		MediaId string `xml:"MediaId" json:"MediaId"` // 通过素材管理接口上传多媒体文件得到 MediaId
    65  	} `xml:"Voice" json:"Voice"`
    66  }
    67  
    68  func NewVoice(to, from string, timestamp int64, mediaId string) (voice *Voice) {
    69  	voice = &Voice{
    70  		MsgHeader: core.MsgHeader{
    71  			ToUserName:   to,
    72  			FromUserName: from,
    73  			CreateTime:   timestamp,
    74  			MsgType:      MsgTypeVoice,
    75  		},
    76  	}
    77  	voice.Voice.MediaId = mediaId
    78  	return
    79  }
    80  
    81  // 视频消息
    82  type Video struct {
    83  	XMLName struct{} `xml:"xml" json:"-"`
    84  	core.MsgHeader
    85  	Video struct {
    86  		MediaId     string `xml:"MediaId"               json:"MediaId"`               // 通过素材管理接口上传多媒体文件得到 MediaId
    87  		Title       string `xml:"Title,omitempty"       json:"Title,omitempty"`       // 视频消息的标题, 可以为空
    88  		Description string `xml:"Description,omitempty" json:"Description,omitempty"` // 视频消息的描述, 可以为空
    89  	} `xml:"Video" json:"Video"`
    90  }
    91  
    92  func NewVideo(to, from string, timestamp int64, mediaId, title, description string) (video *Video) {
    93  	video = &Video{
    94  		MsgHeader: core.MsgHeader{
    95  			ToUserName:   to,
    96  			FromUserName: from,
    97  			CreateTime:   timestamp,
    98  			MsgType:      MsgTypeVideo,
    99  		},
   100  	}
   101  	video.Video.MediaId = mediaId
   102  	video.Video.Title = title
   103  	video.Video.Description = description
   104  	return
   105  }
   106  
   107  // 音乐消息
   108  type Music struct {
   109  	XMLName struct{} `xml:"xml" json:"-"`
   110  	core.MsgHeader
   111  	Music struct {
   112  		Title        string `xml:"Title,omitempty"        json:"Title,omitempty"`       // 音乐标题
   113  		Description  string `xml:"Description,omitempty"  json:"Description,omitempty"` // 音乐描述
   114  		MusicURL     string `xml:"MusicUrl"               json:"MusicUrl"`              // 音乐链接
   115  		HQMusicURL   string `xml:"HQMusicUrl"             json:"HQMusicUrl"`            // 高质量音乐链接, WIFI环境优先使用该链接播放音乐
   116  		ThumbMediaId string `xml:"ThumbMediaId"           json:"ThumbMediaId"`          // 通过素材管理接口上传多媒体文件得到 ThumbMediaId
   117  	} `xml:"Music" json:"Music"`
   118  }
   119  
   120  func NewMusic(to, from string, timestamp int64, thumbMediaId, musicURL, HQMusicURL, title, description string) (music *Music) {
   121  	music = &Music{
   122  		MsgHeader: core.MsgHeader{
   123  			ToUserName:   to,
   124  			FromUserName: from,
   125  			CreateTime:   timestamp,
   126  			MsgType:      MsgTypeMusic,
   127  		},
   128  	}
   129  	music.Music.Title = title
   130  	music.Music.Description = description
   131  	music.Music.MusicURL = musicURL
   132  	music.Music.HQMusicURL = HQMusicURL
   133  	music.Music.ThumbMediaId = thumbMediaId
   134  	return
   135  }
   136  
   137  // 图文消息里的 Article
   138  type Article struct {
   139  	Title       string `xml:"Title,omitempty"       json:"Title,omitempty"`       // 图文消息标题
   140  	Description string `xml:"Description,omitempty" json:"Description,omitempty"` // 图文消息描述
   141  	PicURL      string `xml:"PicUrl,omitempty"      json:"PicUrl,omitempty"`      // 图片链接, 支持JPG, PNG格式, 较好的效果为大图360*200, 小图200*200
   142  	URL         string `xml:"Url,omitempty"         json:"Url,omitempty"`         // 点击图文消息跳转链接
   143  }
   144  
   145  // 图文消息
   146  type News struct {
   147  	XMLName struct{} `xml:"xml" json:"-"`
   148  	core.MsgHeader
   149  	ArticleCount int       `xml:"ArticleCount"            json:"ArticleCount"`       // 图文消息个数, 限制为10条以内
   150  	Articles     []Article `xml:"Articles>item,omitempty" json:"Articles,omitempty"` // 多条图文消息信息, 默认第一个item为大图, 注意, 如果图文数超过10, 则将会无响应
   151  }
   152  
   153  func NewNews(to, from string, timestamp int64, articles []Article) (news *News) {
   154  	news = &News{
   155  		MsgHeader: core.MsgHeader{
   156  			ToUserName:   to,
   157  			FromUserName: from,
   158  			CreateTime:   timestamp,
   159  			MsgType:      MsgTypeNews,
   160  		},
   161  	}
   162  	news.ArticleCount = len(articles)
   163  	news.Articles = articles
   164  	return
   165  }
   166  
   167  // 将消息转发到多客服, 参见多客服模块
   168  type TransferToCustomerService struct {
   169  	XMLName struct{} `xml:"xml" json:"-"`
   170  	core.MsgHeader
   171  	TransInfo *TransInfo `xml:"TransInfo,omitempty" json:"TransInfo,omitempty"`
   172  }
   173  
   174  type TransInfo struct {
   175  	KfAccount string `xml:"KfAccount" json:"KfAccount"`
   176  }
   177  
   178  // 如果不指定客服则 kfAccount 留空.
   179  func NewTransferToCustomerService(to, from string, timestamp int64, kfAccount string) (msg *TransferToCustomerService) {
   180  	msg = &TransferToCustomerService{
   181  		MsgHeader: core.MsgHeader{
   182  			ToUserName:   to,
   183  			FromUserName: from,
   184  			CreateTime:   timestamp,
   185  			MsgType:      MsgTypeTransferCustomerService,
   186  		},
   187  	}
   188  	if kfAccount != "" {
   189  		msg.TransInfo = &TransInfo{
   190  			KfAccount: kfAccount,
   191  		}
   192  	}
   193  	return
   194  }