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.

153 lines
4.1KB

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