From 3bbfbd7509a14f50b9515f627e1a5d77f010e890 Mon Sep 17 00:00:00 2001 From: Patrick Peng Sun Date: Sat, 27 May 2017 11:47:21 +1000 Subject: [PATCH] init a procedure architecture designed & tested. --- chatState.go | 63 +++++++++++++++++++++++++++++++++++++++++ chatState_test.go | 7 +++++ procGetBasicUserInfo.go | 51 +++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+) create mode 100644 procGetBasicUserInfo.go diff --git a/chatState.go b/chatState.go index e8cf904..a5fbe68 100644 --- a/chatState.go +++ b/chatState.go @@ -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 +} diff --git a/chatState_test.go b/chatState_test.go index b8a4cf9..24d4185 100644 --- a/chatState_test.go +++ b/chatState_test.go @@ -40,3 +40,10 @@ func TestChatState(t *testing.T) { AssertEqual(t, err, nil, "delete chatState should be good") } + +func TestStartProcedure(t *testing.T) { + s := startProcedure("id", "TestDummy") + AssertEqual(t, s != nil, true, "TestDummy should return error") + s = startProcedure("id", "TestEcho") + AssertEqual(t, s, nil, "TestEqual should return success") +} diff --git a/procGetBasicUserInfo.go b/procGetBasicUserInfo.go new file mode 100644 index 0000000..483d97c --- /dev/null +++ b/procGetBasicUserInfo.go @@ -0,0 +1,51 @@ +package main + +//procedure 000 +//available state +var statesGetBasicUserInfo = map[string]chatState{ + "Intro": chatState{}, + "AskName": chatState{}, + "AskEnglishName": chatState{}, + "AskEmail": chatState{}, + "AskMobileNumber": chatState{}, + "AskProfilePhoto": chatState{}, + "Done": chatState{}, +} + +var validateGetBasicUserInfo = map[string]Validator{ + "validateChineseName": validateChineseName, +} + +func proc000AskName(openid string) { + s := chatState{} + s.Name = "AskName" + s.Expire = 300 //5 minutes + s.Save = map[string]string{} //clear + s.Send.Message["q"] = "请输入您的真实中文名,没有请填写 ”无“ " + s.Receive.Validator = "validateChineseName" +} + +func validateChineseName(s chatState) (r ValidationResult) { + r.accept = true + r.Error = "" + + input := s.Receive.Message["name"] + r.Hint = "通常中文名只有三个字或者四个字,比如 王更新,诸葛亮,司马相如,慕容白雪" + if len(input) >= 10 { + r.accept = false + r.Error = "您的名字过长" + } + if len(input) <= 1 { + r.accept = false + r.Error = "您的名字很萌,就是有点太短了" + } + + if len(input) >= 5 { + r.Warning = "您的名字有点长,可以勉强接收" + } + return +} + +func initGetBasicUserInfo(openid string) (initState chatState) { + return +}