wxmsg

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2020 License: MIT Imports: 19 Imported by: 0

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

View Source
const (
	SIGNATURE = iota
	TIMESTAMP
	NONCE
)
View Source
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"
)
View Source
const (
	// reply template name
	RTN_ENCRYPTED_REPLY = iota
	RTN_TEXT
	RTN_IMAGE
	RTN_VOICE
	RTN_VIDEO
	RTN_MUSIC
	RTN_NEWS
	RTN_TOTAL
)

Variables

View Source
var MustSignatureArgs = []string{"signature", "timestamp", "nonce"}
View Source
var SUCCESS_TEXT = []byte("success")

Functions

func GetRandomBytes

func GetRandomBytes(n int) []byte

func HashStrings

func HashStrings(sl []string) string

func ParseMessageBody

func ParseMessageBody(u *url.URL, body []byte) ([]byte, string, string, error)

使用缺省配置解析消息,可以用于调试

Types

type BaseMsg

type BaseMsg struct {
	ToUserName   string
	FromUserName string
	CreateTime   int
	MsgType      string
}

--------------------- message -------------------

type ClickEvent

type ClickEvent struct {
	EventMsg
	EventKey string
}

---- event CLICK ----

type EventMsg

type EventMsg struct {
	BaseMsg
	Event string
}

------------- event ---------------

type FnMessageHandler

type FnMessageHandler func(receivedMsg ReceivedMsg) ReplyMsg

type ImageMsg

type ImageMsg struct {
	Msg
	PicUrl  string
	MediaId string
}

---- image message ----

type LinkMsg

type LinkMsg struct {
	Msg
	Title       string
	Description string
	Url         string
}

---- link message -----

type LocationEvent

type LocationEvent struct {
	EventMsg
	Latitude  string
	Longitude string
	Precision string
}

---- event LOCATION -----

type LocationMsg

type LocationMsg struct {
	Msg
	Location_X string
	Location_Y string
	Scale      int
	Label      string
}

---- location message -----

type MassSentEvent

type MassSentEvent struct {
	EventMsg
	MsgID       string
	Status      string
	TotalCount  int
	FilterCount int
	SentCount   int
	ErrorCount  int
	ResultCount int
	ResultList  []ResultListItem
}

type Msg

type Msg struct {
	BaseMsg
	MsgId string
}

type NewsArticle

type NewsArticle struct {
	Title  string
	Desc   string
	PicUrl string
	Url    string
}

------------ 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 ReplyMsgBase struct {
	ToUserName   string
	FromUserName string
	CreateTime   int64
}

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

type ScancodeEvent struct {
	EventMsg
	EventKey   string
	ScanType   string
	ScanResult string
}

---- event scancode_waitmsg, scancode_push ----

type SubscribeEvent

type SubscribeEvent struct {
	EventMsg
	EventKey string
	Ticket   string
}

---- 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

type TemplateSentEvent struct {
	EventMsg
	MsgId  string
	Status string
}

---- event TEMPLATESENDJOBFINISH ----- 模板消息发送结果推送事件

type TextMsg

type TextMsg struct {
	Msg
	Content string
}

---- text message ----

type VideoMsg

type VideoMsg struct {
	Msg
	MediaId      string
	ThumbMediaId string
}

---- video message ----

type ViewEvent

type ViewEvent struct {
	EventMsg
	EventKey string
	MenuId   string
}

---- event VIEW ----

type VoiceMsg

type VoiceMsg struct {
	Msg
	MediaId     string
	Format      string
	Recognition string
}

---- voice message ----

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

func (p *WxAppIdMsgParser) GetMessageBody(r *http.Request) ([]byte, string, string, error)

获取服务号收到的消息参数,返回 (消息体, 时间戳, 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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL