Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

192 lines
5.6KB

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "time"
  7. )
  8. type procEchoDef struct {
  9. chatProcedureID
  10. }
  11. var procEcho = procEchoDef{}
  12. func (m *procEchoDef) init(ss *openIDSessionData) {
  13. ss.setProcedure(m.getID())
  14. ss.refreshExpire(600)
  15. }
  16. func (m *procEchoDef) clean(ss *openIDSessionData) {
  17. ss.Procedure = ""
  18. //log.Println(*ss)
  19. }
  20. func (m *procEchoDef) start(ss *openIDSessionData, in* InWechatMsg) {
  21. ss.setKvPair("started at", time.Now().Format("2006/03:04:05"))
  22. }
  23. func (m *procEchoDef) serve(ss *openIDSessionData, in* InWechatMsg) {
  24. stopEcho := false
  25. if in.header.MsgType == "text" {
  26. if in.body.(TextMsg).Content == "结束Echo" || in.body.(TextMsg).Content == "结束echo" {
  27. stopEcho = true
  28. }
  29. }
  30. if in.header.MsgType == "voice" {
  31. if in.body.(VoiceMsg).Recognition == "退出退出。" {
  32. stopEcho = true
  33. }
  34. }
  35. if stopEcho {
  36. in.replyText("echo 正式结束")
  37. procEcho.summary(ss)
  38. procEcho.clean(ss)
  39. } else {
  40. ss.refreshExpire(600)
  41. procEcho.doCommand(ss, in)
  42. }
  43. }
  44. func (m *procEchoDef) summary(ss *openIDSessionData) {
  45. msg := ss.getVal("started at")
  46. allmsg := ss.getVal("msg") //may be too long
  47. if len(allmsg) > 2000 {
  48. sub := allmsg[0:2000] + "..."
  49. allmsg = sub
  50. }
  51. msg = msg + "\n" + allmsg
  52. kfSendTxt(ss.OpenID, msg)
  53. }
  54. func (m *procEchoDef) intro(ss *openIDSessionData, in* InWechatMsg) {
  55. in.replyText("请输入不同类型的微信信息,比如文字,图片,视频,地址,链接,我们将原样回应您")
  56. kfSendTxt(ss.OpenID, "10分钟静默之后 Echo将自动结束,\n输入 [结束Echo],或者语音 ‘退出退出', 清除Echo模式")
  57. }
  58. func (m *procEchoDef) doCommand(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. procEcho.recordMsg(ss, "文字:"+m.Content)
  67. in.replyXML(str)
  68. case PicMsg:
  69. m := in.body.(PicMsg)
  70. str = buildPicMsg(openID, m.MediaId)
  71. in.replyXML(str)
  72. procEcho.recordMsg(ss, "图片:..")
  73. case VoiceMsg:
  74. m := in.body.(VoiceMsg)
  75. str = buildVoiceMsg(openID, m.MediaId)
  76. in.replyXML(str)
  77. procEcho.recordMsg(ss, "音译:"+m.Recognition)
  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. procEcho.recordMsg(ss, "视频:..")
  84. case ShortVideoMsg:
  85. m := in.body.(ShortVideoMsg)
  86. str = buildVideoMsg(openID, "e2iNEiSxCX5TV1WbFd0TQMn5lilY3bylh1--lDBwi7I", "航拍春日哈工大", m.MediaId)
  87. in.replyXML(str)
  88. procEcho.recordMsg(ss, "小视频:..")
  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. procEcho.recordMsg(ss, "坐标:..")
  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. procEcho.recordMsg(ss, "链接:"+m.Title)
  103. case EventMsg:
  104. procEcho.doEvents(ss, in)
  105. default:
  106. str, err = BuildTextMsg(openID, "text message")
  107. }
  108. if err != nil {
  109. log.Println("build response failed")
  110. }
  111. return
  112. }
  113. func (m *procEchoDef) doEvents(ss *openIDSessionData, in* InWechatMsg) {
  114. openID := in.header.FromUserName
  115. str := ""
  116. e := in.body.(EventMsg)
  117. //log.Println("Event = " + m.Event)
  118. procEcho.recordMsg(ss, "事件:"+e.Event)
  119. switch e.Event {
  120. case "LOCATION":
  121. mid := location2MediaID(e.Latitude, e.Longitude)
  122. str = buildPicMsg(openID, mid)
  123. case "CLICK":
  124. str, _ = BuildTextMsg(openID, e.EventKey)
  125. case "subscribe":
  126. str, _ = BuildTextMsg(openID, e.EventKey)
  127. case "unsubscribe":
  128. str, _ = BuildTextMsg(openID, e.EventKey)
  129. case "SCAN":
  130. str, _ = BuildTextMsg(openID, e.EventKey)
  131. case "VIEW":
  132. str, _ = BuildTextMsg(openID, e.EventKey)
  133. case "kf_create_session":
  134. kfSendTxt(openID, e.KfAccount)
  135. //str, _ = BuildTextMsg(openID, m.KfAccount) // response msg is ignored by wechat
  136. case "kf_close_session":
  137. kfSendTxt(openID, e.KfAccount+"\n close type ="+e.CloseType)
  138. //str, _ = BuildTextMsg(openID, m.KfAccount+"\n close type ="+m.CloseType) //response msg is ignored by wechat
  139. case "scancode_waitmsg":
  140. log.Println(e.ScanCodeInfo.ScanResult)
  141. log.Println(e.ScanCodeInfo.ScanType)
  142. msg := fmt.Sprintf("ScanResult =%s, ScanType=%s", e.ScanCodeInfo.ScanResult, e.ScanCodeInfo.ScanType)
  143. kfSendTxt(openID, msg)
  144. case "pic_photo_or_album":
  145. msg := fmt.Sprintf("Count = %d ", e.SendPicsInfo.Count)
  146. log.Println(e.SendPicsInfo.Count)
  147. for _, v := range e.SendPicsInfo.PicList.Item {
  148. log.Println(v.PicMd5Sum)
  149. kfSendTxt(openID, v.PicMd5Sum)
  150. }
  151. kfSendTxt(openID, msg)
  152. default:
  153. str, _ = BuildTextMsg(openID, " unknown event:"+e.Event)
  154. }
  155. in.replyXML(str)
  156. return
  157. }
  158. func location2MediaID(lat, long float64) (mediaID string) {
  159. 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)
  160. file, _, _ := saveURL(url)
  161. mediaID = uploadImage(file)
  162. os.Remove(file)
  163. return
  164. }
  165. func location2URL(lat, long float64) (url string) {
  166. url = fmt.Sprintf("http://maps.google.com/maps?z=12&t=m&q=loc:%f+%f", lat, long)
  167. log.Println(url)
  168. return
  169. }
  170. func (m *procEchoDef) recordMsg(ss *openIDSessionData, newmsg string) {
  171. msg := ss.getVal("msg")
  172. msg = msg + newmsg + "\n"
  173. ss.setKvPair("msg", msg)
  174. }