Sfoglia il codice sorgente

state machine redesign state saving is controlled by main routine,

serveProc procude state transit,

its input is old state, output is new state.
master
Patrick Peng Sun 8 anni fa
parent
commit
3dde62d629
3 ha cambiato i file con 21 aggiunte e 10 eliminazioni
  1. +7
    -2
      chatState.go
  2. +1
    -1
      procedure.go
  3. +13
    -7
      server.go

+ 7
- 2
chatState.go Vedi File

//Validator function type for validating all wechat inputs //Validator function type for validating all wechat inputs
type Validator func(s chatState) ValidationResult type Validator func(s chatState) ValidationResult


func saveChatState(state chatState) {

func saveChatState(openID, procedure string, state chatState) (err error) {
if isExpired(state.Expire) {
//skip saving sate
return
}
_, err = setCurrentState(openID, procedure, state)
return
} }

+ 1
- 1
procedure.go Vedi File

} }


//follow procedure, if there is any //follow procedure, if there is any
func serveProc(openID string, input InWechatMsg) (next chatState) {
func serveProc(state chatState, input InWechatMsg) (next chatState) {
return return
} }

+ 13
- 7
server.go Vedi File



//are we in an existing procedure //are we in an existing procedure
openID := in.header.FromUserName openID := in.header.FromUserName
yes, state := isInProc(openID)
if yes {
state := serveProc(openID, in)
reply = state.response
inProc, state := isInProc(openID) //if inside a procedure, resume last saved state
if inProc {
state = serveProc(state, in) //transit to new state
reply = state.response //xml response
} else { } else {
state, processed := serveCommand(openID, in) //search or other command
state, processed := serveCommand(openID, in) //menu or txt command e.g. search
if !processed { // transfer to Customer Service (kf) if !processed { // transfer to Customer Service (kf)
reply = buildKfForwardMsg(openID, "") reply = buildKfForwardMsg(openID, "")
kfSendTxt(openID, "未识别的命令,已转接校友会理事会,稍后答复您")
} else { } else {
reply = state.response reply = state.response
} }
} }
log.Println(reply)
log.Println(reply) //instant reply, answering user's request
w.Header().Set("Content-Type", "text/xml; charset=utf-8") w.Header().Set("Content-Type", "text/xml; charset=utf-8")
fmt.Fprint(w, reply) fmt.Fprint(w, reply)


saveChatState(state)
err := saveChatState(openID, state.Procedure, state)
if err != nil {
log.Println("Error Cannot Save chat sate")
log.Println(err)
log.Println(state)
}
return return
} }



Loading…
Annulla
Salva