用Golang寫了個簡單易用的微信SDK

WeChat SDK for Go

使用Golang開發的微信SDK,簡單、易用。git

項目地址:https://github.com/silenceper/wechatgithub

文檔地址:DOCS微信

快速開始

如下是一個處理消息接收以及回覆的例子:框架

//配置微信參數
config := &wechat.Config{
    AppID:          "xxxx",
    AppSecret:      "xxxx",
    Token:          "xxxx",
    EncodingAESKey: "xxxx",
    Cache:          memCache
}
wc := wechat.NewWechat(config)

// 傳入request和responseWriter
server := wc.GetServer(request, responseWriter)
server.SetMessageHandler(func(msg message.MixMessage) *message.Reply {

    //回覆消息:演示回覆用戶發送的消息
    text := message.NewText(msg.Content)
    return &message.Reply{message.MsgText, text}
})

server.Serve()
server.Send()

完整代碼:examples/http/http.go測試

和主流框架配合使用

主要是request和responseWriter在不一樣框架中獲取方式可能不同:code

  • Beego: ./examples/beego/beego.go視頻

  • Gin Framework: ./examples/gin/gin.goserver

基本配置

memcache := cache.NewMemcache("127.0.0.1:11211")

wcConfig := &wechat.Config{
    AppID:          cfg.AppID,
    AppSecret:      cfg.AppSecret,
    Token:          cfg.Token,
    EncodingAESKey: cfg.EncodingAESKey,//消息加解密時用到
    Cache:          memcache,
}

Cache 設置token

Cache主要用來保存全局access_token以及js-sdk中的ticket:
默認採用memcache存儲。固然也能夠直接實現cache/cache.go中的接口接口

基本API使用

  • 消息管理

    • 接收普通消息

    • 接收事件推送

    • 被動回覆消息

      • 回覆文本消息

      • 回覆圖片消息

      • 回覆視頻消息

      • 回覆音樂消息

      • 回覆圖文消息

  • 自定義菜單

    • 自定義菜單建立接口

    • 自定義菜單查詢接口

    • 自定義菜單刪除接口

    • 自定義菜單事件推送

    • 個性化菜單接口

      • 添加個性化菜單

      • 刪除個性化菜單

      • 測試個性化菜單匹配結果

    • 獲取公衆號菜單配置

  • 微信網頁開發

    • Oauth2 受權

      • 發起受權

      • 經過code換取access_token

      • 拉取用戶信息

      • 刷新access_token

      • 檢驗access_token是否有效

    • 獲取js-sdk配置

  • 素材管理

更多API使用請參考文檔:
https://github.com/silenceper...

相關文章
相關標籤/搜索