Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

105 Zeilen
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. }
  44. func processProcedureState(state chatState) (err error) {
  45. //send what we need to send
  46. if isExpired(state.Expire) {
  47. return errors.New("State has expired " + state.Name)
  48. }
  49. //mark we have sent.
  50. //do we need input? waiting for input
  51. //if not, what is next state
  52. log.Println(state)
  53. return
  54. }
  55. func getProcedureInit(openID, procedure string) initProcFunc {
  56. initFunc := map[string]initProcFunc{
  57. "TestDummy": nil,
  58. "TestEcho": initTestEcho,
  59. "GetBasicUserInfo": initGetBasicUserInfo,
  60. "GetEmailAddr": initGetBasicUserInfo,
  61. }
  62. return initFunc[procedure]
  63. }
  64. func initTestEcho(openid string) (r chatState) {
  65. r.Name = openid
  66. r.Expire = int32(time.Now().Unix() + 100)
  67. return
  68. }
  69. //are we inside a procedure, and not finished?
  70. func isInProc(openID string) (result bool, state chatState) {
  71. r, err := getCurrentSesssion(openID)
  72. if err != nil {
  73. return false, state
  74. }
  75. if isExpired(r.Expire) {
  76. return false, state
  77. }
  78. state, err = getCurrentState(openID, r.Procedure)
  79. if err != nil || isExpired(state.Expire) {
  80. return false, state
  81. }
  82. return true, state
  83. }
  84. //follow procedure, if there is any
  85. func serveProc(openID string, input InWechatMsg) (next chatState) {
  86. return
  87. }