GithubHelp home page GithubHelp logo

daheige / wechat-1 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from esap/wechat

0.0 2.0 0.0 106 KB

微信SDK的golang实现,短小精悍,同时兼容【企业号/服务号/订阅号/小程序】

License: MIT License

Go 100.00%

wechat-1's Introduction

WeChat SDK

Build Status Go Report Card GoDoc

微信SDK的golang实现,短小精悍,同时兼容【企业号/服务号/订阅号/小程序】

快速开始

5行代码,链式消息,快速开启微信API示例:

package main

import (
	"net/http"
	"github.com/esap/wechat" // 微信SDK包
)

func main() {
	wechat.Debug = true
	wechat.Set("yourToken", "yourAppID", "yourSecret", "yourAesKey")
	http.HandleFunc("/", WxHandler)
	http.ListenAndServe(":9090", nil)
}

func WxHandler(w http.ResponseWriter, r *http.Request) {
	wechat.VerifyURL(w, r).NewText("这是客服消息").Send().NewText("这是被动回复").Reply()
}

配置方式

  • 使用默认实例
	// 不带aesKey则为明文模式
	wechat.Set("token", "appId", "secret")

	// 带aesKey则为密文模式
	wechat.Set("token", "appId", "secret", "aesKey")

	// 企业号自动配置为密文模式
	wechat.SetEnt("token", "appId", "secret", "aesKey", "agentId")
  • 创建其他实例,密文模式
	// 创建公众号实例(服务号/订阅号/小程序)
	app := wechat.New("token", "appId", "secret", "aesKey")

	// 创建企业号实例
	app := wechat.New("token", "appId", "secret", "aesKey", "agentId")

	// 实例化后其他业务操作
	ctx := app.VerifyURL(w, r)
	ctx.NewText("这是客服消息").Send().NewText("这是被动回复").Reply()

消息管理

  • 通常将wechat.VerifyURL(http.ResponseWriter, *http.Request)嵌入http handler

该函数返回*wechat.Context基本对象,其中的Msg为用户消息:

// 混合用户消息,业务判断的主体
type WxMsg struct {
	XMLName      xml.Name `xml:"xml"`
	ToUserName   string
	FromUserName string
	CreateTime   int64
	MsgId        int64
	MsgType      string
	Content      string  // text
	AgentID      int     // corp
	PicUrl       string  // image
	MediaId      string  // image/voice/video/shortvideo
	Format       string  // voice
	Recognition  string  // voice
	ThumbMediaId string  // video
	LocationX    float32 `xml:"Latitude"`  // location
	LocationY    float32 `xml:"Longitude"` // location
	Precision    float32 // LOCATION
	Scale        int     // location
	Label        string  // location
	Title        string  // link
	Description  string  // link
	Url          string  // link
	Event        string  // event
	EventKey     string  // event
	SessionFrom  string  // event|user_enter_tempsession
	Ticket       string

	ScanCodeInfo struct {
		ScanType   string
		ScanResult string
	}

	AgentType string
	ItemCount int
	PackageId string

	Item []struct {
		FromUserName string
		CreateTime   int64
		MsgType      string
		Event        string // event
		Name         string
		Owner        string
		AddUserList  string
		DelUserList  string
		ChatId       string
		MsgId        int64

		ChatInfo struct {
			ChatId   string
			Name     string
			Owner    string
			UserList string
		}

		Content  string // text
		Receiver struct {
			Type string
			Id   string
		}

		FileName string // file
		PicUrl   string // image/link
		MediaId  string // image/voice/video/shortvideo

		Location_X float32 // location
		Location_Y float32 // location
		Scale      int     // location
		Label      string  // location

		Title       string // link
		Description string // link
		Url         string // link
	}
}
  • 如果使用其他web框架,例如echo/gin/beego等,则把VerifyURL()放入controller或handler
// echo示例 企业号回调接口
func wxApiPost(c echo.Context) error {
	ctx := wechat.VerifyURL(c.Response().Writer, c.Request())
	// TODO: 这里是其他业务操作
	return nil
}

回复消息

回复消息有两种方式:

  • 被动回复,采用XML格式编码返回(Reply);

  • 客服消息,采用json格式编码返回(Send);

  • 两种方式都可先调用*wechat.Context对象的New方法创建消息,然后调用Reply()或Send()。

  • 支持链式调用,但Reply()只有第一次有效。

	ctx.NewText("正在查询中...").Reply()
	ctx.NewText("客服消息1").Send().NewText("客服消息2").Send()
  • 被动回复可直接调用ReplySuccess(),表示已收到,然后调用客服消息。

文本消息

	ctx.NewText("content")

图片/语言/文件消息

	// mediaID 可通过素材管理-上上传多媒体文件获得
	ctx.NewImage("mediaID")
	ctx.NewVoice("mediaID")
	
	// 仅企业号支持
	ctx.NewFile("mediaID")

视频消息

	ctx.NewVideo("mediaID", "title", "description")

音乐消息

	ctx.NewMusic("thumbMediaID","title", "description", "musicURL", "hqMusicURL")

图文消息

	// 先创建三个文章
	art1 := wechat.NewArticle("拥抱AI,享受工作",
		"来自村长的ESAP系统最新技术分享",
		"http://ylin.wang/img/esap18-1.png",
		"http://ylin.wang/2017/07/13/esap18/")
	art2 := wechat.NewArticle("用企业微信代替pda实现扫描入库",
		"来自村长的ESAP系统最新技术分享",
		"http://ylin.wang/img/esap17-2.png",
		"http://ylin.wang/2017/06/23/esap17/")
	art3 := wechat.NewArticle("大道至简的哲学",
		"来自村长的工作日志",
		"http://ylin.wang/img/golang.jpg",
		"http://ylin.wang/2017/01/29/log7/")
	// 打包成新闻
	ctx.NewNews(art1, art2, art3)

License

MIT

wechat-1's People

Contributors

esap avatar

Watchers

James Cloos avatar heige avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.