You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 satır
1.5KB

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. )
  6. type commandFunction func(*openIDSessionData, InWechatMsg) bool
  7. var commandMap = map[string]commandFunction{
  8. "版本信息": cmdVersion,
  9. "version": cmdVersion,
  10. //"所有命令": allCommand, //include it will cause initialization loop
  11. "echo": cmdEcho,
  12. "回音": cmdEcho,
  13. }
  14. func (ss *openIDSessionData) serveCommand(in InWechatMsg) (processed bool) {
  15. if in.header.MsgType == "text" {
  16. cmd := in.body.(TextMsg).Content
  17. if f, hasFunction := commandMap[cmd]; hasFunction {
  18. return f(ss, in)
  19. }
  20. if cmd == "所有命令" || cmd == "all command" {
  21. return allCommand(ss, in)
  22. }
  23. }
  24. processed = false
  25. return
  26. }
  27. func allCommand(ss *openIDSessionData, in InWechatMsg) (processed bool) {
  28. processed = true
  29. msg := "命令如下:\n"
  30. count := 0
  31. for k := range commandMap {
  32. count++
  33. msg = msg + fmt.Sprintf("%0d : %s \n", count, k)
  34. }
  35. str, _ := BuildTextMsg(in.header.FromUserName, msg)
  36. in.immediateResponse(str)
  37. return
  38. }
  39. func cmdVersion(ss *openIDSessionData, in InWechatMsg) (processed bool) {
  40. processed = true
  41. str, _ := BuildTextMsg(in.header.FromUserName, "这是测试版本"+time.Now().Format("2006/01/02 03:04:05"))
  42. in.immediateResponse(str)
  43. return
  44. }
  45. func cmdEcho(ss *openIDSessionData, in InWechatMsg) (processed bool) {
  46. in.immediateResponse("请输入不同类型的微信信息,比如文字,图片,视频,地址,链接,我们将原样回应您")
  47. procEcho.init(ss)
  48. procEcho.start(ss, in)
  49. return
  50. }