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.

193 line
5.7KB

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