Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

122 lines
3.7KB

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