gowechat

package module
v0.0.0-...-385cc76 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2018 License: MIT Imports: 14 Imported by: 0

README

gowechat

一款由Go语言开发的微信公众号后台框架,通过本框架可快速搭建起微信公众号后台服务器,支持订阅号和服务号。 由于公众号后台服务在用户交互时大部分情景需要交互响应,此处涉及的模板推荐参考接下来会开源的开发样例。

功能

功能 描述 完成状态
TOKEN定时刷新 定时刷新微信服务器Token,默认刷新周期3600秒。
自定义菜单 支持自定义菜单设置,启动刷新调整
接收普通消息 接收并保存用户返回的普通消息(文本、图片、语音、视频、小视频、地理位置)
接收事件推送 接收并保存用户与公众号交互产生的事件
被动消息回复 支持被动消息回复功能
客服消息 支持外部客服消息接入
安全模式 支持安全模式的加密解密功能
模板消息 支持模板消息发送
安全模式 支持安全模式的加密解密功能

使用方法

获取gowechat库

go get github.com/dennismao/gowechat

程序调用

import(
	"github.com/dennismao/wechat"
)

使用示例

package main

import (
	"fmt"
	_ "teaer/routers"

	"github.com/astaxie/beego"
	"github.com/dennismao/gowechat"
)

const (
	Appid     = ""
	Appsecret = ""
	Token     = ""
)

func main() {

	//	Substantialize server
	myWechat, err := gowechat.New("", "", "")
	if err != nil {
		fmt.Println(err)
		return
	}

	//	refresh token
	go myWechat.Token_Refresh()

	//	Initialization
	//	Create custom menu
	err = myWechat.CustomMenuCreate([]gowechat.CustomButton{
		wechat.CustomButton{
			Name: "主菜单",
			SubButton: []gowechat.Button{
				wechat.Button{
					Type: "views",
					Name: "百度搜索",
					Url:  "www.baidu.com",
				},
				gowechat.Button{
					Type: "click",
					Name: "点击事件",
					Key:  "V1000_button1",
				},
			},
		},
	})
	if err != nil {
		fmt.Println(err)
	}

	//	Run the http server
	beego.Run()
}

License

gowechat source code is licensed under the MIT Licence

Documentation

Overview

Token管理

message encryption and decryption

自定义菜单

客服信息

模板消息

Index

Constants

This section is empty.

Variables

View Source
var GAccessToken string

全局token变量

Functions

func CustomMenuQuery

func CustomMenuQuery(accesstoken string) error

查询当前自定义菜单 @accesstoken: 传入当前有效的token

func Menu_Delete(accesstoken string) error

删除当前自定义菜单 @accesstoken: 传入当前有效的token

func ServerToken

func ServerToken() (string, error)

return the user token

func Service_Send_G

func Service_Send_G(accesstoken, openid, content string) error

客服消息:信息发送 @openid:发送对象的微信id @content:需要发送的文字

func SignatureVerify

func SignatureVerify(timestamp, nonce, remoteSignature, echostr string) string
	Verify signature
	@token:	user token
	@timestamp: timestamp of verification request
	@nonce: nonce of verification request
	@echostr: the echo should be responded when verification passes
 @return:
 response message returned to wechat server directly

func TplMessage_Send_G

func TplMessage_Send_G(accesstoken, openid, time, content string) error

/////////////// 自定义模块数据结构 ////////////////// 模板消息:信息发送 @openid:发送对象的微信id @time: 发送时间 @content:需要发送的文字

Types

type AccessTokenErrorResponse

type AccessTokenErrorResponse struct {
	Errcode float64
	Errmsg  string
}

微信服务器返回错误信息的格式

type AccessTokenResponse

type AccessTokenResponse struct {
	AccessToken string  `json:"access_token"`
	ExpiresIn   float64 `json:"expires_in"`
}

微信服务器返回token的格式

type Button

type Button struct {
	Type string `json:"type;omitempty"`
	Name string `json:"name;omitempty"`
	Key  string `json:"key;omitempty"`
	Url  string `json:"url;omitempty"`
}

button basic structure with compat on click mode and views mode

type Content_Msg_Clear

type Content_Msg_Clear struct {
	XMLName      xml.Name `xml:"xml"`
	ToUserName   string   `xml:"ToUserName"`
	FromUserName string   `xml:"FromUserName"`
	CreateTime   string   `xml:"CreateTime"`
	MsgType      string   `xml:"MsgType"`
	Content      string   `xml:"Content"`
	MsgId        string   `xml:"MsgId"`
}

type Content_Msg_Safe

type Content_Msg_Safe struct {
	ToUserName string `xml:"ToUserName"` //消息发送者微信openid
	Encrypt    string `xml:"Encrypt"`    //加密内容
}

Message pattern(safe mode)

type CustomButton

type CustomButton struct {
	Type      string   `json:"type;omitempty"`
	Name      string   `json:"name;omitempty"`
	Key       string   `json:"key;omitempty"`
	Url       string   `json:"url;omitempty"`
	SubButton []Button `json:"sub_button;omitempty"`
}

button menu @name

type CustomMenu

type CustomMenu struct {
	Url     string
	Content []CustomButton
}

custon menu requset structure

type ReqWechatTplData_Default

type ReqWechatTplData_Default struct {
	Value string `json:"value"` //数据值
	Color string `json:"color"` //颜色值 #173177 (蓝色)
}

模板数据-官方默认

type ReqWechatTplMiniprogramMsg

type ReqWechatTplMiniprogramMsg struct {
	AppId    string `json:"appid"`    //微信小程序id
	PagePath string `json:"pagepath"` //所需跳转到小程序的具体页面路径,支持带参数,(示例index?foo=bar)
}

模板消息-微信小程序信息

type ReqWechatTplMsg

type ReqWechatTplMsg struct {
	Touser      string                     `json:"touser"`      //接收人微信OpenId
	TempateId   string                     `json:"template_id"` //模板ID  在微信公众平台后台生成
	Url         string                     `json:"url"`         //消息内的跳转页面
	Miniprogram ReqWechatTplMiniprogramMsg `json:"-"`           //微信小程序信息 当前未使用
	Data        interface{}                `json:"data"`        //需要传入模板的数据
}

模板消息发送请求格式

type ReqWechatTpl_Alarm

type ReqWechatTpl_Alarm struct {
	First    ReqWechatTplData_Default `json:"first"`       //首标题
	KeyNote1 ReqWechatTplData_Default `json:"performance"` //数据块1
	KeyNote2 ReqWechatTplData_Default `json:"time"`        //数据块2
	Remark   ReqWechatTplData_Default `json:"remark"`      //备注 可用于拓展
}

模板-告警2.2.1

type ReqWechatTpl_Default

type ReqWechatTpl_Default struct {
	First    ReqWechatTplData_Default `json:"first"`    //首标题
	KeyNote1 ReqWechatTplData_Default `json:"keynote1"` //数据块1
	KeyNote2 ReqWechatTplData_Default `json:"keynote2"` //数据块2
	KeyNote3 ReqWechatTplData_Default `json:"keynote3"` //数据块3
	Remark   ReqWechatTplData_Default `json:"remark"`   //备注 可用于拓展
}

模板-官方默认

type RespWechatTplMsg

type RespWechatTplMsg struct {
	Errcode int    `json:"errcode"`
	Errmsg  string `json:"errmsg"`
}

模板消息响应格式

type Server

type Server struct {
	// contains filtered or unexported fields
}

wechat server basic structure

func New

func New(appid, appsecret, token string) (*Server, error)

substantialize a wechat server

func (*Server) CustomMenuCreate

func (we *Server) CustomMenuCreate(menuContent []CustomButton) error

create cunstom menu

func (*Server) Service_Add

func (we *Server) Service_Add() error

客服消息:客服账号添加 @accesstoken:当前有效的token 未设置传参,可直接修改kf_account、nickname和password。注意kf_account格式是 【自定义客服名@微信公众号账号】

func (*Server) Service_Send

func (we *Server) Service_Send(openid, content string) error

客服消息:信息发送 @openid:发送对象的微信id @content:需要发送的文字

func (*Server) Token_Refresh

func (we *Server) Token_Refresh()

刷新Token (刷新时间:1小时) @appid:微信后台的id号 @appsecret:微信后台开发的secret号

Directories

Path Synopsis
http utils
http utils

Jump to

Keyboard shortcuts

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