package main import ( "encoding/json" "io/ioutil" "log" "os" ) //chat state that we might be use for collecting infomation from user type chatState struct { Name string `json:"Name"` //state name Expire int32 `json:"Expire"` //unix timestamp when this state expire Send struct { //anything we need to send? Type string `json:"Type"` //what type of message Message map[string]string `json:"Message"` //the message to be sent,key value pair to describe the message } `json:"Send"` Receive struct { //anything we expect to receive Validator string `json:"Validator"` Hint string `json:"Hint"` Message map[string]string `json:"Message"` //the description for receiving message } `json:"Receive"` } //for individual state func getCurrentState(openID string, procedure string) (result chatState, err error) { path := getProcedurePath(openID, procedure) log.Printf("read state from %s\r\n", path) body, err := ioutil.ReadFile(path) if err != nil { //read file error if isFileExist(path) { log.Println("Error session reading " + path) } return //empty and expired session } err = json.Unmarshal(body, &result) if err != nil { log.Printf("Session Content [path=%s] not correct: ", path) log.Println(err) } //we don't check Expire, we give the caller full control on //how to deal wiht expired session return } func setCurrentState(openID, procedure string, state chatState) (newState chatState, err error) { j, err := json.Marshal(state) if err != nil { return } path := getProcedurePath(openID, procedure) err = ioutil.WriteFile(path, j, 0600) if err != nil { log.Println("write state error" + path) log.Println(err) return } newState = state return } func deleteChatState(openID, procedure string) (err error) { path := getProcedurePath(openID, procedure) err = os.Remove(path) return }