wxmsg

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2019 License: MIT Imports: 18 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的实现不需要关心它

*

  • 消息/事件的响应消息体构造和序列化为XML,被msg_parse.go调用
  • 目前支持消息类型有限,可以根据需要扩充

Index

Constants

View Source
const (
	SUCCESS_TEXT = "success"

	// msgType
	MT_TEXT  = "text"
	MT_EVENT = "event"

	// event type
	ET_SUBSCRIBE   = "subscribe"
	ET_UNSUBSCRIBE = "unsubscribe"
	ET_VIEW        = "VIEW"
)
View Source
const (
	SIGNATURE = iota
	TIMESTAMP
	NONCE
)

Variables

View Source
var (
	HandleTextMsg = func(textMsg *TextMsg) ReplyMsg {
		return NewReplyTextMsg(textMsg.FromUserName, textMsg.ToUserName, textMsg.Content)
	}
	HandleViewEvent = func(viewEvent *ViewEvent) ReplyMsg {
		return NewSuccessMsg()
	}
	HandleSubscribeEvent = func(subscribeEvent *SubscribeEvent) ReplyMsg {
		return NewReplyTextMsg(subscribeEvent.FromUserName, subscribeEvent.ToUserName, "welcome")
	}
	HandleUnsubscribeEvent = func(subscribeEvent *SubscribeEvent) ReplyMsg {
		return NewSuccessMsg()
	}
)
View Source
var MustSignatureArgs = []string{"signature", "timestamp", "nonce"}

Functions

func EncryptReply

func EncryptReply(replyMsg string, timestamp string, nonce string) []byte

根据响应消息生成加密消息

func GetMessageBody

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

获取服务号收到的消息参数,返回 (消息体, 时间戳, nonce, error)

func GetRandomBytes

func GetRandomBytes(n int) []byte

func GetReply

func GetReply(msgBody []byte) (string, error)

根据消息体获取返回消息

func HashStrings

func HashStrings(sl []string) string

func ParseMessageBody

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

从GetMessageBody()独立出来,可以通过各种方式调用,方便调试

func StartWxMsgParsers

func StartWxMsgParsers(workNum int)

初始化应用时启动若干个消息解析线程

Types

type BaseMsg

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

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

type EventMsg

type EventMsg struct {
	Msg
	Event string
}

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

type Msg

type Msg struct {
	BaseMsg
	MsgId string
}

type ReplyMsg

type ReplyMsg interface {
	ToXML() string
}

响应消息序列化XML接口定义

type ReplyMsgBase

type ReplyMsgBase struct {
	ToUserName   string
	FromUserName string
	CreateTime   int64
}

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() string

type SubscribeEvent

type SubscribeEvent struct {
	EventMsg
	EventKey string
	Ticket   string
}

subscribe, unsubscribe

type SuccessTextMsg

type SuccessTextMsg struct {
}

------------------ reply only "success" ----------

func NewSuccessMsg

func NewSuccessMsg() *SuccessTextMsg

func (*SuccessTextMsg) ToXML

func (msg *SuccessTextMsg) ToXML() string

type TextMsg

type TextMsg struct {
	Msg
	Content string
}

type ViewEvent

type ViewEvent struct {
	EventMsg
	EventKey string
	MenuId   string
}

VIEW

Jump to

Keyboard shortcuts

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