Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

155 lignes
4.0KB

  1. package main
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "io/ioutil"
  6. "log"
  7. "os"
  8. )
  9. //chat state that we might be use for collecting infomation from user
  10. type chatState struct {
  11. //back pointer style information
  12. OpenID string `json:"OpenID"` //whom is this belongs to
  13. Procedure string `json:"Procedure"` //which procedure this belongs to
  14. //real state information
  15. Name string `json:"Name"` //state name
  16. Expire int32 `json:"Expire"` //unix timestamp when this state expire
  17. Send struct { //anything we need to send?
  18. Type string `json:"Type"` //what type of message
  19. Message map[string]string `json:"Message"` //the message to be sent,key value pair to describe the message
  20. } `json:"Send"`
  21. Receive struct { //anything we expect to receive
  22. Validator string `json:"Validator"`
  23. Hint string `json:"Hint"`
  24. Message map[string]string `json:"Message"` //the description for receiving message
  25. } `json:"Receive"`
  26. Save map[string]string `json:"Save"` //the state save some data for later usage
  27. }
  28. //for individual state
  29. func getCurrentState(openID string, procedure string) (result chatState, err error) {
  30. path := getProcedurePath(openID, procedure)
  31. log.Printf("read state from %s\r\n", path)
  32. body, err := ioutil.ReadFile(path)
  33. if err != nil { //read file error
  34. if isFileExist(path) {
  35. log.Println("Error session reading " + path)
  36. }
  37. return //empty and expired session
  38. }
  39. err = json.Unmarshal(body, &result)
  40. if err != nil {
  41. log.Printf("Session Content [path=%s] not correct: ", path)
  42. log.Println(err)
  43. }
  44. //we don't check Expire, we give the caller full control on
  45. //how to deal wiht expired session
  46. return
  47. }
  48. func setCurrentState(openID, procedure string, state chatState) (newState chatState, err error) {
  49. j, err := json.Marshal(state)
  50. if err != nil {
  51. return
  52. }
  53. path := getProcedurePath(openID, procedure)
  54. err = ioutil.WriteFile(path, j, 0600)
  55. if err != nil {
  56. log.Println("write state error" + path)
  57. log.Println(err)
  58. return
  59. }
  60. newState = state
  61. return
  62. }
  63. func deleteChatState(openID, procedure string) (err error) {
  64. path := getProcedurePath(openID, procedure)
  65. err = os.Remove(path)
  66. return
  67. }
  68. //ValidationResult After input validation, what is the result
  69. type ValidationResult struct {
  70. accept bool
  71. Hint string
  72. Error string
  73. Warning string
  74. }
  75. //Validator function type for validating all wechat inputs
  76. type Validator func(s chatState) ValidationResult
  77. //start a procedure
  78. func startProcedure(openID, procedure string) (err error) {
  79. //init procedure state
  80. init := getProcedureInit(openID, procedure)
  81. if init == nil {
  82. msg := "FATAL: cannot initialize procedure [" + procedure + "] "
  83. err = errors.New(msg)
  84. return
  85. }
  86. //init and get initial state
  87. state := init(openID)
  88. //do the real concret work for processing the state
  89. err = processProcedureState(state)
  90. return
  91. }
  92. //resume a previous Procedure's state
  93. func resumeProcedure(openID, procedure string) (err error) {
  94. state, err := getCurrentState(openID, procedure)
  95. if err != nil {
  96. return
  97. }
  98. return processProcedureState(state)
  99. }
  100. //finish a procedure, regardless its been finished or not
  101. //normally not finished normally
  102. func stopProcedure(openID, procedure string) {
  103. path := getProcedurePath(openID, procedure)
  104. os.Remove(path)
  105. log.Println("Clearing [" + openID + "] @ [" + procedure + "]")
  106. }
  107. func processProcedureState(state chatState) (err error) {
  108. //send what we need to send
  109. if isExpired(state.Expire) {
  110. return errors.New("State has expired " + stae.Name)
  111. }
  112. //mark we have sent.
  113. //do we need input? waiting for input
  114. //if not, what is next state
  115. log.Println(state)
  116. return
  117. }
  118. type initProcedureFunction func(openid string) (initState chatState)
  119. func getProcedureInit(openID, procedure string) initProcedureFunction {
  120. initFunc := map[string]initProcedureFunction{
  121. "TestDummy": nil,
  122. "TestEcho": initTestEcho,
  123. "GetBasicUserInfo": initGetBasicUserInfo,
  124. "GetEmailAddr": initGetBasicUserInfo,
  125. }
  126. return initFunc[procedure]
  127. }
  128. func initTestEcho(openid string) (r chatState) {
  129. r.Name = openid
  130. r.Expire = 0
  131. return
  132. }