From 3a00e3c04518cdfa27fd48ae71a57510c0cbf06a Mon Sep 17 00:00:00 2001 From: Patrick Peng Sun Date: Thu, 1 Jun 2017 01:07:41 +1000 Subject: [PATCH] echo server first version done. --- procEcho.go | 44 ++++++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/procEcho.go b/procEcho.go index 2c48f5d..68dc496 100644 --- a/procEcho.go +++ b/procEcho.go @@ -18,20 +18,33 @@ var procEcho = chatProcedure{ } func echoInit(ss *openIDSessionData) { - ss.Procedure = idProcEcho + ss.setProcedure(idProcEcho) + ss.refreshExpire(600) } func echoClean(ss *openIDSessionData) { ss.Procedure = "" + log.Println(*ss) } func echoStart(ss *openIDSessionData, in InWechatMsg) { - echoCommand(ss, in) + } func echoServe(ss *openIDSessionData, in InWechatMsg) { + if in.header.MsgType == "text" { + if in.body.(TextMsg).Content == "结束Echo" { + in.replyText("echo 正式结束") + echoClean(ss) + return + } + } + ss.refreshExpire(600) + echoCommand(ss, in) + kfSendTxt(ss.OpenID, "10 分钟之后 Echo将自动结束,\n输入 [结束Echo] 清除Echo模式") } + func echoSummary(ss *openIDSessionData) { } @@ -42,58 +55,57 @@ func echoInto(ss *openIDSessionData) { func echoCommand(ss *openIDSessionData, in InWechatMsg) { openID := in.header.FromUserName - processed = true str, err := BuildTextMsg(openID, "default") log.Println("echoCommand :" + in.header.MsgType) switch in.body.(type) { case TextMsg: m := in.body.(TextMsg) str, err = BuildTextMsg(openID, m.Content+"\n\n关键词 [转接] 将后续信息都转接到 客服 测试版") - if m.Content == "转接" { - processed = false - } + in.replyXML(str) case PicMsg: m := in.body.(PicMsg) str = buildPicMsg(openID, m.MediaId) - + in.replyXML(str) case VoiceMsg: m := in.body.(VoiceMsg) str = buildVoiceMsg(openID, m.MediaId) + in.replyXML(str) kfSendTxt(openID, "翻译结果:"+m.Recognition) case VideoMsg: m := in.body.(VideoMsg) str = buildVideoMsg(openID, "e2iNEiSxCX5TV1WbFd0TQMn5lilY3bylh1--lDBwi7I", "航拍春日哈工大", m.MediaId) + in.replyXML(str) case ShortVideoMsg: m := in.body.(ShortVideoMsg) str = buildVideoMsg(openID, "e2iNEiSxCX5TV1WbFd0TQMn5lilY3bylh1--lDBwi7I", "航拍春日哈工大", m.MediaId) + in.replyXML(str) case LocationMsg: m := in.body.(LocationMsg) mid := location2MediaID(m.Location_X, m.Location_Y) - //str, _ = BuildTextMsg(openID, fmt.Sprintf("long=%f, lat=%f, scale=%d", m.Location_X, m.Location_Y, m.Scale)) + str, _ = BuildTextMsg(openID, fmt.Sprintf("long=%f, lat=%f, scale=%d", m.Location_X, m.Location_Y, m.Scale)) + in.replyXML(str) str = buildPicMsg(openID, mid) url := location2URL(m.Location_X, m.Location_Y) kfSendTxt(openID, url) - case LinkMsg: m := in.body.(LinkMsg) - kfSendTxt(openID, m.Title+m.Description) str, _ = BuildTextMsg(openID, m.Url) + in.replyXML(str) + kfSendTxt(openID, m.Title+m.Description) case EventMsg: - return echoEvents(openID, in) + echoEvents(in) default: str, err = BuildTextMsg(openID, "text message") } if err != nil { log.Println("build response failed") - processed = false } - //state is any state that return } -func echoEvents(openID string, in InWechatMsg) (state chatState, processed bool) { - processed = true +func echoEvents(in InWechatMsg) { + openID := in.header.FromUserName str := "" m := in.body.(EventMsg) log.Println("Event = " + m.Event) @@ -133,7 +145,7 @@ func echoEvents(openID string, in InWechatMsg) (state chatState, processed bool) default: str, _ = BuildTextMsg(openID, " unknown event:"+m.Event) } - state.response = str + in.replyXML(str) return }