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.

106 line
2.3KB

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "strings"
  6. "time"
  7. )
  8. type commandFunction func(*openIDSessionData, *InWechatMsg) bool
  9. var commandMap = map[string]commandFunction{
  10. "版本信息": cmdVersion,
  11. "version": cmdVersion,
  12. //"所有命令": allCommand, //include it will cause initialization loop
  13. "回音": cmdEcho,
  14. }
  15. func (ss *openIDSessionData) serveCommand(in *InWechatMsg) (processed bool) {
  16. switch in.header.MsgType {
  17. case "text":
  18. processed = ss.serveTextCommand(in)
  19. case "image":
  20. in.replyText("图片收到")
  21. processed = true
  22. case "voice":
  23. processed = ss.serveTextCommand(in)
  24. case "video":
  25. case "shortvideo":
  26. case "location":
  27. case "link":
  28. case "event":
  29. processed = ss.serveEvents(in)
  30. default:
  31. processed = false
  32. }
  33. return
  34. }
  35. func (ss *openIDSessionData) serveTextCommand(in *InWechatMsg) (processed bool) {
  36. cmd := ""
  37. if in.header.MsgType == "text" {
  38. cmd = in.body.(TextMsg).Content
  39. } else if in.header.MsgType == "voice" {
  40. cmd = voice2Cmd(in.body.(VoiceMsg))
  41. }
  42. if cmd != "" {
  43. if f, hasFunction := commandMap[cmd]; hasFunction {
  44. processed = f(ss, in)
  45. log.Printf("cmd...%s", ss.Procedure)
  46. return
  47. }
  48. if cmd == "所有命令" || cmd == "all command" {
  49. return allCommand(ss, in)
  50. }
  51. if proc, hasProc := AllProc[cmd]; hasProc {
  52. proc.init(ss)
  53. proc.intro(ss, in)
  54. proc.start(ss, in)
  55. processed = true
  56. return true
  57. }
  58. }
  59. return false //不认识的命令,我们选择这个信息不处理
  60. }
  61. func allCommand(ss *openIDSessionData, in *InWechatMsg) (processed bool) {
  62. processed = true
  63. msg := "命令如下:\n"
  64. count := 0
  65. for k := range commandMap {
  66. count++
  67. msg = msg + fmt.Sprintf("%0d : %s \n", count, k)
  68. }
  69. for k := range AllProc {
  70. count++
  71. msg = msg + fmt.Sprintf("%0d : %s \n", count, k)
  72. }
  73. in.replyText(msg)
  74. return
  75. }
  76. func cmdVersion(ss *openIDSessionData, in *InWechatMsg) (processed bool) {
  77. processed = true
  78. in.replyText("这是测试版本" + time.Now().Format("2006/01/02 03:04:05"))
  79. return
  80. }
  81. func cmdEcho(ss *openIDSessionData, in *InWechatMsg) (processed bool) {
  82. processed = true
  83. procEcho.init(ss)
  84. procEcho.intro(ss, in)
  85. procEcho.start(ss, in)
  86. return
  87. }
  88. func voice2Cmd(v VoiceMsg) (cmd string) {
  89. cmd = v.Recognition
  90. cmd = strings.TrimSuffix(cmd, "。") //去掉末尾的标点符号,句号
  91. return
  92. }