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.

190 lines
5.1KB

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