beego.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/astaxie/beego"
  5. "github.com/astaxie/beego/context"
  6. "github.com/yaotian/gowechat"
  7. "github.com/yaotian/gowechat/mp/message"
  8. "github.com/yaotian/gowechat/wxcontext"
  9. )
  10. func hello(ctx *context.Context) {
  11. //配置微信参数
  12. config := wxcontext.Config{
  13. AppID: "your app id",
  14. AppSecret: "your app secret",
  15. Token: "your token",
  16. EncodingAESKey: "your encoding aes key",
  17. }
  18. wc := gowechat.NewWechat(config)
  19. mp, err := wc.MpMgr()
  20. if err != nil {
  21. return
  22. }
  23. // 传入request和responseWriter
  24. msgHandler := mp.GetMsgHandler(ctx.Request, ctx.ResponseWriter)
  25. //设置接收消息的处理方法
  26. msgHandler.SetHandleMessageFunc(func(msg message.MixMessage) *message.Reply {
  27. //回复消息:演示回复用户发送的消息
  28. text := message.NewText(msg.Content)
  29. return &message.Reply{message.MsgTypeText, text}
  30. })
  31. //处理消息接收以及回复
  32. err = msgHandler.Handle()
  33. if err != nil {
  34. fmt.Println(err)
  35. return
  36. }
  37. }
  38. func main() {
  39. beego.Any("/", hello)
  40. beego.Run(":8001")
  41. }