Skip to content

Conversation

@rabikor
Copy link

@rabikor rabikor commented Oct 11, 2024

The try to implement cache contract like was described in issue #288

Let's discuss it

@rabikor rabikor changed the title flow: initial functional flow: reformated fsm Oct 11, 2024
@rabikor
Copy link
Author

rabikor commented Nov 26, 2024

@demget what do you think? is it interesting to Telebot? I need a reason to accomplish all the described changes.

@rabikor rabikor mentioned this pull request May 23, 2025
@rabikor
Copy link
Author

rabikor commented May 25, 2025

Below is a minimal working flow example that you can use to see how it works:

testFlow := b.BeginFlow("test", func(c tele.Context) error { return c.Send("Input your firstname:") })

testFlow.OnUpdate(tele.OnText, "test.firstname", func(c tele.Context) error {
	// actions

	return c.Send("Thank you. Input your lastname:")
})

testFlow.OnUpdate(tele.OnText, "test.lastname", func(c tele.Context) error {
	// actions

	return c.Send("Thank you. Input your age:")
})

testFlow.OnUpdate(tele.OnText, "test.age", func(c tele.Context) error {
	age, err := strconv.Atoi(c.Text())
	if err != nil {
		c.Set("flow:state", tele.FlowRepeat)
		return c.Send("Please input a valid number for age:")
	}

	if age < 18 {
		c.Set("flow:state", tele.FlowRepeat)
		defer c.Send("Input your age:")
		return c.Send("You're too young. Let's go back.")
	}

	// actions

	c.Set("flow:state", tele.FlowEnd)
	return c.Send("All set. Thank you!")
})

testFlow.Transite("test", "test.firstname", func(c tele.Context) bool { return true })
testFlow.Transite("test.firstname", "test.lastname", func(c tele.Context) bool { return true })
testFlow.Transite("test.lastname", "test.age", func(c tele.Context) bool { return true })

b.HandleFlow("/test", testFlow)
b.Start()
Image

@rabikor
Copy link
Author

rabikor commented May 25, 2025

more harder example:

Below is a minimal working flow example that you can use to see how it works:

        f := b.BeginFlow("start", func(c tele.Context) error {
		return c.Send(b.Text(c, "welcome.begin", c.Sender().FirstName), b.Markup(c, "start"))
	})

	f.OnUpdate(tele.OnCallback, "lang.choose", func(c tele.Context) error {
		return c.Send(b.Text(c, "lang.choose"), b.Markup(c, "lang"))
	})
	f.OnUpdate(tele.OnCallback, "lang.chosen", func(c tele.Context) error {
		defer b.Markup(c, "currency")

		lang := c.Data()

		if locale, _ := b.Locale(c); locale == lang {
			return c.Respond()
		}

		b.SetLocale(c, lang)

		return c.Respond()
	})
	f.Handle("currency.choose", func(c tele.Context) error {
		return c.Send(b.Text(c, "currency.choose"), b.Markup(c, "currency"))
	})
	f.OnUpdate(tele.OnCallback, "currency.chosen", func(c tele.Context) error {
		return c.Respond()
	})
	f.Handle("configured", func(c tele.Context) error {
		defer c.Send(b.Text(c, "welcome.finish"))
		return c.Send(b.Text(c, "welcome.info"))
	})

	f.Transite("start", "lang.choose", tele.NoConditionTransition)
	f.Transite("lang.choose", "lang.chosen", tele.NoConditionTransition)
	f.Transite("lang.chosen", "currency.choose", tele.NoConditionTransition)
	f.Transite("currency.choose", "currency.chosen", tele.NoConditionTransition)
	f.Transite("currency.chosen", "configured", tele.NoConditionTransition)

telegram-cloud-photo-size-2-5303252006389016704-y

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