| //chat state that we might be use for collecting infomation from user | //chat state that we might be use for collecting infomation from user | ||||
| type chatState struct { | type chatState struct { | ||||
| //back pointer style information | |||||
| OpenID string `json:"OpenID"` //whom is this belongs to | |||||
| Procedure string `json:"Procedure"` //which procedure this belongs to | |||||
| //real state information | |||||
| Name string `json:"Name"` //state name | Name string `json:"Name"` //state name | ||||
| Expire int32 `json:"Expire"` //unix timestamp when this state expire | Expire int32 `json:"Expire"` //unix timestamp when this state expire | ||||
| Send struct { //anything we need to send? | Send struct { //anything we need to send? | ||||
| //do the real concret work for processing the state | //do the real concret work for processing the state | ||||
| err = processProcedureState(state) | err = processProcedureState(state) | ||||
| return | return | ||||
| } | } | ||||
| //resume a previous Procedure | |||||
| func resumeProcedure(procedure string) { | |||||
| //resume a previous Procedure's state | |||||
| func resumeProcedure(openID, procedure string) (err error) { | |||||
| state, err := getCurrentState(openID, procedure) | |||||
| if err != nil { | |||||
| return | |||||
| } | |||||
| return processProcedureState(state) | |||||
| } | } | ||||
| func stopProcedure(procedure string) { | |||||
| //finish a procedure, regardless its been finished or not | |||||
| //normally not finished normally | |||||
| func stopProcedure(openID, procedure string) { | |||||
| path := getProcedurePath(openID, procedure) | |||||
| os.Remove(path) | |||||
| log.Println("Clearing [" + openID + "] @ [" + procedure + "]") | |||||
| } | } | ||||
| func processProcedureState(state chatState) (err error) { | func processProcedureState(state chatState) (err error) { | ||||
| //send what we need to send | |||||
| if isExpired(state.Expire) { | |||||
| return errors.New("State has expired " + stae.Name) | |||||
| } | |||||
| //mark we have sent. | |||||
| //do we need input? waiting for input | |||||
| //if not, what is next state | |||||
| log.Println(state) | log.Println(state) | ||||
| return | return | ||||
| } | } |