Documentation
¶
Overview ¶
封装微信服务器推送过来的消息(事件)处理 Handler.
Index ¶
- Constants
- func ServeHTTP(w http.ResponseWriter, r *http.Request, urlValues url.Values, agent Agent, ...)
- func WriteImage(w io.Writer, msg *response.Image, timestamp int64, nonce string, ...) (err error)
- func WriteNews(w io.Writer, msg *response.News, timestamp int64, nonce string, ...) (err error)
- func WriteText(w io.Writer, msg *response.Text, timestamp int64, nonce string, ...) (err error)
- func WriteVideo(w io.Writer, msg *response.Video, timestamp int64, nonce string, ...) (err error)
- func WriteVoice(w io.Writer, msg *response.Voice, timestamp int64, nonce string, ...) (err error)
- type Agent
- type AgentFrontend
- type DefaultAgent
- func (this *DefaultAgent) GetAESKey() [32]byte
- func (this *DefaultAgent) GetAgentId() int64
- func (this *DefaultAgent) GetCorpId() string
- func (this *DefaultAgent) GetToken() string
- func (this *DefaultAgent) Init(CorpId string, AgentId int64, Token string, AESKey []byte)
- func (this *DefaultAgent) ServeImageMsg(w http.ResponseWriter, r *http.Request, msg *request.Image, rawXMLMsg []byte, ...)
- func (this *DefaultAgent) ServeLocationEvent(w http.ResponseWriter, r *http.Request, event *request.LocationEvent, ...)
- func (this *DefaultAgent) ServeLocationMsg(w http.ResponseWriter, r *http.Request, msg *request.Location, ...)
- func (this *DefaultAgent) ServeMenuClickEvent(w http.ResponseWriter, r *http.Request, event *request.MenuClickEvent, ...)
- func (this *DefaultAgent) ServeMenuLocationSelectEvent(w http.ResponseWriter, r *http.Request, event *request.MenuLocationSelectEvent, ...)
- func (this *DefaultAgent) ServeMenuPicPhotoOrAlbumEvent(w http.ResponseWriter, r *http.Request, ...)
- func (this *DefaultAgent) ServeMenuPicSysPhotoEvent(w http.ResponseWriter, r *http.Request, event *request.MenuPicSysPhotoEvent, ...)
- func (this *DefaultAgent) ServeMenuPicWeixinEvent(w http.ResponseWriter, r *http.Request, event *request.MenuPicWeixinEvent, ...)
- func (this *DefaultAgent) ServeMenuScanCodePushEvent(w http.ResponseWriter, r *http.Request, event *request.MenuScanCodePushEvent, ...)
- func (this *DefaultAgent) ServeMenuScanCodeWaitMsgEvent(w http.ResponseWriter, r *http.Request, ...)
- func (this *DefaultAgent) ServeMenuViewEvent(w http.ResponseWriter, r *http.Request, event *request.MenuViewEvent, ...)
- func (this *DefaultAgent) ServeSubscribeEvent(w http.ResponseWriter, r *http.Request, event *request.SubscribeEvent, ...)
- func (this *DefaultAgent) ServeTextMsg(w http.ResponseWriter, r *http.Request, msg *request.Text, rawXMLMsg []byte, ...)
- func (this *DefaultAgent) ServeUnknownMsg(w http.ResponseWriter, r *http.Request, rawXMLMsg []byte, timestamp int64, ...)
- func (this *DefaultAgent) ServeUnsubscribeEvent(w http.ResponseWriter, r *http.Request, event *request.UnsubscribeEvent, ...)
- func (this *DefaultAgent) ServeVideoMsg(w http.ResponseWriter, r *http.Request, msg *request.Video, rawXMLMsg []byte, ...)
- func (this *DefaultAgent) ServeVoiceMsg(w http.ResponseWriter, r *http.Request, msg *request.Voice, rawXMLMsg []byte, ...)
- type InvalidRequestHandler
- type InvalidRequestHandlerFunc
- type MultiAgentFrontend
- func (this *MultiAgentFrontend) DeleteAgent(agentkey string)
- func (this *MultiAgentFrontend) DeleteAllAgent()
- func (this *MultiAgentFrontend) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (this *MultiAgentFrontend) SetAgent(agentkey string, agent Agent)
- func (this *MultiAgentFrontend) SetInvalidRequestHandler(handler InvalidRequestHandler)
Constants ¶
View Source
const URLQueryAgentKeyName = "agentkey"
定义回调 URL 上指定 Agent 的查询参数名
Variables ¶
This section is empty.
Functions ¶
func ServeHTTP ¶
func ServeHTTP(w http.ResponseWriter, r *http.Request, urlValues url.Values, agent Agent, invalidRequestHandler InvalidRequestHandler)
ServeHTTP 处理 http 消息请求
NOTE: 确保所有参数合法, r.Body 能正确读取数据
func WriteImage ¶
func WriteImage(w io.Writer, msg *response.Image, timestamp int64, nonce string, AESKey [32]byte, random []byte, CorpId, Token string) (err error)
把 image 回复消息 msg 写入 writer w
func WriteNews ¶
func WriteNews(w io.Writer, msg *response.News, timestamp int64, nonce string, AESKey [32]byte, random []byte, CorpId, Token string) (err error)
把 news 回复消息 msg 写入 writer w
func WriteText ¶
func WriteText(w io.Writer, msg *response.Text, timestamp int64, nonce string, AESKey [32]byte, random []byte, CorpId, Token string) (err error)
把 text 回复消息 msg 写入 writer w
Types ¶
type Agent ¶
type Agent interface {
GetCorpId() string // 获取应用所属的企业号Id
GetAgentId() int64 // 获取应用的Id
GetToken() string // 获取应用的Token
GetAESKey() [32]byte // 获取 32bytes 的 AES 加密 Key
// 未知类型的消息处理方法
// rawXMLMsg 是解密后的"明文" xml 消息体
// timestamp 是请求 URL 中的时间戳
// nonce 是请求 URL 中的随机数
// random 是请求 http body 中的密文消息加密时所用的 random, 16 bytes
// r *http.Request 的 Body 已经读取过了, 不要再读取了, 但是可以获取其他信息, 比如 r.URL.RawQuery
ServeUnknownMsg(w http.ResponseWriter, r *http.Request, rawXMLMsg []byte, timestamp int64, nonce string, random []byte)
// 消息处理函数
// msg 是成功解析的消息结构体
// rawXMLMsg 是解密后的"明文" xml 消息体
// timestamp 是请求 URL 中的时间戳
// nonce 是请求 URL 中的随机数
// random 是请求 http body 中的密文消息加密时所用的 random, 16 bytes
// r *http.Request 的 Body 已经读取过了, 不要再读取了, 但是可以获取其他信息, 比如 r.URL.RawQuery
ServeTextMsg(w http.ResponseWriter, r *http.Request, msg *request.Text, rawXMLMsg []byte, timestamp int64, nonce string, random []byte)
ServeImageMsg(w http.ResponseWriter, r *http.Request, msg *request.Image, rawXMLMsg []byte, timestamp int64, nonce string, random []byte)
ServeVoiceMsg(w http.ResponseWriter, r *http.Request, msg *request.Voice, rawXMLMsg []byte, timestamp int64, nonce string, random []byte)
ServeVideoMsg(w http.ResponseWriter, r *http.Request, msg *request.Video, rawXMLMsg []byte, timestamp int64, nonce string, random []byte)
ServeLocationMsg(w http.ResponseWriter, r *http.Request, msg *request.Location, rawXMLMsg []byte, timestamp int64, nonce string, random []byte)
// 事件处理函数
// event 是成功解析的消息结构体
// rawXMLMsg 是解密后的"明文" xml 消息体
// timestamp 是请求 URL 中的时间戳
// nonce 是请求 URL 中的随机数
// random 是请求 http body 中的密文消息加密时所用的 random, 16 bytes
// r *http.Request 的 Body 已经读取过了, 不要再读取了, 但是可以获取其他信息, 比如 r.URL.RawQuery
ServeSubscribeEvent(w http.ResponseWriter, r *http.Request, event *request.SubscribeEvent, rawXMLMsg []byte, timestamp int64, nonce string, random []byte)
ServeUnsubscribeEvent(w http.ResponseWriter, r *http.Request, event *request.UnsubscribeEvent, rawXMLMsg []byte, timestamp int64, nonce string, random []byte)
ServeLocationEvent(w http.ResponseWriter, r *http.Request, event *request.LocationEvent, rawXMLMsg []byte, timestamp int64, nonce string, random []byte)
ServeMenuClickEvent(w http.ResponseWriter, r *http.Request, event *request.MenuClickEvent, rawXMLMsg []byte, timestamp int64, nonce string, random []byte)
ServeMenuViewEvent(w http.ResponseWriter, r *http.Request, event *request.MenuViewEvent, rawXMLMsg []byte, timestamp int64, nonce string, random []byte)
ServeMenuScanCodePushEvent(w http.ResponseWriter, r *http.Request, event *request.MenuScanCodePushEvent, rawXMLMsg []byte, timestamp int64, nonce string, random []byte)
ServeMenuScanCodeWaitMsgEvent(w http.ResponseWriter, r *http.Request, event *request.MenuScanCodeWaitMsgEvent, rawXMLMsg []byte, timestamp int64, nonce string, random []byte)
ServeMenuPicSysPhotoEvent(w http.ResponseWriter, r *http.Request, event *request.MenuPicSysPhotoEvent, rawXMLMsg []byte, timestamp int64, nonce string, random []byte)
ServeMenuPicPhotoOrAlbumEvent(w http.ResponseWriter, r *http.Request, event *request.MenuPicPhotoOrAlbumEvent, rawXMLMsg []byte, timestamp int64, nonce string, random []byte)
ServeMenuPicWeixinEvent(w http.ResponseWriter, r *http.Request, event *request.MenuPicWeixinEvent, rawXMLMsg []byte, timestamp int64, nonce string, random []byte)
ServeMenuLocationSelectEvent(w http.ResponseWriter, r *http.Request, event *request.MenuLocationSelectEvent, rawXMLMsg []byte, timestamp int64, nonce string, random []byte)
}
企业号单个应用对外暴露的接口
type AgentFrontend ¶
type AgentFrontend struct {
// contains filtered or unexported fields
}
Agent 的前端, 负责处理 http 请求, net/http.Handler 的实现
NOTE: 只能处理一个企业号应用的消息
func NewAgentFrontend ¶
func NewAgentFrontend(agent Agent, invalidRequestHandler InvalidRequestHandler) *AgentFrontend
创建一个新的 AgentFrontend.
agent 不能为 nil, 如果 invalidRequestHandler == nil 则使用 DefaultInvalidRequestHandler
func (*AgentFrontend) ServeHTTP ¶
func (this *AgentFrontend) ServeHTTP(w http.ResponseWriter, r *http.Request)
type DefaultAgent ¶
func (*DefaultAgent) GetAESKey ¶
func (this *DefaultAgent) GetAESKey() [32]byte
func (*DefaultAgent) GetAgentId ¶
func (this *DefaultAgent) GetAgentId() int64
func (*DefaultAgent) GetCorpId ¶
func (this *DefaultAgent) GetCorpId() string
func (*DefaultAgent) GetToken ¶
func (this *DefaultAgent) GetToken() string
func (*DefaultAgent) Init ¶
func (this *DefaultAgent) Init(CorpId string, AgentId int64, Token string, AESKey []byte)
func (*DefaultAgent) ServeImageMsg ¶
func (*DefaultAgent) ServeLocationEvent ¶
func (this *DefaultAgent) ServeLocationEvent(w http.ResponseWriter, r *http.Request, event *request.LocationEvent, rawXMLMsg []byte, timestamp int64, nonce string, random []byte)
func (*DefaultAgent) ServeLocationMsg ¶
func (*DefaultAgent) ServeMenuClickEvent ¶
func (this *DefaultAgent) ServeMenuClickEvent(w http.ResponseWriter, r *http.Request, event *request.MenuClickEvent, rawXMLMsg []byte, timestamp int64, nonce string, random []byte)
func (*DefaultAgent) ServeMenuLocationSelectEvent ¶
func (this *DefaultAgent) ServeMenuLocationSelectEvent(w http.ResponseWriter, r *http.Request, event *request.MenuLocationSelectEvent, rawXMLMsg []byte, timestamp int64, nonce string, random []byte)
func (*DefaultAgent) ServeMenuPicPhotoOrAlbumEvent ¶
func (this *DefaultAgent) ServeMenuPicPhotoOrAlbumEvent(w http.ResponseWriter, r *http.Request, event *request.MenuPicPhotoOrAlbumEvent, rawXMLMsg []byte, timestamp int64, nonce string, random []byte)
func (*DefaultAgent) ServeMenuPicSysPhotoEvent ¶
func (this *DefaultAgent) ServeMenuPicSysPhotoEvent(w http.ResponseWriter, r *http.Request, event *request.MenuPicSysPhotoEvent, rawXMLMsg []byte, timestamp int64, nonce string, random []byte)
func (*DefaultAgent) ServeMenuPicWeixinEvent ¶
func (this *DefaultAgent) ServeMenuPicWeixinEvent(w http.ResponseWriter, r *http.Request, event *request.MenuPicWeixinEvent, rawXMLMsg []byte, timestamp int64, nonce string, random []byte)
func (*DefaultAgent) ServeMenuScanCodePushEvent ¶
func (this *DefaultAgent) ServeMenuScanCodePushEvent(w http.ResponseWriter, r *http.Request, event *request.MenuScanCodePushEvent, rawXMLMsg []byte, timestamp int64, nonce string, random []byte)
func (*DefaultAgent) ServeMenuScanCodeWaitMsgEvent ¶
func (this *DefaultAgent) ServeMenuScanCodeWaitMsgEvent(w http.ResponseWriter, r *http.Request, event *request.MenuScanCodeWaitMsgEvent, rawXMLMsg []byte, timestamp int64, nonce string, random []byte)
func (*DefaultAgent) ServeMenuViewEvent ¶
func (this *DefaultAgent) ServeMenuViewEvent(w http.ResponseWriter, r *http.Request, event *request.MenuViewEvent, rawXMLMsg []byte, timestamp int64, nonce string, random []byte)
func (*DefaultAgent) ServeSubscribeEvent ¶
func (this *DefaultAgent) ServeSubscribeEvent(w http.ResponseWriter, r *http.Request, event *request.SubscribeEvent, rawXMLMsg []byte, timestamp int64, nonce string, random []byte)
func (*DefaultAgent) ServeTextMsg ¶
func (*DefaultAgent) ServeUnknownMsg ¶
func (this *DefaultAgent) ServeUnknownMsg(w http.ResponseWriter, r *http.Request, rawXMLMsg []byte, timestamp int64, nonce string, random []byte)
func (*DefaultAgent) ServeUnsubscribeEvent ¶
func (this *DefaultAgent) ServeUnsubscribeEvent(w http.ResponseWriter, r *http.Request, event *request.UnsubscribeEvent, rawXMLMsg []byte, timestamp int64, nonce string, random []byte)
func (*DefaultAgent) ServeVideoMsg ¶
func (*DefaultAgent) ServeVoiceMsg ¶
type InvalidRequestHandler ¶
type InvalidRequestHandler interface {
// 非法请求的处理方法, err 是错误信息
ServeInvalidRequest(w http.ResponseWriter, r *http.Request, err error)
}
var DefaultInvalidRequestHandler InvalidRequestHandler = InvalidRequestHandlerFunc(func(w http.ResponseWriter, r *http.Request, err error) {})
type InvalidRequestHandlerFunc ¶
type InvalidRequestHandlerFunc func(w http.ResponseWriter, r *http.Request, err error)
func (InvalidRequestHandlerFunc) ServeInvalidRequest ¶
func (fn InvalidRequestHandlerFunc) ServeInvalidRequest(w http.ResponseWriter, r *http.Request, err error)
type MultiAgentFrontend ¶
type MultiAgentFrontend struct {
// contains filtered or unexported fields
}
多个 Agent 的前端, 负责处理 http 请求, net/http.Handler 的实现
NOTE:
MultiAgentFrontend 可以处理多个公众号的消息(事件),但是要求在回调 URL 上加上一个查询
参数,一般为 agentkey(参考常量 URLQueryAgentKeyName),这个参数的值就是 MultiAgentFrontend
索引 Agent 的 key。
例如回调 URL 为 http://www.xxx.com/weixin?agentkey=1234567890,那么就可以在后端调用
MultiAgentFrontend.SetAgent("1234567890", agent)
来增加一个 Agent 来处理 agentkey=1234567890 的消息(事件)。
MultiAgentFrontend 并发安全,可以在运行中动态增加和删除 Agent。
func (*MultiAgentFrontend) DeleteAgent ¶
func (this *MultiAgentFrontend) DeleteAgent(agentkey string)
删除 agentkey 对应的 Agent
func (*MultiAgentFrontend) DeleteAllAgent ¶
func (this *MultiAgentFrontend) DeleteAllAgent()
删除所有的 Agent
func (*MultiAgentFrontend) ServeHTTP ¶
func (this *MultiAgentFrontend) ServeHTTP(w http.ResponseWriter, r *http.Request)
func (*MultiAgentFrontend) SetAgent ¶
func (this *MultiAgentFrontend) SetAgent(agentkey string, agent Agent)
添加(设置) agentkey-agent pair, 如果 agent == nil 则不做任何操作
func (*MultiAgentFrontend) SetInvalidRequestHandler ¶
func (this *MultiAgentFrontend) SetInvalidRequestHandler(handler InvalidRequestHandler)
设置 InvalidRequestHandler, 如果 handler == nil 则使用默认的 DefaultInvalidRequestHandler
Click to show internal directories.
Click to hide internal directories.