Просмотр исходного кода

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 лет назад
Родитель
Сommit
3dde62d629
3 измененных файлов: 21 добавлений и 10 удалений
  1. +7
    -2
      chatState.go
  2. +1
    -1
      procedure.go
  3. +13
    -7
      server.go

+ 7
- 2
chatState.go Просмотреть файл

@@ -96,6 +96,11 @@ type ValidationResult struct {
//Validator function type for validating all wechat inputs
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 Просмотреть файл

@@ -99,6 +99,6 @@ func isInProc(openID string) (result bool, state chatState) {
}

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

+ 13
- 7
server.go Просмотреть файл

@@ -72,23 +72,29 @@ func answerWechatPost(w http.ResponseWriter, r *http.Request) {

//are we in an existing procedure
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 {
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)
reply = buildKfForwardMsg(openID, "")
kfSendTxt(openID, "未识别的命令,已转接校友会理事会,稍后答复您")
} else {
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")
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
}


Загрузка…
Отмена
Сохранить