Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

107 rindas
2.3KB

  1. package main
  2. import (
  3. "errors"
  4. "log"
  5. "os"
  6. "time"
  7. )
  8. //start a procedure
  9. func startProcedure(openID, procedure string) (err error) {
  10. //init procedure state
  11. init := getProcedureInit(openID, procedure)
  12. if init == nil {
  13. msg := "FATAL: cannot initialize procedure [" + procedure + "] "
  14. err = errors.New(msg)
  15. return
  16. }
  17. //init and get initial state
  18. state := init(openID)
  19. //save it
  20. setCurrentState(openID, procedure, state)
  21. //next is to waiting for user's input
  22. //which may or may not happen very soon
  23. return
  24. }
  25. //resume a previous Procedure's state
  26. func resumeProcedure(openID, procedure string) (err error) {
  27. state, err := getCurrentState(openID, procedure)
  28. if err != nil {
  29. return
  30. }
  31. //re-introduce what we are doing
  32. // showProcIntro(openID, peocedure)
  33. //tell user what has been achieved
  34. // showProcSumary(openID, procedure)
  35. return processProcedureState(state)
  36. }
  37. //finish a procedure, regardless its been finished or not
  38. //normally not finished normally
  39. func cleanProcedure(openID, procedure string) {
  40. path := getProcedurePath(openID, procedure)
  41. os.Remove(path)
  42. log.Println("Clearing [" + openID + "] @ [" + procedure + "]")
  43. //TODO: save session to "" procedure
  44. }
  45. func processProcedureState(state chatState) (err error) {
  46. //send what we need to send
  47. if isExpired(state.Expire) {
  48. return errors.New("State has expired " + state.Name)
  49. }
  50. //mark we have sent.
  51. //do we need input? waiting for input
  52. //if not, what is next state
  53. log.Println(state)
  54. return
  55. }
  56. func getProcedureInit(openID, procedure string) initProcFunc {
  57. initFunc := map[string]initProcFunc{
  58. "TestDummy": nil,
  59. "TestEcho": initTestEcho,
  60. "GetBasicUserInfo": initGetBasicUserInfo,
  61. "GetEmailAddr": initGetBasicUserInfo,
  62. }
  63. return initFunc[procedure]
  64. }
  65. func initTestEcho(openid string) (r chatState) {
  66. r.Name = openid
  67. r.Expire = int32(time.Now().Unix() + 100)
  68. return
  69. }
  70. //are we inside a procedure, and not finished?
  71. func isInProc(openID string) (result bool, state chatState) {
  72. r, err := getCurrentSesssion(openID)
  73. if err != nil {
  74. return false, state
  75. }
  76. if isExpired(r.Expire) {
  77. return false, state
  78. }
  79. state, err = getCurrentState(openID, r.Procedure)
  80. if err != nil || isExpired(state.Expire) {
  81. return false, state
  82. }
  83. return true, state
  84. }
  85. //follow procedure, if there is any
  86. func serveProc(state chatState, input InWechatMsg) (next chatState) {
  87. return
  88. }