Skip to content

Conversation

@hellocashmere
Copy link

P.S: I used #657 as a basis.

In the basic version, I did not like that Flow = tele.HandlerFunc:

var email, password string
err := flowBus.Flow(
	"/start",
	flow.New().
                OnEachStep(loggingEachStep).
		Next(flow.NewStep(sendUserMessage("Enter email:")).Assign(TextAssigner(&email))).
		Next(flow.NewStep(sendUserMessage("Enter password:")).Assign(TextAssigner(&password))).
		Then(registerUser).
		Catch(flowFailed),
)

I would like to see the steps described inside the tele.HandlerFunc:

bot.Handle("/signup", func(c tele.Context) error {
		ctx := context.Background()

		f := bus.Flow("signup", 1*time.Minute)

		var username string

		f.OnText(func(state *teleflow.State) error {
			return state.Context.EditOrSend("Enter username:")
		}).Assign(func(state *teleflow.State) error {
			username = state.Context.Message().Text
			return nil
		})

		f.OnText(func(state *teleflow.State) error {
			return state.Context.EditOrSend("Enter password:")
		}).Ok(func(state *teleflow.State) error {
			log.Println("username:", username)
			log.Println("password:", state.Context.Message().Text)

			return nil
		})

		return bus.Start(ctx, c, f)
	})

Why is this option better in my opinion?
In all my bots, I have a common Handlers structure that contains methods for registration, deposits, etc:

type Handler struct {
	userService    *service.UserService
	orderService   *service.OrderService
	depositService *service.DepositService
	
	bus flow.Bus
}

func (h *Handler) SignUp() tele.HandlerFunc {
	return func(c tele.Context) error {
		// ...
	}
}

func (h *Handler) DepositByUserID() tele.HandlerFunc {
	return func(c tele.Context) error {
		// ...
	}
}

Therefore, I think my option is preferable, I want to thank @shindakioku for his code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant