package message import ( "encoding/xml" ) // MsgType 基本消息类型 type MsgType string // EventType 事件类型 type EventType string // InfoType 第三方平台授权事件类型 type InfoType string const ( //MsgTypeText 表示文本消息 MsgTypeText MsgType = "text" //MsgTypeImage 表示图片消息 MsgTypeImage = "image" //MsgTypeVoice 表示语音消息 MsgTypeVoice = "voice" //MsgTypeVideo 表示视频消息 MsgTypeVideo = "video" //MsgTypeShortVideo 表示短视频消息[限接收] MsgTypeShortVideo = "shortvideo" //MsgTypeLocation 表示坐标消息[限接收] MsgTypeLocation = "location" //MsgTypeLink 表示链接消息[限接收] MsgTypeLink = "link" //MsgTypeMusic 表示音乐消息[限回复] MsgTypeMusic = "music" //MsgTypeNews 表示图文消息[限回复] MsgTypeNews = "news" //MsgTypeTransfer 表示消息消息转发到客服 MsgTypeTransfer = "transfer_customer_service" //MsgTypeEvent 表示事件推送消息 MsgTypeEvent = "event" MsgTypeChannels = "channels" MsgTypeChnageContract = "change_contact" ) const ( //EventSubscribe 订阅 EventSubscribe EventType = "subscribe" //EventUnsubscribe 取消订阅 EventUnsubscribe = "unsubscribe" //EventScan 用户已经关注公众号,则微信会将带场景值扫描事件推送给开发者 EventScan = "SCAN" //EventLocation 上报地理位置事件 EventLocation = "LOCATION" //EventClick 点击菜单拉取消息时的事件推送 EventClick = "CLICK" //EventView 点击菜单跳转链接时的事件推送 EventView = "VIEW" //EventScancodePush 扫码推事件的事件推送 EventScancodePush = "scancode_push" //EventScancodeWaitmsg 扫码推事件且弹出“消息接收中”提示框的事件推送 EventScancodeWaitmsg = "scancode_waitmsg" //EventPicSysphoto 弹出系统拍照发图的事件推送 EventPicSysphoto = "pic_sysphoto" //EventPicPhotoOrAlbum 弹出拍照或者相册发图的事件推送 EventPicPhotoOrAlbum = "pic_photo_or_album" //EventPicWeixin 弹出微信相册发图器的事件推送 EventPicWeixin = "pic_weixin" //EventLocationSelect 弹出地理位置选择器的事件推送 EventLocationSelect = "location_select" //EventTemplateSendJobFinish 发送模板消息推送通知 EventTemplateSendJobFinish = "TEMPLATESENDJOBFINISH" //EventWxaMediaCheck 异步校验图片/音频是否含有违法违规内容推送事件 EventWxaMediaCheck = "wxa_media_check" ) const ( // InfoTypeVerifyTicket 返回ticket InfoTypeVerifyTicket InfoType = "component_verify_ticket" // InfoTypeAuthorized 授权 InfoTypeAuthorized = "authorized" // InfoTypeUnauthorized 取消授权 InfoTypeUnauthorized = "unauthorized" // InfoTypeUpdateAuthorized 更新授权 InfoTypeUpdateAuthorized = "updateauthorized" ) // MixMessage 存放所有微信发送过来的消息和事件 type MixMessage struct { CommonToken `json:"commonToken"` //基本消息 MsgID int64 `xml:"MsgId" json:"msgID,omitempty"` Content string `xml:"Content" json:"content,omitempty"` Recognition string `xml:"Recognition" json:"recognition,omitempty"` PicURL string `xml:"PicUrl" json:"picURL,omitempty"` MediaID string `xml:"MediaId" json:"mediaID,omitempty"` Format string `xml:"Format" json:"format,omitempty"` ThumbMediaID string `xml:"ThumbMediaId" json:"thumbMediaID,omitempty"` LocationX float64 `xml:"Location_X" json:"locationX,omitempty"` LocationY float64 `xml:"Location_Y" json:"locationY,omitempty"` Scale float64 `xml:"Scale" json:"scale,omitempty"` Label string `xml:"Label" json:"label,omitempty"` Title string `xml:"Title" json:"title,omitempty"` Description string `xml:"Description" json:"description,omitempty"` URL string `xml:"Url" json:"URL,omitempty"` //事件相关 Event EventType `xml:"Event" json:"event,omitempty"` EventKey string `xml:"EventKey" json:"eventKey,omitempty"` Ticket string `xml:"Ticket" json:"ticket,omitempty"` Latitude string `xml:"Latitude" json:"latitude,omitempty"` Longitude string `xml:"Longitude" json:"longitude,omitempty"` Precision string `xml:"Precision" json:"precision,omitempty"` MenuID string `xml:"MenuId" json:"menuID,omitempty"` Status string `xml:"Status" json:"status,omitempty"` SessionFrom string `xml:"SessionFrom" json:"sessionFrom,omitempty"` ScanCodeInfo struct { ScanType string `xml:"ScanType" json:"scanType,omitempty"` ScanResult string `xml:"ScanResult" json:"scanResult,omitempty"` } `xml:"ScanCodeInfo" json:"scanCodeInfo"` // 企业微信客服相关 OpenKfId string `xml:"OpenKfId" json:"openKfId,omitempty"` Token string `xml:"Token" json:"token,omitempty"` // 企业微信审核相关 ApprovalInfo struct { SpNo string `xml:"SpNo" json:"spNo,omitempty"` SpName string `xml:"SpName" json:"spName,omitempty"` SpStatus int `xml:"SpStatus" json:"spStatus,omitempty"` TemplateId string `xml:"TemplateId" json:"templateId,omitempty"` ApplyTime int64 `xml:"ApplyTime" json:"applyTime,omitempty"` Applyer struct { UserId string `xml:"UserId" json:"userId,omitempty"` } `xml:"Applyer" json:"applyer"` } `xml:"ApprovalInfo" json:"approvalInfo"` SendPicsInfo struct { Count int32 `xml:"Count" json:"count,omitempty"` PicList []EventPic `xml:"PicList>item" json:"picList,omitempty"` } `xml:"SendPicsInfo" json:"sendPicsInfo"` SendLocationInfo struct { LocationX float64 `xml:"Location_X" json:"locationX,omitempty"` LocationY float64 `xml:"Location_Y" json:"locationY,omitempty"` Scale float64 `xml:"Scale" json:"scale,omitempty"` Label string `xml:"Label" json:"label,omitempty"` Poiname string `xml:"Poiname" json:"poiname,omitempty"` } `json:"sendLocationInfo"` // 第三方平台相关 InfoType InfoType `xml:"InfoType" json:"infoType,omitempty"` AppID string `xml:"AppId" json:"appID,omitempty"` ComponentVerifyTicket string `xml:"ComponentVerifyTicket" json:"componentVerifyTicket,omitempty"` AuthorizerAppid string `xml:"AuthorizerAppid" json:"authorizerAppid,omitempty"` AuthorizationCode string `xml:"AuthorizationCode" json:"authorizationCode,omitempty"` AuthorizationCodeExpiredTime int64 `xml:"AuthorizationCodeExpiredTime" json:"authorizationCodeExpiredTime,omitempty"` PreAuthCode string `xml:"PreAuthCode" json:"preAuthCode,omitempty"` // 卡券相关 CardID string `xml:"CardId" json:"cardID,omitempty"` RefuseReason string `xml:"RefuseReason" json:"refuseReason,omitempty"` IsGiveByFriend int32 `xml:"IsGiveByFriend" json:"isGiveByFriend,omitempty"` FriendUserName string `xml:"FriendUserName" json:"friendUserName,omitempty"` UserCardCode string `xml:"UserCardCode" json:"userCardCode,omitempty"` OldUserCardCode string `xml:"OldUserCardCode" json:"oldUserCardCode,omitempty"` OuterStr string `xml:"OuterStr" json:"outerStr,omitempty"` IsRestoreMemberCard int32 `xml:"IsRestoreMemberCard" json:"isRestoreMemberCard,omitempty"` UnionID string `xml:"UnionId" json:"unionID,omitempty"` // 内容审核相关 IsRisky bool `xml:"isrisky" json:"isRisky,omitempty"` ExtraInfoJSON string `xml:"extra_info_json" json:"extraInfoJSON,omitempty"` TraceID string `xml:"trace_id" json:"traceID,omitempty"` StatusCode int `xml:"status_code" json:"statusCode,omitempty"` //通讯录同步相关 ChangeType string `xml:"ChangeType"` UserID string `xml:"UserID"` Department string `xml:"Department"` } // EventPic 发图事件推送 type EventPic struct { PicMd5Sum string `xml:"PicMd5Sum"` } // EncryptedXMLMsg 安全模式下的消息体 type EncryptedXMLMsg struct { XMLName struct{} `xml:"xml" json:"-"` ToUserName string `xml:"ToUserName" json:"ToUserName"` EncryptedMsg string `xml:"Encrypt" json:"Encrypt"` } // ResponseEncryptedXMLMsg 需要返回的消息体 type ResponseEncryptedXMLMsg struct { XMLName struct{} `xml:"xml" json:"-"` EncryptedMsg string `xml:"Encrypt" json:"Encrypt"` MsgSignature string `xml:"MsgSignature" json:"MsgSignature"` Timestamp int64 `xml:"TimeStamp" json:"TimeStamp"` Nonce string `xml:"Nonce" json:"Nonce"` } // CDATA 使用该类型,在序列化为 xml 文本时文本会被解析器忽略 type CDATA string // MarshalXML 实现自己的序列化方法 func (c CDATA) MarshalXML(e *xml.Encoder, start xml.StartElement) error { return e.EncodeElement(struct { string `xml:",cdata"` }{string(c)}, start) } // CommonToken 消息中通用的结构 type CommonToken struct { XMLName xml.Name `xml:"xml"` ToUserName CDATA `xml:"ToUserName"` FromUserName CDATA `xml:"FromUserName"` CreateTime int64 `xml:"CreateTime"` MsgType MsgType `xml:"MsgType"` } // SetToUserName set ToUserName func (msg *CommonToken) SetToUserName(toUserName CDATA) { msg.ToUserName = toUserName } // SetFromUserName set FromUserName func (msg *CommonToken) SetFromUserName(fromUserName CDATA) { msg.FromUserName = fromUserName } // SetCreateTime set createTime func (msg *CommonToken) SetCreateTime(createTime int64) { msg.CreateTime = createTime } // SetMsgType set MsgType func (msg *CommonToken) SetMsgType(msgType MsgType) { msg.MsgType = msgType }