1 Star 0 Fork 0

陆海空 / wechat

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
example_test.go 2.02 KB
一键复制 编辑 原始数据 按行查看 历史
package wechat_test
import (
"net/http"
"github.com/esap/wechat" // 微信SDK包
// "github.com/labstack/echo"
)
func Example() {
wechat.Debug = true
cfg := &wechat.WxConfig{
Token: "yourToken",
AppId: "yourAppID",
Secret: "yourSecret",
EncodingAESKey: "yourEncodingAesKey",
}
app := wechat.New(cfg)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
ctx := app.VerifyURL(w, r)
// 根据消息类型主动回复
switch ctx.Msg.MsgType {
case wechat.TypeText:
ctx.NewText(ctx.Msg.Content).Reply() // 回复文字
case wechat.TypeImage:
ctx.NewImage(ctx.Msg.MediaId).Reply() // 回复图片
case wechat.TypeVoice:
ctx.NewVoice(ctx.Msg.MediaId).Reply() // 回复语音
case wechat.TypeVideo:
ctx.NewVideo(ctx.Msg.MediaId, "video title", "video description").Reply() //回复视频
case wechat.TypeFile:
ctx.NewFile(ctx.Msg.MediaId).Reply() // 回复文件,仅企业微信可用
default:
ctx.NewText("其他消息类型" + ctx.Msg.MsgType).Reply() // 回复模板消息
}
})
http.ListenAndServe(":9090", nil)
}
// func Example_echo() {
// app := wechat.New("yourToken", "yourAppID", "yourSecret", "yourEncodingAesKey")
// e := echo.New()
// e.Any("/", func(c echo.Context) error {
// ctx := app.VerifyURL(c.Response().Writer, c.Request())
// // 根据消息类型主动回复
// switch ctx.Msg.MsgType {
// case wechat.TypeText:
// ctx.NewText(ctx.Msg.Content).Reply() // 回复文字
// case wechat.TypeImage:
// ctx.NewImage(ctx.Msg.MediaId).Reply() // 回复图片
// case wechat.TypeVoice:
// ctx.NewVoice(ctx.Msg.MediaId).Reply() // 回复语音
// case wechat.TypeVideo:
// ctx.NewVideo(ctx.Msg.MediaId, "video title", "video description").Reply() //回复视频
// case wechat.TypeFile:
// ctx.NewFile(ctx.Msg.MediaId).Reply() // 回复文件,仅企业微信可用
// default:
// ctx.NewText("其他消息类型" + ctx.Msg.MsgType).Reply() // 回复模板消息
// }
// return nil
// })
// e.Start(":9090")
// }
Go
1
https://gitee.com/hugh2632/wechat.git
git@gitee.com:hugh2632/wechat.git
hugh2632
wechat
wechat
master

搜索帮助