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.

183 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 + "\n"
  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. msg := ss.getVal("msg")
  78. msg = msg + m.Recognition + "\n"
  79. ss.setKvPair("msg", msg)
  80. kfSendTxt(openID, "翻译结果:"+m.Recognition)
  81. case VideoMsg:
  82. m := in.body.(VideoMsg)
  83. str = buildVideoMsg(openID, "e2iNEiSxCX5TV1WbFd0TQMn5lilY3bylh1--lDBwi7I", "航拍春日哈工大", m.MediaId)
  84. in.replyXML(str)
  85. case ShortVideoMsg:
  86. m := in.body.(ShortVideoMsg)
  87. str = buildVideoMsg(openID, "e2iNEiSxCX5TV1WbFd0TQMn5lilY3bylh1--lDBwi7I", "航拍春日哈工大", m.MediaId)
  88. in.replyXML(str)
  89. case LocationMsg:
  90. m := in.body.(LocationMsg)
  91. mid := location2MediaID(m.Location_X, m.Location_Y)
  92. str = buildPicMsg(openID, mid)
  93. in.replyXML(str)
  94. url := location2URL(m.Location_X, m.Location_Y)
  95. kfSendTxt(openID, url+"\n"+fmt.Sprintf("long=%f, lat=%f, scale=%d", m.Location_X, m.Location_Y, m.Scale))
  96. case LinkMsg:
  97. m := in.body.(LinkMsg)
  98. str, _ = BuildTextMsg(openID, m.Url)
  99. in.replyXML(str)
  100. kfSendTxt(openID, m.Title+m.Description)
  101. case EventMsg:
  102. echoEvents(in)
  103. default:
  104. str, err = BuildTextMsg(openID, "text message")
  105. }
  106. if err != nil {
  107. log.Println("build response failed")
  108. }
  109. return
  110. }
  111. func echoEvents(in InWechatMsg) {
  112. openID := in.header.FromUserName
  113. str := ""
  114. m := in.body.(EventMsg)
  115. log.Println("Event = " + m.Event)
  116. switch m.Event {
  117. case "LOCATION":
  118. mid := location2MediaID(m.Latitude, m.Longitude)
  119. str = buildPicMsg(openID, mid)
  120. case "CLICK":
  121. str, _ = BuildTextMsg(openID, m.EventKey)
  122. case "subscribe":
  123. str, _ = BuildTextMsg(openID, m.EventKey)
  124. case "unsubscribe":
  125. str, _ = BuildTextMsg(openID, m.EventKey)
  126. case "SCAN":
  127. str, _ = BuildTextMsg(openID, m.EventKey)
  128. case "VIEW":
  129. str, _ = BuildTextMsg(openID, m.EventKey)
  130. case "kf_create_session":
  131. kfSendTxt(openID, m.KfAccount)
  132. //str, _ = BuildTextMsg(openID, m.KfAccount) // response msg is ignored by wechat
  133. case "kf_close_session":
  134. kfSendTxt(openID, m.KfAccount+"\n close type ="+m.CloseType)
  135. //str, _ = BuildTextMsg(openID, m.KfAccount+"\n close type ="+m.CloseType) //response msg is ignored by wechat
  136. case "scancode_waitmsg":
  137. log.Println(m.ScanCodeInfo.ScanResult)
  138. log.Println(m.ScanCodeInfo.ScanType)
  139. msg := fmt.Sprintf("ScanResult =%s, ScanType=%s", m.ScanCodeInfo.ScanResult, m.ScanCodeInfo.ScanType)
  140. kfSendTxt(openID, msg)
  141. case "pic_photo_or_album":
  142. msg := fmt.Sprintf("Count = %d ", m.SendPicsInfo.Count)
  143. log.Println(m.SendPicsInfo.Count)
  144. for _, v := range m.SendPicsInfo.PicList.Item {
  145. log.Println(v.PicMd5Sum)
  146. kfSendTxt(openID, v.PicMd5Sum)
  147. }
  148. kfSendTxt(openID, msg)
  149. default:
  150. str, _ = BuildTextMsg(openID, " unknown event:"+m.Event)
  151. }
  152. in.replyXML(str)
  153. return
  154. }
  155. func location2MediaID(lat, long float64) (mediaID string) {
  156. 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)
  157. file, _, _ := saveURL(url)
  158. mediaID = uploadImage(file)
  159. os.Remove(file)
  160. return
  161. }
  162. func location2URL(lat, long float64) (url string) {
  163. url = fmt.Sprintf("http://maps.google.com/maps?z=12&t=m&q=loc:%f+%f", lat, long)
  164. log.Println(url)
  165. return
  166. }