All Projects → rosbit → go-wx-api

rosbit / go-wx-api

Licence: MIT license
微信公众号开发API封装

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to go-wx-api

dynamic-engine
Monitor events in the background on Android.
Stars: ✭ 24 (+41.18%)
Mutual labels:  service
wulaphp
一个有点复杂的PHP框架!
Stars: ✭ 26 (+52.94%)
Mutual labels:  service
ng2-STOMP-Over-WebSocket
STOMP Over WebSocket service for angular2
Stars: ✭ 35 (+105.88%)
Mutual labels:  service
xtuff
A CLI to generate stuff easy! (components, services, etc...)
Stars: ✭ 28 (+64.71%)
Mutual labels:  service
UnSupportedServices.bundle
UnSupported Services.bundle for Plex Media Server (https://plex.tv)
Stars: ✭ 18 (+5.88%)
Mutual labels:  service
Nano.Library
Take full advantage of Nano Library, and rapidly implement enriched .Net 6.0 micro-services.
Stars: ✭ 39 (+129.41%)
Mutual labels:  service
json-resume-service
JSON resumes as a Service - generate Resumes from the resume.json schema
Stars: ✭ 41 (+141.18%)
Mutual labels:  service
elixir-ms
an elixir microservice base/skeleton 💀
Stars: ✭ 39 (+129.41%)
Mutual labels:  service
ApiCenter
A repository for all your API specifications
Stars: ✭ 26 (+52.94%)
Mutual labels:  service
login-server
Login and connect accounts with multiple identity providers
Stars: ✭ 28 (+64.71%)
Mutual labels:  service
LockerScreen
Android lock screen,slide to unlock ! 安卓锁屏,上滑解锁,效果酷炫,值得拥有!
Stars: ✭ 81 (+376.47%)
Mutual labels:  service
MusicPlayer
Android music player example.
Stars: ✭ 20 (+17.65%)
Mutual labels:  service
muse-as-service
REST API for sentence tokenization and embedding using Multilingual Universal Sentence Encoder.
Stars: ✭ 45 (+164.71%)
Mutual labels:  service
origami-image-service
Optimises and resizes images
Stars: ✭ 15 (-11.76%)
Mutual labels:  service
gologger
A concurrent, fast queue/service worker based filesystem logging system perfect for servers with concurrent connections
Stars: ✭ 16 (-5.88%)
Mutual labels:  service
suchtube
🔍 📼 YouTube search as a service, with Slack integration and CLI
Stars: ✭ 25 (+47.06%)
Mutual labels:  service
HiveMind
HiveMind is a project management and ERP application for services organizations. It features project/task management, request tracking, time tracking, expenses, invoices/payments, general ledger, and content management (wiki). HiveMind is based on Moqui Framework, Mantle Business Artifacts, and Simple Screens.
Stars: ✭ 40 (+135.29%)
Mutual labels:  service
go-windows-svc
GO shell for a Windows service
Stars: ✭ 57 (+235.29%)
Mutual labels:  service
TgTwitterStreamer
Continous Integration from Twitter to Telegram.
Stars: ✭ 55 (+223.53%)
Mutual labels:  service
impress-cli
Impress Application Server Command line interface
Stars: ✭ 25 (+47.06%)
Mutual labels:  service

微信公众号开发SDK

go-wx-api是对微信公众号API的封装,可以当作SDK使用,主要特点:

  • 对常用消息、事件的接收和回复做了封装,已经无需了解相关的公众号开发文档;见使用方法1;
  • 提供了缺省的消息、事件处理方法,可以根据实际需求覆盖相关实现;见使用方法2;
  • 使用go-wx-api可以同时支持多个公众号;见使用方法3;
  • 菜单点击后允许进入自己的页面,go-wx-api提供了统一的入口进入菜单处理,获取用户openId, 根据state参数进入具体业务处理。可以通过提供菜单跳转处理器实现具体业务;见使用方法1;
  • go-wx-gateway是使用go-wx-api实现的微信公众号网关服务, 通过go-wx-gateway,就可以把微信公众号服务的开发转化为普通web服务开发。

编译例子

  1. 该函数包已经使用go modules发布,需要golang 1.11.x及以上版本
  2. 请参考go-wx-apps,那里包含了例程和工具程序

使用方法1: (实现消息处理器、菜单跳转处理器的例子)

go-wx-api已经对公众号常用的消息(文本框架输入、发语音等)、事件(用户关注、点击菜单等)做了提取和封装处理。缺省处理对消息、事件做了简单的应答处理,缺省处理除了能给公众号管理后台做开发者设置功能外,没有实际意义。具体业务可以根据需要对go-wx-api的消息处理器接口进行实现:

  1. 消息处理器的定义和实现

    import (
        "github.com/rosbit/go-wx-api/v2/msg"
        "fmt"
    )
    
    // 消息处理器定义
    type YourMsgHandler struct {
        wxmsg.WxMsgHandlerAdapter  // 包含了所有消息、事件的缺省实现
    }
    
    // 接口定义见 wxmsg.WxMsgHandler,根据需要选择实现其中的方法
    
    // 文本消息处理
    func (h *YourMsgHandler) HandleTextMsg(textMsg *wxmsg.TextMsg) wxmsg.ReplyMsg {
        return NewReplyTextMsg(textMsg.FromUserName, textMsg.ToUserName, fmt.Sprintf("收到了消息:%s", textMsg.Content))
    }
    
    // 用户关注公众号处理
    func (h *YourMsgHandler) HandleSubscribeEvent(subscribeEvent *wxmsg.SubscribeEvent) wxmsg.ReplyMsg {
        return wxmsg.NewReplyTextMsg(subscribeEvent.FromUserName, subscribeEvent.ToUserName, "welcome")
    }
  2. 注册消息处理器

    • 单一公众号注册方法见方法2
    • 多公众号注册方法见方法3

使用方法2: (单一公众号服务)

以下是一个简单的例子,用于说明使用go-wx-api的主要执行步骤。更详细的例子参考go-wx-apps

package main

import (
	"github.com/rosbit/go-wx-api/v2/conf"
	"github.com/rosbit/go-wx-api/v2/msg"
	"github.com/rosbit/go-wx-api/v2"
	"net/http"
	"fmt"
)

const (
	token     = "微信公众号的token"
	appId     = "微信公众号appId"
	appSecret = "微信公众号的secret"
	aesKey    = "" //安全模式 使用的AESKey,如果是 明文传输,该串为空
	
	listenPort = 7070   // 服务侦听的端口号,请根据微信公众号管理端的服务器配置正确设置
	service    = "/wx"  // 微信公众号管理端服务器配置中URL的路径部分

	workerNum = 3 // 处理请求的并发数
	name = "test" // 服务的名字
)

func main() {
	// 步骤1. 初始化accessToken缓存路径
	wxapi.InitWx("/tmp")

	// 步骤2. 设置配置参数
	if err := wxapi.SetWxParams(name, token, appId, appSecret, aesKey); err != nil {
		fmt.Printf("failed to set params: %v\n", err)
		return
	}

	// 步骤3: 设置签名验证的中间件。由于net/http不支持中间件,省去该步骤
	// signatureChecker := wxapi.NewWxSignatureChecker(token, 0, []string{service})
	// <middleWareContainer>.Use(signatureChecker)

	// 步骤4. 设置http路由,启动http服务
	// 用于公众号配置
	http.HandleFunc(service, wxapi.CreateEcho(token))

	// 用于实际执行公众号请求,和wxapi.CreateEcho只能使用一个。
	// 可以使用支持高级路由功能的web框架同时设置
	http.HandleFunc(service, wxapi.CreateMsgHandler(name, workerNum, wxmsg.MsgHandler))  // 使用缺省的消息处理器
	http.HandleFunc(service, wxapi.CreateMsgHandler(name, workerNum, &YourMsgHandler{})) // 使用自定义的消息处理器
	                                         
	http.ListenAndServe(fmt.Sprintf(":%d", listenPort), nil)
}

使用方法3: (多个公众号服务)

以下代码仅仅为同时启用公众号的示例:

package main

import (
	"github.com/rosbit/go-wx-api/v2/conf"
	"github.com/rosbit/go-wx-api/v2"
	"net/http"
	"fmt"
)

type WxConf struct {
	name string
	token string
	appId string
	appSecret string
	aesKey string
	workerNum int
	service string
	rd string
	redirectUrl string
}

var (
	listenPort = 7070   // 服务侦听的端口号,请根据微信公众号管理端的服务器配置正确设置
	wxServices = []WxConf{
		WxConf{
			name: "wx1",
			token: "微信公众号1的token",
			appId: "微信公众号1的appId",
			appSecret: "微信公众号的1secret",
			aesKey: "",      // 安全模式 使用的AESKey,如果是 明文传输,该串为空
			workerNum: 3,    // 处理请求的并发数
			service: "/wx1", // 微信公众号管理端服务器配置中URL的路径部分
			rd: "/rd1", // 网页授权接受地址
			redirectUrl: "http://localhost:port/redirect", // 接受网页授权转发的其它服务
		},
		WxConf{
			name: "wx2",
			token: "微信公众号2的token",
			appId: "微信公众号2的appId",
			appSecret: "微信公众号2的secret",
			aesKey: "",      // 安全模式 使用的AESKey,如果是 明文传输,该串为空
			workerNum: 3,    // 处理请求的并发数
			service: "/wx2", // 微信公众号管理端服务器配置中URL的路径部分
			rd: "/rd2", // 网页授权接受地址
			redirectUrl: "http://localhost:port/redirect", // 接受网页授权转发的其它服务
		},
		// 其它服务号
	}
)

func main() {
	// 步骤1. 初始化accessToken缓存路径
	wxapi.InitWx("/tmp")

	// 对于每一个公众号执行
	for _, conf := range wxServices {
		// 步骤2. 设置配置参数
		if err := wxapi.SetWxParams(conf.name, conf.token, conf.appId, conf.appSecret, conf.aesKey); err != nil {
			fmt.Printf("failed to set params: %v\n", err)
			return
		}

		// 步骤2.5 设置签名验证的中间件。由于net/http不支持中间件,省去该步骤
		// signatureChecker := wxapi.NewWxSignatureChecker(conf.token, 0, []string{conf.service})
		// <middleWareContainer>.Use(signatureChecker)

		// 步骤3. 设置http路由,启动http服务
		http.HandleFunc(conf.service, wxapi.CreateEcho(conf.token))     // 用于配置

		// 用于实际执行公众号请求,和wxapi.CreateEcho只能使用一个。
		// 可以使用支持高级路由功能的web框架同时设置。使用POST路由
		http.HandleFunc(conf.service, wxapi.CreateMsgHandler(conf.name, conf.workerNum, wxmsg.MsgHandler))  // 使用缺省的消息处理器
		http.HandleFunc(conf.service, wxapi.CreateMsgHandler(conf.name, conf.workerNum, &YourMsgHandler{})) // 使用自定义的消息处理器

		// 菜单跳转全权交给另外一个URL处理,用GET路由
		// redirectURL接收POST请求,POST body是一个JSON: {"appId":"xxx", "openId", "xxx", "state": "xxx", "userInfo": {}}
		// 它可以随意处理HTTP请求、输出HTTP响应,响应结果直接返回公众号浏览器
		http.HandleFunc(conf.rd, wxapi.CreateOAuth2Redirector(conf.name, conf.workerNum, conf.redirectUrl))
	}

	http.ListenAndServe(fmt.Sprintf(":%d", listenPort), nil)
}

其它

  1. 该函数包可以处理文本消息、用户关注/取消关注事件、菜单点击事件
  2. 其它消息、事件可以根据需要扩充
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].