package main import ( "errors" "log" "os" "time" ) //start a procedure func startProcedure(openID, procedure string) (err error) { //init procedure state init := getProcedureInit(openID, procedure) if init == nil { msg := "FATAL: cannot initialize procedure [" + procedure + "] " err = errors.New(msg) return } //init and get initial state state := init(openID) //save it setCurrentState(openID, procedure, state) //next is to waiting for user's input //which may or may not happen very soon return } //resume a previous Procedure's state func resumeProcedure(openID, procedure string) (err error) { state, err := getCurrentState(openID, procedure) if err != nil { return } //re-introduce what we are doing // showProcIntro(openID, peocedure) //tell user what has been achieved // showProcSumary(openID, procedure) return processProcedureState(state) } //finish a procedure, regardless its been finished or not //normally not finished normally func cleanProcedure(openID, procedure string) { path := getProcedurePath(openID, procedure) os.Remove(path) log.Println("Clearing [" + openID + "] @ [" + procedure + "]") //TODO: save session to "" procedure } func processProcedureState(state chatState) (err error) { //send what we need to send if isExpired(state.Expire) { return errors.New("State has expired " + state.Name) } //mark we have sent. //do we need input? waiting for input //if not, what is next state log.Println(state) return } func getProcedureInit(openID, procedure string) initProcFunc { initFunc := map[string]initProcFunc{ "TestDummy": nil, "TestEcho": initTestEcho, "GetBasicUserInfo": initGetBasicUserInfo, "GetEmailAddr": initGetBasicUserInfo, } return initFunc[procedure] } func initTestEcho(openid string) (r chatState) { r.Name = openid r.Expire = int32(time.Now().Unix() + 100) return } //are we inside a procedure, and not finished? func isInProc(openID string) (result bool, state chatState) { r, err := getCurrentSesssion(openID) if err != nil { return false, state } if isExpired(r.Expire) { return false, state } state, err = getCurrentState(openID, r.Procedure) if err != nil || isExpired(state.Expire) { return false, state } return true, state } //follow procedure, if there is any func serveProc(state chatState, input InWechatMsg) (next chatState) { return }