forked from esap/wechat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
62 lines (54 loc) · 1.78 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package wechat_test
import (
"net/http"
"github.com/esap/wechat" // 微信SDK包
"github.com/labstack/echo"
)
func Example() {
wechat.Debug = true
wechat.Set("yourToken", "yourAppID", "yourSecret", "yourEncodingAesKey")
http.HandleFunc("/", HttpHandler)
http.ListenAndServe(":9090", nil)
}
func Example_echo() {
e := echo.New()
e.Any("/", EchoHandler)
e.Start(":9090")
}
func HttpHandler(w http.ResponseWriter, r *http.Request) {
ctx := wechat.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() // 回复模板消息
}
}
func EchoHandler(c echo.Context) error {
ctx := wechat.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
}