Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

191 Zeilen
5.5KB

  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) {
  55. kfSendTxt(ss.OpenID, "10分钟静默之后 Echo将自动结束,\n输入 [结束Echo],或者语音 ‘退出退出', 清除Echo模式")
  56. }
  57. func (m *procEchoDef) doCommand(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. procEcho.recordMsg(ss, "文字:"+m.Content)
  66. in.replyXML(str)
  67. case PicMsg:
  68. m := in.body.(PicMsg)
  69. str = buildPicMsg(openID, m.MediaId)
  70. in.replyXML(str)
  71. procEcho.recordMsg(ss, "图片:..")
  72. case VoiceMsg:
  73. m := in.body.(VoiceMsg)
  74. str = buildVoiceMsg(openID, m.MediaId)
  75. in.replyXML(str)
  76. procEcho.recordMsg(ss, "音译:"+m.Recognition)
  77. kfSendTxt(openID, "翻译结果:"+m.Recognition)
  78. case VideoMsg:
  79. m := in.body.(VideoMsg)
  80. str = buildVideoMsg(openID, "e2iNEiSxCX5TV1WbFd0TQMn5lilY3bylh1--lDBwi7I", "航拍春日哈工大", m.MediaId)
  81. in.replyXML(str)
  82. procEcho.recordMsg(ss, "视频:..")
  83. case ShortVideoMsg:
  84. m := in.body.(ShortVideoMsg)
  85. str = buildVideoMsg(openID, "e2iNEiSxCX5TV1WbFd0TQMn5lilY3bylh1--lDBwi7I", "航拍春日哈工大", m.MediaId)
  86. in.replyXML(str)
  87. procEcho.recordMsg(ss, "小视频:..")
  88. case LocationMsg:
  89. m := in.body.(LocationMsg)
  90. mid := location2MediaID(m.Location_X, m.Location_Y)
  91. str = buildPicMsg(openID, mid)
  92. in.replyXML(str)
  93. url := location2URL(m.Location_X, m.Location_Y)
  94. kfSendTxt(openID, url+"\n"+fmt.Sprintf("long=%f, lat=%f, scale=%d", m.Location_X, m.Location_Y, m.Scale))
  95. procEcho.recordMsg(ss, "坐标:..")
  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. procEcho.recordMsg(ss, "链接:"+m.Title)
  102. case EventMsg:
  103. procEcho.doEvents(ss, 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 (m *procEchoDef) doEvents(ss *openIDSessionData, in InWechatMsg) {
  113. openID := in.header.FromUserName
  114. str := ""
  115. e := in.body.(EventMsg)
  116. //log.Println("Event = " + m.Event)
  117. procEcho.recordMsg(ss, "事件:"+e.Event)
  118. switch e.Event {
  119. case "LOCATION":
  120. mid := location2MediaID(e.Latitude, e.Longitude)
  121. str = buildPicMsg(openID, mid)
  122. case "CLICK":
  123. str, _ = BuildTextMsg(openID, e.EventKey)
  124. case "subscribe":
  125. str, _ = BuildTextMsg(openID, e.EventKey)
  126. case "unsubscribe":
  127. str, _ = BuildTextMsg(openID, e.EventKey)
  128. case "SCAN":
  129. str, _ = BuildTextMsg(openID, e.EventKey)
  130. case "VIEW":
  131. str, _ = BuildTextMsg(openID, e.EventKey)
  132. case "kf_create_session":
  133. kfSendTxt(openID, e.KfAccount)
  134. //str, _ = BuildTextMsg(openID, m.KfAccount) // response msg is ignored by wechat
  135. case "kf_close_session":
  136. kfSendTxt(openID, e.KfAccount+"\n close type ="+e.CloseType)
  137. //str, _ = BuildTextMsg(openID, m.KfAccount+"\n close type ="+m.CloseType) //response msg is ignored by wechat
  138. case "scancode_waitmsg":
  139. log.Println(e.ScanCodeInfo.ScanResult)
  140. log.Println(e.ScanCodeInfo.ScanType)
  141. msg := fmt.Sprintf("ScanResult =%s, ScanType=%s", e.ScanCodeInfo.ScanResult, e.ScanCodeInfo.ScanType)
  142. kfSendTxt(openID, msg)
  143. case "pic_photo_or_album":
  144. msg := fmt.Sprintf("Count = %d ", e.SendPicsInfo.Count)
  145. log.Println(e.SendPicsInfo.Count)
  146. for _, v := range e.SendPicsInfo.PicList.Item {
  147. log.Println(v.PicMd5Sum)
  148. kfSendTxt(openID, v.PicMd5Sum)
  149. }
  150. kfSendTxt(openID, msg)
  151. default:
  152. str, _ = BuildTextMsg(openID, " unknown event:"+e.Event)
  153. }
  154. in.replyXML(str)
  155. return
  156. }
  157. func location2MediaID(lat, long float64) (mediaID string) {
  158. 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)
  159. file, _, _ := saveURL(url)
  160. mediaID = uploadImage(file)
  161. os.Remove(file)
  162. return
  163. }
  164. func location2URL(lat, long float64) (url string) {
  165. url = fmt.Sprintf("http://maps.google.com/maps?z=12&t=m&q=loc:%f+%f", lat, long)
  166. log.Println(url)
  167. return
  168. }
  169. func (m *procEchoDef) recordMsg(ss *openIDSessionData, newmsg string) {
  170. msg := ss.getVal("msg")
  171. msg = msg + newmsg + "\n"
  172. ss.setKvPair("msg", msg)
  173. }