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.

196 satır
5.4KB

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