Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

128 lines
3.8KB

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. )
  7. func serveCommand(openID string, in InWechatMsg) (state chatState, processed bool) {
  8. log.Println("process command")
  9. return echoCommand(openID, in)
  10. }
  11. func echoCommand(openID string, in InWechatMsg) (state chatState, processed bool) {
  12. processed = true
  13. str, err := BuildTextMsg(openID, "default")
  14. log.Println(in.header.MsgType)
  15. switch in.body.(type) {
  16. case TextMsg:
  17. m := in.body.(TextMsg)
  18. str, err = BuildTextMsg(openID, m.Content+"\n\n关键词 [转接] 将后续信息都转接到 客服")
  19. if m.Content == "转接" {
  20. processed = false
  21. }
  22. case PicMsg:
  23. m := in.body.(PicMsg)
  24. str = buildPicMsg(openID, m.MediaId)
  25. case VoiceMsg:
  26. m := in.body.(VoiceMsg)
  27. str = buildVoiceMsg(openID, m.MediaId)
  28. kfSendTxt(openID, "翻译结果:"+m.Recognition)
  29. case VideoMsg:
  30. m := in.body.(VideoMsg)
  31. str = buildVideoMsg(openID, "e2iNEiSxCX5TV1WbFd0TQMn5lilY3bylh1--lDBwi7I", "航拍春日哈工大", m.MediaId)
  32. case ShortVideoMsg:
  33. m := in.body.(ShortVideoMsg)
  34. str = buildVideoMsg(openID, "e2iNEiSxCX5TV1WbFd0TQMn5lilY3bylh1--lDBwi7I", "航拍春日哈工大", m.MediaId)
  35. case LocationMsg:
  36. m := in.body.(LocationMsg)
  37. mid := location2MediaID(m.Location_X, m.Location_Y)
  38. //str, _ = BuildTextMsg(openID, fmt.Sprintf("long=%f, lat=%f, scale=%d", m.Location_X, m.Location_Y, m.Scale))
  39. str = buildPicMsg(openID, mid)
  40. url := location2URL(m.Location_X, m.Location_Y)
  41. kfSendTxt(openID, url)
  42. case LinkMsg:
  43. m := in.body.(LinkMsg)
  44. kfSendTxt(openID, m.Title+m.Description)
  45. str, _ = BuildTextMsg(openID, m.Url)
  46. case EventMsg:
  47. return echoEvents(openID, in)
  48. default:
  49. str, err = BuildTextMsg(openID, "text message")
  50. }
  51. state.OpenID = openID
  52. state.Procedure = ""
  53. state.response = str
  54. log.Println(str)
  55. if err != nil {
  56. log.Println("build response failed")
  57. processed = false
  58. }
  59. //state is any state that
  60. return
  61. }
  62. func echoEvents(openID string, in InWechatMsg) (state chatState, processed bool) {
  63. processed = true
  64. str := ""
  65. m := in.body.(EventMsg)
  66. log.Println("Event = " + m.Event)
  67. switch m.Event {
  68. case "LOCATION":
  69. mid := location2MediaID(m.Latitude, m.Longitude)
  70. str = buildPicMsg(openID, mid)
  71. case "CLICK":
  72. str, _ = BuildTextMsg(openID, m.EventKey)
  73. case "subscribe":
  74. str, _ = BuildTextMsg(openID, m.EventKey)
  75. case "unsubscribe":
  76. str, _ = BuildTextMsg(openID, m.EventKey)
  77. case "SCAN":
  78. str, _ = BuildTextMsg(openID, m.EventKey)
  79. case "VIEW":
  80. str, _ = BuildTextMsg(openID, m.EventKey)
  81. case "kf_create_session":
  82. kfSendTxt(openID, m.KfAccount)
  83. //str, _ = BuildTextMsg(openID, m.KfAccount) // response msg is ignored by wechat
  84. case "kf_close_session":
  85. kfSendTxt(openID, m.KfAccount+"\n close type ="+m.CloseType)
  86. //str, _ = BuildTextMsg(openID, m.KfAccount+"\n close type ="+m.CloseType) //response msg is ignored by wechat
  87. case "scancode_waitmsg":
  88. log.Println(m.ScanCodeInfo.ScanResult)
  89. log.Println(m.ScanCodeInfo.ScanType)
  90. msg := fmt.Sprintf("ScanResult =%s, ScanType=%s", m.ScanCodeInfo.ScanResult, m.ScanCodeInfo.ScanType)
  91. kfSendTxt(openID, msg)
  92. case "pic_photo_or_album":
  93. msg := fmt.Sprintf("Count = %d ", m.SendPicsInfo.Count)
  94. log.Println(m.SendPicsInfo.Count)
  95. for _, v := range m.SendPicsInfo.PicList.Item {
  96. log.Println(v.PicMd5Sum)
  97. kfSendTxt(openID, v.PicMd5Sum)
  98. }
  99. kfSendTxt(openID, msg)
  100. default:
  101. str, _ = BuildTextMsg(openID, " unknown event:"+m.Event)
  102. }
  103. state.response = str
  104. return
  105. }
  106. func location2MediaID(lat, long float64) (mediaID string) {
  107. url := fmt.Sprintf("https://maps.googleapis.com/maps/api/staticmap?center=%f,%f&markers=color:red|label:S|%f,%f&zoom=17&size=1280x2014", lat, long, lat, long)
  108. file, _, _ := saveURL(url)
  109. mediaID = uploadImage(file)
  110. os.Remove(file)
  111. return
  112. }
  113. func location2URL(lat, long float64) (url string) {
  114. url = fmt.Sprintf("http://maps.google.com/maps?z=12&t=m&q=loc:%f+%f", lat, long)
  115. log.Println(url)
  116. return
  117. }