| //are we in an existing procedure | //are we in an existing procedure | ||||
| log.Printf("current procedure = %s", ss.Procedure) | log.Printf("current procedure = %s", ss.Procedure) | ||||
| proc, found := AllProc[ss.Procedure] | proc, found := AllProc[ss.Procedure] | ||||
| processed := false | |||||
| if found { | 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) | log.Printf("current procedure after command = %s", ss.Procedure) | ||||
| if !processed { // transfer to Customer Service (kf) | if !processed { // transfer to Customer Service (kf) | ||||
| //start transfer | //start transfer |
| ss.Procedure = "" | 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) summary(ss *openIDSessionData) { | ||||
| } | } | ||||
| func (m *procDummyDef) intro(ss *openIDSessionData, in* InWechatMsg) { | |||||
| func (m *procDummyDef) intro(ss *openIDSessionData, in *InWechatMsg) { | |||||
| } | } |
| //log.Println(*ss) | //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")) | 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 | stopEcho := false | ||||
| if in.header.MsgType == "text" { | if in.header.MsgType == "text" { | ||||
| if in.body.(TextMsg).Content == "结束Echo" || in.body.(TextMsg).Content == "结束echo" { | if in.body.(TextMsg).Content == "结束Echo" || in.body.(TextMsg).Content == "结束echo" { | ||||
| ss.refreshExpire(600) | ss.refreshExpire(600) | ||||
| procEcho.doCommand(ss, in) | procEcho.doCommand(ss, in) | ||||
| } | } | ||||
| processed = true | |||||
| return | |||||
| } | } | ||||
| func (m *procEchoDef) summary(ss *openIDSessionData) { | func (m *procEchoDef) summary(ss *openIDSessionData) { | ||||
| kfSendTxt(ss.OpenID, msg) | kfSendTxt(ss.OpenID, msg) | ||||
| } | } | ||||
| func (m *procEchoDef) intro(ss *openIDSessionData, in* InWechatMsg) { | |||||
| func (m *procEchoDef) intro(ss *openIDSessionData, in *InWechatMsg) { | |||||
| in.replyText("请输入不同类型的微信信息,比如文字,图片,视频,地址,链接,我们将原样回应您") | in.replyText("请输入不同类型的微信信息,比如文字,图片,视频,地址,链接,我们将原样回应您") | ||||
| kfSendTxt(ss.OpenID, "10分钟静默之后 Echo将自动结束,\n输入 [结束Echo],或者语音 ‘退出退出', 清除Echo模式") | 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 | openID := in.header.FromUserName | ||||
| str, err := BuildTextMsg(openID, "default") | str, err := BuildTextMsg(openID, "default") | ||||
| log.Println("echoCommand :" + in.header.MsgType) | log.Println("echoCommand :" + in.header.MsgType) | ||||
| return | return | ||||
| } | } | ||||
| func (m *procEchoDef) doEvents(ss *openIDSessionData, in* InWechatMsg) { | |||||
| func (m *procEchoDef) doEvents(ss *openIDSessionData, in *InWechatMsg) { | |||||
| openID := in.header.FromUserName | openID := in.header.FromUserName | ||||
| str := "" | str := "" | ||||
| e := in.body.(EventMsg) | e := in.body.(EventMsg) |
| procGetBasicUserInfo.clean(ss) | 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) { | func (m *getBasicUserInfoDef) summary(ss *openIDSessionData) { | ||||
| //build article message with user's picture and email,phone | //build article message with user's picture and email,phone |
| } | } | ||||
| func (m *procSearchMemberDef) serve(ss *openIDSessionData, in *InWechatMsg) { | |||||
| func (m *procSearchMemberDef) serve(ss *openIDSessionData, in *InWechatMsg) (processed bool) { | |||||
| switch msg := in.body.(type) { | switch msg := in.body.(type) { | ||||
| case TextMsg: | case TextMsg: | ||||
| if m.endingCommand(ss, msg.Content) { | if m.endingCommand(ss, msg.Content) { | ||||
| str, _ := BuildTextMsg(ss.OpenID, "搜索 "+msg.Content+"... ...") | str, _ := BuildTextMsg(ss.OpenID, "搜索 "+msg.Content+"... ...") | ||||
| in.replyXML(str) | in.replyXML(str) | ||||
| m.displayUserInfo(ss, msg.Content) | m.displayUserInfo(ss, msg.Content) | ||||
| processed = true | |||||
| case PicMsg: | case PicMsg: | ||||
| //str := buildPicMsg(ss.OpenID, msg.MediaId) | //str := buildPicMsg(ss.OpenID, msg.MediaId) | ||||
| xml, _ := BuildKFTransferAnyOneMsg(ss.OpenID) | xml, _ := BuildKFTransferAnyOneMsg(ss.OpenID) | ||||
| in.replyXML(xml) | in.replyXML(xml) | ||||
| kfSendPicByMediaID(ss.OpenID, msg.MediaId) | kfSendPicByMediaID(ss.OpenID, msg.MediaId) | ||||
| kfSendTxt(ss.OpenID, "已转接校友会理事会,稍后答复您") | kfSendTxt(ss.OpenID, "已转接校友会理事会,稍后答复您") | ||||
| processed = true | |||||
| case VoiceMsg: | case VoiceMsg: | ||||
| name := strings.TrimSuffix(msg.Recognition, "。") | name := strings.TrimSuffix(msg.Recognition, "。") | ||||
| str, _ := BuildTextMsg(ss.OpenID, "搜索 "+name+"... ...") | str, _ := BuildTextMsg(ss.OpenID, "搜索 "+name+"... ...") | ||||
| in.replyXML(str) | in.replyXML(str) | ||||
| m.displayUserInfo(ss, name) | m.displayUserInfo(ss, name) | ||||
| processed = true | |||||
| case EventMsg: | case EventMsg: | ||||
| if msg.Event == "LOCATION" { | if msg.Event == "LOCATION" { | ||||
| onLocation(in) | onLocation(in) | ||||
| processed = true | |||||
| } | } | ||||
| default: | default: | ||||
| str := fmt.Sprintf("搜索功能结束") | str := fmt.Sprintf("搜索功能结束") | ||||
| kfSendTxtAs(ss.OpenID, str, "孙鹏") | kfSendTxtAs(ss.OpenID, str, "孙鹏") | ||||
| m.clean(ss) | |||||
| processed = false | |||||
| } | } | ||||
| return | |||||
| } | } | ||||
| func (m *procSearchMemberDef) summary(ss *openIDSessionData) { | func (m *procSearchMemberDef) summary(ss *openIDSessionData) { | ||||
| init(*openIDSessionData) //house keeping | init(*openIDSessionData) //house keeping | ||||
| clean(*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 | //AllProc all procedure that we implemented |