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.

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