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.

181 lines
4.9KB

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