|
|
@@ -0,0 +1,48 @@
|
|
|
+package main
|
|
|
+
|
|
|
+import (
|
|
|
+ "fmt"
|
|
|
+ "net/http"
|
|
|
+
|
|
|
+ "github.com/yaotian/gowechat"
|
|
|
+ "github.com/yaotian/gowechat/mp/message"
|
|
|
+)
|
|
|
+
|
|
|
+func hello(rw http.ResponseWriter, req *http.Request) {
|
|
|
+
|
|
|
+ //配置微信参数
|
|
|
+ config := &gowechat.Config{
|
|
|
+ AppID: "your app id",
|
|
|
+ AppSecret: "your app secret",
|
|
|
+ Token: "your token",
|
|
|
+ EncodingAESKey: "your encoding aes key",
|
|
|
+ }
|
|
|
+ wc := gowechat.NewWechat(config)
|
|
|
+
|
|
|
+ // 传入request和responseWriter
|
|
|
+ server := wc.GetServer(req, rw)
|
|
|
+ //设置接收消息的处理方法
|
|
|
+ server.SetMessageHandler(func(msg message.MixMessage) *message.Reply {
|
|
|
+
|
|
|
+ //回复消息:演示回复用户发送的消息
|
|
|
+ text := message.NewText(msg.Content)
|
|
|
+ return &message.Reply{message.MsgTypeText, text}
|
|
|
+ })
|
|
|
+
|
|
|
+ //处理消息接收以及回复
|
|
|
+ err := server.Serve()
|
|
|
+ if err != nil {
|
|
|
+ fmt.Println(err)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ //发送回复的消息
|
|
|
+ server.Send()
|
|
|
+}
|
|
|
+
|
|
|
+func main() {
|
|
|
+ http.HandleFunc("/", hello)
|
|
|
+ err := http.ListenAndServe(":8001", nil)
|
|
|
+ if err != nil {
|
|
|
+ fmt.Printf("start server error , err=%v", err)
|
|
|
+ }
|
|
|
+}
|