| @@ -157,10 +157,20 @@ func (ss *openIDSessionData) incomingMsg(v *InWechatMsg) { | |||
| //are we in an existing procedure | |||
| log.Printf("current procedure = %s", ss.Procedure) | |||
| proc, found := AllProc[ss.Procedure] | |||
| processed := false | |||
| if found { | |||
| proc.serve(ss, v) //transit to new state | |||
| } else { | |||
| processed := ss.serveCommand(v) //menu or txt command e.g. search | |||
| processed = proc.serve(ss, v) //transit to new state | |||
| if processed { | |||
| return | |||
| } | |||
| // | |||
| log.Printf("Auto Switch to next State [%s] ending", ss.Procedure) | |||
| proc.summary(ss) | |||
| proc.clean(ss) | |||
| } | |||
| if !processed { | |||
| processed = ss.serveCommand(v) //menu or txt command e.g. search | |||
| log.Printf("current procedure after command = %s", ss.Procedure) | |||
| if !processed { // transfer to Customer Service (kf) | |||
| //start transfer | |||
| @@ -15,17 +15,17 @@ func (m *procDummyDef) clean(ss *openIDSessionData) { | |||
| ss.Procedure = "" | |||
| } | |||
| func (m *procDummyDef) start(ss *openIDSessionData, in* InWechatMsg) { | |||
| func (m *procDummyDef) start(ss *openIDSessionData, in *InWechatMsg) { | |||
| } | |||
| func (m *procDummyDef) serve(ss *openIDSessionData, in* InWechatMsg) { | |||
| func (m *procDummyDef) serve(ss *openIDSessionData, in *InWechatMsg) (processed bool) { | |||
| return | |||
| } | |||
| func (m *procDummyDef) summary(ss *openIDSessionData) { | |||
| } | |||
| func (m *procDummyDef) intro(ss *openIDSessionData, in* InWechatMsg) { | |||
| func (m *procDummyDef) intro(ss *openIDSessionData, in *InWechatMsg) { | |||
| } | |||
| @@ -23,11 +23,11 @@ func (m *procEchoDef) clean(ss *openIDSessionData) { | |||
| //log.Println(*ss) | |||
| } | |||
| func (m *procEchoDef) start(ss *openIDSessionData, in* InWechatMsg) { | |||
| func (m *procEchoDef) start(ss *openIDSessionData, in *InWechatMsg) { | |||
| ss.setKvPair("started at", time.Now().Format("2006/03:04:05")) | |||
| } | |||
| func (m *procEchoDef) serve(ss *openIDSessionData, in* InWechatMsg) { | |||
| func (m *procEchoDef) serve(ss *openIDSessionData, in *InWechatMsg) (processed bool) { | |||
| stopEcho := false | |||
| if in.header.MsgType == "text" { | |||
| if in.body.(TextMsg).Content == "结束Echo" || in.body.(TextMsg).Content == "结束echo" { | |||
| @@ -48,7 +48,8 @@ func (m *procEchoDef) serve(ss *openIDSessionData, in* InWechatMsg) { | |||
| ss.refreshExpire(600) | |||
| procEcho.doCommand(ss, in) | |||
| } | |||
| processed = true | |||
| return | |||
| } | |||
| func (m *procEchoDef) summary(ss *openIDSessionData) { | |||
| @@ -62,12 +63,12 @@ func (m *procEchoDef) summary(ss *openIDSessionData) { | |||
| kfSendTxt(ss.OpenID, msg) | |||
| } | |||
| func (m *procEchoDef) intro(ss *openIDSessionData, in* InWechatMsg) { | |||
| func (m *procEchoDef) intro(ss *openIDSessionData, in *InWechatMsg) { | |||
| in.replyText("请输入不同类型的微信信息,比如文字,图片,视频,地址,链接,我们将原样回应您") | |||
| kfSendTxt(ss.OpenID, "10分钟静默之后 Echo将自动结束,\n输入 [结束Echo],或者语音 ‘退出退出', 清除Echo模式") | |||
| } | |||
| func (m *procEchoDef) doCommand(ss *openIDSessionData, in* InWechatMsg) { | |||
| func (m *procEchoDef) doCommand(ss *openIDSessionData, in *InWechatMsg) { | |||
| openID := in.header.FromUserName | |||
| str, err := BuildTextMsg(openID, "default") | |||
| log.Println("echoCommand :" + in.header.MsgType) | |||
| @@ -124,7 +125,7 @@ func (m *procEchoDef) doCommand(ss *openIDSessionData, in* InWechatMsg) { | |||
| return | |||
| } | |||
| func (m *procEchoDef) doEvents(ss *openIDSessionData, in* InWechatMsg) { | |||
| func (m *procEchoDef) doEvents(ss *openIDSessionData, in *InWechatMsg) { | |||
| openID := in.header.FromUserName | |||
| str := "" | |||
| e := in.body.(EventMsg) | |||
| @@ -21,8 +21,8 @@ func (m *getBasicUserInfoDef) start(ss *openIDSessionData, in *InWechatMsg) { | |||
| procGetBasicUserInfo.clean(ss) | |||
| } | |||
| func (m *getBasicUserInfoDef) serve(ss *openIDSessionData, in *InWechatMsg) { | |||
| func (m *getBasicUserInfoDef) serve(ss *openIDSessionData, in *InWechatMsg) (processed bool) { | |||
| return false | |||
| } | |||
| func (m *getBasicUserInfoDef) summary(ss *openIDSessionData) { | |||
| //build article message with user's picture and email,phone | |||
| @@ -25,7 +25,7 @@ func (m *procSearchMemberDef) start(ss *openIDSessionData, in *InWechatMsg) { | |||
| } | |||
| func (m *procSearchMemberDef) serve(ss *openIDSessionData, in *InWechatMsg) { | |||
| func (m *procSearchMemberDef) serve(ss *openIDSessionData, in *InWechatMsg) (processed bool) { | |||
| switch msg := in.body.(type) { | |||
| case TextMsg: | |||
| if m.endingCommand(ss, msg.Content) { | |||
| @@ -34,26 +34,31 @@ func (m *procSearchMemberDef) serve(ss *openIDSessionData, in *InWechatMsg) { | |||
| str, _ := BuildTextMsg(ss.OpenID, "搜索 "+msg.Content+"... ...") | |||
| in.replyXML(str) | |||
| m.displayUserInfo(ss, msg.Content) | |||
| processed = true | |||
| case PicMsg: | |||
| //str := buildPicMsg(ss.OpenID, msg.MediaId) | |||
| xml, _ := BuildKFTransferAnyOneMsg(ss.OpenID) | |||
| in.replyXML(xml) | |||
| kfSendPicByMediaID(ss.OpenID, msg.MediaId) | |||
| kfSendTxt(ss.OpenID, "已转接校友会理事会,稍后答复您") | |||
| processed = true | |||
| case VoiceMsg: | |||
| name := strings.TrimSuffix(msg.Recognition, "。") | |||
| str, _ := BuildTextMsg(ss.OpenID, "搜索 "+name+"... ...") | |||
| in.replyXML(str) | |||
| m.displayUserInfo(ss, name) | |||
| processed = true | |||
| case EventMsg: | |||
| if msg.Event == "LOCATION" { | |||
| onLocation(in) | |||
| processed = true | |||
| } | |||
| default: | |||
| str := fmt.Sprintf("搜索功能结束") | |||
| kfSendTxtAs(ss.OpenID, str, "孙鹏") | |||
| m.clean(ss) | |||
| processed = false | |||
| } | |||
| return | |||
| } | |||
| func (m *procSearchMemberDef) summary(ss *openIDSessionData) { | |||
| @@ -24,10 +24,10 @@ type chatProcedure interface { | |||
| init(*openIDSessionData) //house keeping | |||
| clean(*openIDSessionData) //house keeping | |||
| start(*openIDSessionData, *InWechatMsg) //for first message | |||
| serve(*openIDSessionData, *InWechatMsg) //for all subsequent message | |||
| summary(*openIDSessionData) //after all message has been done | |||
| intro(*openIDSessionData, *InWechatMsg) //initial text/video/voice introduction | |||
| start(*openIDSessionData, *InWechatMsg) //for first message | |||
| serve(*openIDSessionData, *InWechatMsg) bool //for all subsequent message | |||
| summary(*openIDSessionData) //after all message has been done | |||
| intro(*openIDSessionData, *InWechatMsg) //initial text/video/voice introduction | |||
| } | |||
| //AllProc all procedure that we implemented | |||