No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

102 líneas
2.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)
  19. case PicMsg:
  20. m := in.body.(PicMsg)
  21. str = buildPicMsg(openID, m.MediaId)
  22. case VoiceMsg:
  23. m := in.body.(VoiceMsg)
  24. str = buildVoiceMsg(openID, m.MediaId)
  25. kfSendTxt(openID, "翻译结果:"+m.Recognition)
  26. case VideoMsg:
  27. m := in.body.(VideoMsg)
  28. str = buildVideoMsg(openID, "e2iNEiSxCX5TV1WbFd0TQMn5lilY3bylh1--lDBwi7I", "航拍春日哈工大", m.MediaId)
  29. case ShortVideoMsg:
  30. m := in.body.(ShortVideoMsg)
  31. str = buildVideoMsg(openID, "e2iNEiSxCX5TV1WbFd0TQMn5lilY3bylh1--lDBwi7I", "航拍春日哈工大", m.MediaId)
  32. case LocationMsg:
  33. m := in.body.(LocationMsg)
  34. mid := location2MediaID(m.Location_X, m.Location_Y)
  35. //str, _ = BuildTextMsg(openID, fmt.Sprintf("long=%f, lat=%f, scale=%d", m.Location_X, m.Location_Y, m.Scale))
  36. // str = buildPicMsg(openID, mid)
  37. url := location2URL(m.Location_X, m.Location_Y)
  38. str, _ = BuildTextMsg(openID, mid+"\r\n"+url)
  39. kfSendTxt(openID, url)
  40. case LinkMsg:
  41. m := in.body.(LinkMsg)
  42. kfSendTxt(openID, m.Title+m.Description)
  43. str, _ = BuildTextMsg(openID, m.Url)
  44. case EventMsg:
  45. return echoEvents(openID, in)
  46. default:
  47. str, err = BuildTextMsg(openID, "text message")
  48. }
  49. state.OpenID = openID
  50. state.Procedure = ""
  51. state.response = str
  52. log.Println(str)
  53. if err != nil {
  54. log.Println("build response failed")
  55. processed = false
  56. }
  57. //state is any state that
  58. return
  59. }
  60. func echoEvents(openID string, in InWechatMsg) (state chatState, processed bool) {
  61. processed = true
  62. str := ""
  63. m := in.body.(EventMsg)
  64. log.Println("Event = " + m.Event)
  65. switch m.Event {
  66. case "LOCATION":
  67. mid := location2MediaID(m.Latitude, m.Longitude)
  68. str = buildPicMsg(openID, mid)
  69. case "CLICK":
  70. str, _ = BuildTextMsg(openID, m.EventKey)
  71. case "subscribe":
  72. case "unsubscribe":
  73. case "SCAN":
  74. case "VIEW":
  75. default:
  76. str, _ = BuildTextMsg(openID, " unknown event :"+m.Event)
  77. }
  78. state.response = str
  79. return
  80. }
  81. func location2MediaID(lat, long float64) (mediaID string) {
  82. 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)
  83. file, _, _ := saveURL(url)
  84. mediaID = uploadImage(file)
  85. os.Remove(file)
  86. return
  87. }
  88. func location2URL(lat, long float64) (url string) {
  89. url = fmt.Sprintf("http://maps.google.com/maps?z=12&t=m&q=loc:%f+%f", lat, long)
  90. log.Println(url)
  91. return
  92. }