This commit is contained in:
Andrey Parhomenko 2023-09-22 12:59:39 +03:00
parent 4737464273
commit 3913979bc9
2 changed files with 20 additions and 12 deletions

View file

@ -79,7 +79,7 @@ var (
tg.NewButton("Mutate messages").Go("/mutate-messages"), tg.NewButton("Mutate messages").Go("/mutate-messages"),
).Row( ).Row(
tg.NewButton("Send location").Go("/send-location"), tg.NewButton("Send location").Go("/send-location"),
).Reply().WithOneTime(true) ).Reply()
sendLocationKeyboard = tg.NewKeyboard().Row( sendLocationKeyboard = tg.NewKeyboard().Row(
tg.NewButton("Send location"). tg.NewButton("Send location").
@ -130,14 +130,13 @@ WithInitFunc(func(c *tg.Context) {
tg.NewButton("Lower case").Go("lower-case"), tg.NewButton("Lower case").Go("lower-case"),
).Row( ).Row(
backButton, backButton,
).Reply().WithOneTime(true).Widget( ).Reply().Widget(
"Choose the function to mutate string", "Choose the function to mutate string",
), ),
), ),
tg.NewNode( tg.NewNode(
"upper-case", tg.NewPage().WithReply( "upper-case", tg.NewPage().WithReply(
backKeyboard.Reply(). backKeyboard.Reply().
WithOneTime(true).
Widget( Widget(
"Type a string and the bot will convert it to upper case", "Type a string and the bot will convert it to upper case",
), ),
@ -148,7 +147,6 @@ WithInitFunc(func(c *tg.Context) {
tg.NewNode( tg.NewNode(
"lower-case", tg.NewPage().WithReply( "lower-case", tg.NewPage().WithReply(
backKeyboard.Reply(). backKeyboard.Reply().
WithOneTime(true).
Widget( Widget(
"Type a string and the bot will convert it to lower case", "Type a string and the bot will convert it to lower case",
), ),
@ -201,14 +199,10 @@ WithInitFunc(func(c *tg.Context) {
tg.NewCommand("read"). tg.NewCommand("read").
Desc("reads a string and sends it back"). Desc("reads a string and sends it back").
WithWidget( WithWidget(
tg.NewTextMessageRead( tg.Func(func(c *tg.Context){
tg.Func(func(c *tg.Context){ str := c.ReadString("Type a string and I will send it back")
c.Sendf("Type a string and it will send it back") c.Sendf2("You typed `%s`", str)
}), }),
tg.Func(func(c *tg.Context){
c.Sendf("You typed %q", c.Message.Text)
}),
),
), ),
tg.NewCommand("image"). tg.NewCommand("image").
Desc("sends a sample image"). Desc("sends a sample image").

View file

@ -204,6 +204,20 @@ func (c *Context) RunWidget(widget Widget, args ...any) *UpdateChan {
return updates return updates
} }
// Simple way to read strings for widgets.
func (c *Context) ReadString(pref string, args ...any) string {
var text string
c.Sendf(pref, args...)
for u := range c.Input() {
if u.Message == nil {
continue
}
text = u.Message.Text
break
}
return text
}
// Change screen to the previous. // Change screen to the previous.
// To get to the parent screen use GoUp. // To get to the parent screen use GoUp.
func (c *Context) GoPrev() { func (c *Context) GoPrev() {