Documentation ¶
Overview ¶
*
- 微信信息加解密/签名/随机数
- 1. HashStrings([]string) -- 微信常用的签名算法 []string -> sort -> sha1 -> hex
- 2. GetRandomBytes(int) -- 获取指定长度的随机串,随机字符为 数字/小写字母/大写字母
*
- 微信消息/事件的缺省处理函数
*
- 触发消息/事件处理
- 1. wxapi.ParseMessageBody() --- 获取消息/事件的(消息体,时间戳,nonce,error),主要用于调试
- 2. wxapi.GetReply([]byte) --- 根据消息体触发消息处理函数得到返回结果。如果使用default_rest.go的实现不需要关心它
*
- 服务号消息/事件的结构定义和解析
- Rosbit Xu
*
- 消息/事件的响应消息体构造和序列化为XML,被msg_parse.go调用
Index ¶
- Constants
- Variables
- func GetRandomBytes(n int) []byte
- func HashStrings(sl []string) string
- func ParseMessageBody(u *url.URL, body []byte) ([]byte, string, string, error)
- type BaseMsg
- type ClickEvent
- type EventMsg
- type FnMessageHandler
- type ImageMsg
- type LinkMsg
- type LocationEvent
- type LocationMsg
- type MassSentEvent
- type Msg
- type NewsArticle
- type PhotoEvent
- type ReceivedMsg
- type ReplyImageMsg
- type ReplyMsg
- type ReplyMsgBase
- type ReplyMusicMsg
- type ReplyNewsMsg
- type ReplyTextMsg
- type ReplyVideoMsg
- type ReplyVoiceMsg
- type ResultListItem
- type ScancodeEvent
- type SubscribeEvent
- type SuccessTextMsg
- type TemplateSentEvent
- type TextMsg
- type VideoMsg
- type ViewEvent
- type VoiceMsg
- type WhereEvent
- type WxAppIdMsgParser
- func (p *WxAppIdMsgParser) EncryptReply(replyMsg []byte, timestamp string, nonce string) []byte
- func (p *WxAppIdMsgParser) GetMessageBody(r *http.Request) ([]byte, string, string, error)
- func (p *WxAppIdMsgParser) GetReply(msgBody []byte) ([]byte, error)
- func (p *WxAppIdMsgParser) RegisterWxMsgHandler(msgHandler WxMsgHandler)
- type WxMsgHandler
- type WxMsgHandlerAdapter
- func (h *WxMsgHandlerAdapter) HandleClickEvent(clickEvent *ClickEvent) ReplyMsg
- func (h *WxMsgHandlerAdapter) HandleImageMsg(imgMsg *ImageMsg) ReplyMsg
- func (h *WxMsgHandlerAdapter) HandleLinkMsg(linkMsg *LinkMsg) ReplyMsg
- func (h *WxMsgHandlerAdapter) HandleLocationEvent(locEvent *LocationEvent) ReplyMsg
- func (h *WxMsgHandlerAdapter) HandleLocationMsg(locMsg *LocationMsg) ReplyMsg
- func (h *WxMsgHandlerAdapter) HandleMassSentEvent(massSentEvent *MassSentEvent) ReplyMsg
- func (h *WxMsgHandlerAdapter) HandlePhotoEvent(phoneEvent *PhotoEvent) ReplyMsg
- func (h *WxMsgHandlerAdapter) HandleScanEvent(subscribeEvent *SubscribeEvent) ReplyMsg
- func (h *WxMsgHandlerAdapter) HandleScancodePushEvent(scanEvent *ScancodeEvent) ReplyMsg
- func (h *WxMsgHandlerAdapter) HandleScancodeWaitEvent(scanEvent *ScancodeEvent) ReplyMsg
- func (h *WxMsgHandlerAdapter) HandleSubscribeEvent(subscribeEvent *SubscribeEvent) ReplyMsg
- func (h *WxMsgHandlerAdapter) HandleTemplateSentEvent(tempSentEvent *TemplateSentEvent) ReplyMsg
- func (h *WxMsgHandlerAdapter) HandleTextMsg(textMsg *TextMsg) ReplyMsg
- func (h *WxMsgHandlerAdapter) HandleUnsubscribeEvent(subscribeEvent *SubscribeEvent) ReplyMsg
- func (h *WxMsgHandlerAdapter) HandleVideoMsg(videoMsg *VideoMsg) ReplyMsg
- func (h *WxMsgHandlerAdapter) HandleViewEvent(viewEvent *ViewEvent) ReplyMsg
- func (h *WxMsgHandlerAdapter) HandleVoiceMsg(voiceMsg *VoiceMsg) ReplyMsg
- func (h *WxMsgHandlerAdapter) HandleWhereEvent(whereEvent *WhereEvent) ReplyMsg
Constants ¶
const ( SIGNATURE = iota TIMESTAMP NONCE )
const ( // msgType MT_TEXT = "text" MT_IMAGE = "image" MT_VOICE = "voice" MT_VIDEO = "video" MT_SHORTVIDEO = "shortvideo" MT_LOCATION = "location" MT_LINK = "link" MT_EVENT = "event" // special message type // event type ET_VIEW = "VIEW" ET_CLICK = "CLICK" ET_SUBSCRIBE = "subscribe" ET_UNSUBSCRIBE = "unsubscribe" ET_SCAN = "SCAN" // SCAN事件内容和subscribe是一样的,注意和scancode_xxx事件的区分 ET_WHERE = "location" // 由location_select菜单引起的事件 ET_LOCATION = "LOCATION" ET_PIC_SYSPHOTO = "pic_sysphoto" ET_PIC_PHOTO_OR_ALBUM = "pic_photo_or_album" ET_PIC_WEIXIN = "pic_weixin" ET_SCANCODE_WAITMSG = "scancode_waitmsg" ET_SCANCODE_PUSH = "scancode_push" ET_MASSSENDJOBFINISH = "MASSSENDJOBFINISH" ET_TEMPLATESENDJOBFINISH = "TEMPLATESENDJOBFINISH" )
const ( // reply template name RTN_ENCRYPTED_REPLY = iota RTN_TEXT RTN_IMAGE RTN_VOICE RTN_VIDEO RTN_MUSIC RTN_NEWS RTN_TOTAL )
Variables ¶
var MustSignatureArgs = []string{"signature", "timestamp", "nonce"}
var SUCCESS_TEXT = []byte("success")
Functions ¶
func GetRandomBytes ¶
func HashStrings ¶
Types ¶
type FnMessageHandler ¶
type FnMessageHandler func(receivedMsg ReceivedMsg) ReplyMsg
type LocationEvent ¶
---- event LOCATION -----
type LocationMsg ¶
---- location message -----
type MassSentEvent ¶
type NewsArticle ¶
------------ reply news message ---------------
func NewNewsArticle ¶
func NewNewsArticle(title, desc, picUrl, url string) *NewsArticle
type PhotoEvent ¶
type PhotoEvent struct { EventMsg EventKey string Count int // SendPicsInfo/Count PicList []string // SendPicsInfo/item/PicMd5Sum }
---- event pic_sysphoto, pic_photo_or_album, pic_weixin ---
type ReceivedMsg ¶
type ReceivedMsg interface {
// contains filtered or unexported methods
}
所有接收消息都实现的接口。消息处理可以统一为 ReceivedMsg -> [处理] -> ReplyMsg
type ReplyImageMsg ¶
type ReplyImageMsg struct { ReplyMsgBase MediaId string }
----------------- reply image message ----------
func NewReplyImageMsg ¶
func NewReplyImageMsg(toUserName, fromUserName, mediaId string) *ReplyImageMsg
func (*ReplyImageMsg) ToXML ¶
func (msg *ReplyImageMsg) ToXML() []byte
type ReplyMsg ¶
type ReplyMsg interface {
ToXML() []byte
}
所有响应消息都必须实现的接口:序列化为XML。由此消息处理过程为 ReceiveMsg -> [处理] -> ReplyMsg
type ReplyMsgBase ¶
type ReplyMusicMsg ¶
type ReplyMusicMsg struct { ReplyMsgBase ThumbMediaId string MusicUrl string HQMusicUrl string Title string Desc string }
----------------- reply music message -----------------
func NewReplyMusicMsg ¶
func NewReplyMusicMsg(toUserName, fromUserName, thumbMediaId, musicUrl, HQMusicUrl, title, desc string) *ReplyMusicMsg
func (*ReplyMusicMsg) ToXML ¶
func (msg *ReplyMusicMsg) ToXML() []byte
type ReplyNewsMsg ¶
type ReplyNewsMsg struct { ReplyMsgBase Articles []*NewsArticle }
func NewReplyNewsMsg ¶
func NewReplyNewsMsg(toUserName, fromUserName string, articles []*NewsArticle) *ReplyNewsMsg
func (*ReplyNewsMsg) ToXML ¶
func (msg *ReplyNewsMsg) ToXML() []byte
type ReplyTextMsg ¶
type ReplyTextMsg struct { ReplyMsgBase Content string }
---------- reply Text Message ---------------
func NewReplyTextMsg ¶
func NewReplyTextMsg(toUserName, fromUserName, content string) *ReplyTextMsg
func (*ReplyTextMsg) ToXML ¶
func (msg *ReplyTextMsg) ToXML() []byte
type ReplyVideoMsg ¶
type ReplyVideoMsg struct { ReplyMsgBase MediaId string Title string Desc string }
---------------- reply video mesage -----------------
func NewReplyVideoMsg ¶
func NewReplyVideoMsg(toUserName, fromUserName, mediaId, title, desc string) *ReplyVideoMsg
func (*ReplyVideoMsg) ToXML ¶
func (msg *ReplyVideoMsg) ToXML() []byte
type ReplyVoiceMsg ¶
type ReplyVoiceMsg struct { ReplyMsgBase MediaId string }
----------------- reply void message ----------------
func NewReplyVoiceMsg ¶
func NewReplyVoiceMsg(toUserName, fromUserName, mediaId string) *ReplyVoiceMsg
func (*ReplyVoiceMsg) ToXML ¶
func (msg *ReplyVoiceMsg) ToXML() []byte
type ResultListItem ¶
type ResultListItem struct { ArticleIdx string UserDeclareState string AuditState string OriginalArticleUrl string OriginalArticleType string CanReprint string NeedReplaceContent string NeedShowReprintSource string }
---- event MASSSENDJOBFINISH ---- 群发结果推送事件
type ScancodeEvent ¶
---- event scancode_waitmsg, scancode_push ----
type SubscribeEvent ¶
---- event subscribe, unsubscribe, SCAN ----
type SuccessTextMsg ¶
type SuccessTextMsg struct { }
------------------ reply only "success" ----------
func NewSuccessMsg ¶
func NewSuccessMsg() *SuccessTextMsg
func (*SuccessTextMsg) ToXML ¶
func (msg *SuccessTextMsg) ToXML() []byte
type TemplateSentEvent ¶
---- event TEMPLATESENDJOBFINISH ----- 模板消息发送结果推送事件
type WhereEvent ¶
type WhereEvent struct { EventMsg Location_X string Location_Y string Scale int Label string MsgId string }
---- event location ----- 由location_select菜单引起的事件
type WxAppIdMsgParser ¶
type WxAppIdMsgParser struct {
// contains filtered or unexported fields
}
func StartWxMsgParsers ¶
func StartWxMsgParsers(params *wxconf.WxParamsT, workNum int) *WxAppIdMsgParser
初始化应用时启动若干个消息解析线程
func (*WxAppIdMsgParser) EncryptReply ¶
func (p *WxAppIdMsgParser) EncryptReply(replyMsg []byte, timestamp string, nonce string) []byte
根据响应消息生成加密消息
func (*WxAppIdMsgParser) GetMessageBody ¶
获取服务号收到的消息参数,返回 (消息体, 时间戳, nonce, error)
func (*WxAppIdMsgParser) GetReply ¶
func (p *WxAppIdMsgParser) GetReply(msgBody []byte) ([]byte, error)
根据消息体获取返回消息
func (*WxAppIdMsgParser) RegisterWxMsgHandler ¶
func (p *WxAppIdMsgParser) RegisterWxMsgHandler(msgHandler WxMsgHandler)
type WxMsgHandler ¶
type WxMsgHandler interface { HandleTextMsg(textMsg *TextMsg) ReplyMsg HandleImageMsg(imgMsg *ImageMsg) ReplyMsg HandleVoiceMsg(voiceMsg *VoiceMsg) ReplyMsg HandleVideoMsg(videoMsg *VideoMsg) ReplyMsg HandleLocationMsg(locMsg *LocationMsg) ReplyMsg HandleLinkMsg(linkMsg *LinkMsg) ReplyMsg HandleClickEvent(clickEvent *ClickEvent) ReplyMsg HandleViewEvent(viewEvent *ViewEvent) ReplyMsg HandleScancodePushEvent(scanEvent *ScancodeEvent) ReplyMsg HandleScancodeWaitEvent(scanEvent *ScancodeEvent) ReplyMsg HandleSubscribeEvent(subscribeEvent *SubscribeEvent) ReplyMsg HandleUnsubscribeEvent(subscribeEvent *SubscribeEvent) ReplyMsg HandleScanEvent(subscribeEvent *SubscribeEvent) ReplyMsg HandleWhereEvent(whereEvent *WhereEvent) ReplyMsg HandlePhotoEvent(phoneEvent *PhotoEvent) ReplyMsg HandleLocationEvent(locEvent *LocationEvent) ReplyMsg HandleMassSentEvent(massSentEvent *MassSentEvent) ReplyMsg HandleTemplateSentEvent(tempSentEvent *TemplateSentEvent) ReplyMsg }
消息/事件处理接口定义
var MsgHandler WxMsgHandler = &WxMsgHandlerAdapter{}
缺省的消息/事件处理器,可以通过RegisterWxMsghandler覆盖
type WxMsgHandlerAdapter ¶
type WxMsgHandlerAdapter struct { }
缺省的消息处理器实现。如果要重新实现某些处理方法,需要 1. 在新的结构体中嵌入(embed)该结构体 2. 覆盖实现某些方法 3. 调用RegisterWxMsghandler覆盖缺省实现
func (*WxMsgHandlerAdapter) HandleClickEvent ¶
func (h *WxMsgHandlerAdapter) HandleClickEvent(clickEvent *ClickEvent) ReplyMsg
func (*WxMsgHandlerAdapter) HandleImageMsg ¶
func (h *WxMsgHandlerAdapter) HandleImageMsg(imgMsg *ImageMsg) ReplyMsg
func (*WxMsgHandlerAdapter) HandleLinkMsg ¶
func (h *WxMsgHandlerAdapter) HandleLinkMsg(linkMsg *LinkMsg) ReplyMsg
func (*WxMsgHandlerAdapter) HandleLocationEvent ¶
func (h *WxMsgHandlerAdapter) HandleLocationEvent(locEvent *LocationEvent) ReplyMsg
func (*WxMsgHandlerAdapter) HandleLocationMsg ¶
func (h *WxMsgHandlerAdapter) HandleLocationMsg(locMsg *LocationMsg) ReplyMsg
func (*WxMsgHandlerAdapter) HandleMassSentEvent ¶
func (h *WxMsgHandlerAdapter) HandleMassSentEvent(massSentEvent *MassSentEvent) ReplyMsg
func (*WxMsgHandlerAdapter) HandlePhotoEvent ¶
func (h *WxMsgHandlerAdapter) HandlePhotoEvent(phoneEvent *PhotoEvent) ReplyMsg
func (*WxMsgHandlerAdapter) HandleScanEvent ¶
func (h *WxMsgHandlerAdapter) HandleScanEvent(subscribeEvent *SubscribeEvent) ReplyMsg
func (*WxMsgHandlerAdapter) HandleScancodePushEvent ¶
func (h *WxMsgHandlerAdapter) HandleScancodePushEvent(scanEvent *ScancodeEvent) ReplyMsg
func (*WxMsgHandlerAdapter) HandleScancodeWaitEvent ¶
func (h *WxMsgHandlerAdapter) HandleScancodeWaitEvent(scanEvent *ScancodeEvent) ReplyMsg
func (*WxMsgHandlerAdapter) HandleSubscribeEvent ¶
func (h *WxMsgHandlerAdapter) HandleSubscribeEvent(subscribeEvent *SubscribeEvent) ReplyMsg
func (*WxMsgHandlerAdapter) HandleTemplateSentEvent ¶
func (h *WxMsgHandlerAdapter) HandleTemplateSentEvent(tempSentEvent *TemplateSentEvent) ReplyMsg
func (*WxMsgHandlerAdapter) HandleTextMsg ¶
func (h *WxMsgHandlerAdapter) HandleTextMsg(textMsg *TextMsg) ReplyMsg
func (*WxMsgHandlerAdapter) HandleUnsubscribeEvent ¶
func (h *WxMsgHandlerAdapter) HandleUnsubscribeEvent(subscribeEvent *SubscribeEvent) ReplyMsg
func (*WxMsgHandlerAdapter) HandleVideoMsg ¶
func (h *WxMsgHandlerAdapter) HandleVideoMsg(videoMsg *VideoMsg) ReplyMsg
func (*WxMsgHandlerAdapter) HandleViewEvent ¶
func (h *WxMsgHandlerAdapter) HandleViewEvent(viewEvent *ViewEvent) ReplyMsg
func (*WxMsgHandlerAdapter) HandleVoiceMsg ¶
func (h *WxMsgHandlerAdapter) HandleVoiceMsg(voiceMsg *VoiceMsg) ReplyMsg
func (*WxMsgHandlerAdapter) HandleWhereEvent ¶
func (h *WxMsgHandlerAdapter) HandleWhereEvent(whereEvent *WhereEvent) ReplyMsg