|
|
|
@@ -2,6 +2,7 @@ package main |
|
|
|
|
|
|
|
import ( |
|
|
|
"encoding/json" |
|
|
|
"errors" |
|
|
|
"io/ioutil" |
|
|
|
"log" |
|
|
|
"os" |
|
|
|
@@ -21,6 +22,8 @@ type chatState struct { |
|
|
|
Hint string `json:"Hint"` |
|
|
|
Message map[string]string `json:"Message"` //the description for receiving message |
|
|
|
} `json:"Receive"` |
|
|
|
|
|
|
|
Save map[string]string `json:"Save"` //the state save some data for later usage |
|
|
|
} |
|
|
|
|
|
|
|
//for individual state |
|
|
|
@@ -65,3 +68,63 @@ func deleteChatState(openID, procedure string) (err error) { |
|
|
|
err = os.Remove(path) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
//ValidationResult After input validation, what is the result |
|
|
|
type ValidationResult struct { |
|
|
|
accept bool |
|
|
|
Hint string |
|
|
|
Error string |
|
|
|
Warning string |
|
|
|
} |
|
|
|
|
|
|
|
//Validator function type for validating all wechat inputs |
|
|
|
type Validator func(s chatState) ValidationResult |
|
|
|
|
|
|
|
//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) |
|
|
|
|
|
|
|
//do the real concret work for processing the state |
|
|
|
err = processProcedureState(state) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
//resume a previous Procedure |
|
|
|
func resumeProcedure(procedure string) { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
func stopProcedure(procedure string) { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
func processProcedureState(state chatState) (err error) { |
|
|
|
log.Println(state) |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
type initProcedureFunction func(openid string) (initState chatState) |
|
|
|
|
|
|
|
func getProcedureInit(openID, procedure string) initProcedureFunction { |
|
|
|
initFunc := map[string]initProcedureFunction{ |
|
|
|
"TestDummy": nil, |
|
|
|
"TestEcho": initTestEcho, |
|
|
|
"GetBasicUserInfo": initGetBasicUserInfo, |
|
|
|
"GetEmailAddr": initGetBasicUserInfo, |
|
|
|
} |
|
|
|
return initFunc[procedure] |
|
|
|
} |
|
|
|
|
|
|
|
func initTestEcho(openid string) (r chatState) { |
|
|
|
r.Name = openid |
|
|
|
r.Expire = 0 |
|
|
|
return |
|
|
|
} |