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.

165 lines
4.4KB

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