mp/

directory
v0.0.0-...-683899a Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2016 License: Apache-2.0

README

回调请求的一般处理逻辑(一个回调地址处理一个公众号的消息和事件)

package main

import (
	"log"
	"net/http"

	"github.com/chanxuehong/wechat.v2/mp/core"
	"github.com/chanxuehong/wechat.v2/mp/menu"
	"github.com/chanxuehong/wechat.v2/mp/message/callback/request"
	"github.com/chanxuehong/wechat.v2/mp/message/callback/response"
)

const (
	wxAppId     = "appid"
	wxAppSecret = "appsecret"

	wxOriId         = "oriid"
	wxToken         = "token"
	wxEncodedAESKey = "aeskey"
)

var (
	// 下面两个变量不一定非要作为全局变量, 根据自己的场景来选择.
	msgHandler core.Handler
	msgServer  *core.Server
)

func init() {
	mux := core.NewServeMux()
	mux.DefaultMsgHandleFunc(defaultMsgHandler)
	mux.DefaultEventHandleFunc(defaultEventHandler)
	mux.MsgHandleFunc(request.MsgTypeText, textMsgHandler)
	mux.EventHandleFunc(menu.EventTypeClick, menuClickEventHandler)

	msgHandler = mux
	msgServer = core.NewServer(wxOriId, wxAppId, wxToken, wxEncodedAESKey, msgHandler, nil)
}

func textMsgHandler(ctx *core.Context) {
	log.Printf("收到文本消息:\n%s\n", ctx.MsgPlaintext)

	msg := request.GetText(ctx.MixedMsg)
	resp := response.NewText(msg.FromUserName, msg.ToUserName, msg.CreateTime, msg.Content)
	//ctx.RawResponse(resp) // 明文回复
	ctx.AESResponse(resp, 0, "", nil) // aes密文回复
}

func defaultMsgHandler(ctx *core.Context) {
	log.Printf("收到消息:\n%s\n", ctx.MsgPlaintext)
	ctx.NoneResponse()
}

func menuClickEventHandler(ctx *core.Context) {
	log.Printf("收到菜单 click 事件:\n%s\n", ctx.MsgPlaintext)

	event := menu.GetClickEvent(ctx.MixedMsg)
	resp := response.NewText(event.FromUserName, event.ToUserName, event.CreateTime, "收到 click 类型的事件")
	//ctx.RawResponse(resp) // 明文回复
	ctx.AESResponse(resp, 0, "", nil) // aes密文回复
}

func defaultEventHandler(ctx *core.Context) {
	log.Printf("收到事件:\n%s\n", ctx.MsgPlaintext)
	ctx.NoneResponse()
}

func init() {
	http.HandleFunc("/wx_callback", wxCallbackHandler)
}

// wxCallbackHandler 是处理回调请求的 http handler.
//  1. 不同的 web 框架有不同的实现
//  2. 一般一个 handler 处理一个公众号的回调请求(当然也可以处理多个, 这里我只处理一个)
func wxCallbackHandler(w http.ResponseWriter, r *http.Request) {
	msgServer.ServeHTTP(w, r, nil)
}

func main() {
	log.Println(http.ListenAndServe(":80", nil))
}

公众号api调用的一般处理逻辑

package main

import (
	"fmt"

	"github.com/chanxuehong/wechat.v2/mp/base"
	"github.com/chanxuehong/wechat.v2/mp/core"
)

const (
	wxAppId     = "appid"
	wxAppSecret = "appsecret"

	wxOriId         = "oriid"
	wxToken         = "token"
	wxEncodedAESKey = "aeskey"
)

var (
	accessTokenServer core.AccessTokenServer = core.NewDefaultAccessTokenServer(wxAppId, wxAppSecret, nil)
	wechatClient      *core.Client           = core.NewClient(accessTokenServer, nil)
)

func main() {
	fmt.Println(base.GetCallbackIP(wechatClient))
}

Directories

Path Synopsis
账号管理.
账号管理.
基础模块
基础模块
微信连Wi-Fi
微信连Wi-Fi
微信卡券接口
微信卡券接口
datacube
see github.com/chanxuehong/wechat.v2/mp/datacube
see github.com/chanxuehong/wechat.v2/mp/datacube
微信公众平台(订阅号&服务号) SDK 的核心库
微信公众平台(订阅号&服务号) SDK 的核心库
数据统计接口.
数据统计接口.
card
卡券数据统计接口
卡券数据统计接口
dkf
多客服接口.
多客服接口.
account
客户账号管理
客户账号管理
record
客服聊天记录接口
客服聊天记录接口
session
多客服会话控制
多客服会话控制
微信JS-SDK.
微信JS-SDK.
永久素材管理.
永久素材管理.
临时素材管理.
临时素材管理.
自定义菜单接口.
自定义菜单接口.
message
callback/request
被动接收的基本消息(事件)的数据结构定义, 更多的消息(事件)定义在对应的业务模块内.
被动接收的基本消息(事件)的数据结构定义, 更多的消息(事件)定义在对应的业务模块内.
callback/response
被动回复的基本消息的数据结构定义, 更多的消息定义在对应的业务模块内.
被动回复的基本消息的数据结构定义, 更多的消息定义在对应的业务模块内.
custom
客服消息.
客服消息.
mass
群发消息.
群发消息.
mass/mass2all
群发消息给所有用户.
群发消息给所有用户.
mass/mass2group
群发消息给特定分组用户.
群发消息给特定分组用户.
mass/mass2users
根据OpenID列表群发.
根据OpenID列表群发.
mass/preview
预览消息.
预览消息.
template
模板消息接口.
模板消息接口.
微信网页授权.
微信网页授权.
微信门店接口.
微信门店接口.
二维码接口.
二维码接口.
摇一摇周边
摇一摇周边
用户管理.
用户管理.
group
用户分组管理.
用户分组管理.

Jump to

Keyboard shortcuts

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