diff --git a/cmd/test/main.go b/cmd/test/main.go index 5dc3038..8cd0828 100644 --- a/cmd/test/main.go +++ b/cmd/test/main.go @@ -13,12 +13,14 @@ var startScreenButton = tx.NewButton(). ScreenChange("start") var beh = tx.NewBehaviour(). + // The function will be called every time // the bot is started. OnStartFunc(func(c *tx.Context) { c.V["counter"] = new(int) c.ChangeScreen("start") }).WithKeyboards( + // Increment/decrement keyboard. tx.NewKeyboard("inc/dec").Row( tx.NewButton().WithText("+").ActionFunc(func(c *tx.Context) { @@ -34,6 +36,7 @@ var beh = tx.NewBehaviour(). ).Row( startScreenButton, ), + // The navigational keyboard. tx.NewKeyboard("nav").Row( tx.NewButton().WithText("Inc/Dec").ScreenChange("inc/dec"), @@ -41,40 +44,70 @@ var beh = tx.NewBehaviour(). tx.NewButton().WithText("Upper case").ScreenChange("upper-case"), tx.NewButton().WithText("Lower case").ScreenChange("lower-case"), ), + + tx.NewKeyboard("istart").Row( + tx.NewButton().WithText("GoT Github page"). + WithUrl("https://github.com/mojosa-software/got"), + ), + // The keyboard to return to the start screen. tx.NewKeyboard("nav-start").Row( startScreenButton, ), ).WithScreens( tx.NewScreen("start"). - WithText("The bot started!"). - Keyboard("nav"), + WithText( + "The bot started!"+ + " The bot is supposed to provide basic"+ + " understand of how the API works, so just"+ + " horse around a bit to guess everything out"+ + " by yourself!", + ).Keyboard("nav"). + IKeyboard("istart"), + tx.NewScreen("inc/dec"). WithText( "The screen shows how"+ "user separated data works"+ - "by saving the counter for each of them", + "by saving the counter for each of users"+ + "separately.", ). Keyboard("inc/dec"). // The function will be called when reaching the screen. ActionFunc(func(c *tx.Context) { counter := c.V["counter"].(*int) - c.Sendf("Current counter value equals %d", *counter) + c.Sendf("Current counter value = %d", *counter) }), + tx.NewScreen("upper-case"). WithText("Type text and the bot will send you the upper case version to you"). Keyboard("nav-start"). - ActionFunc(func(c *tx.Context) { - for { - s, err := c.ReadTextMessage() - if err == tx.NotAvailableErr { - break - } - c.Sendf("%s", strings.ToUpper(s)) - } - }), + ActionFunc(mutateMessage(strings.ToUpper)), + + tx.NewScreen("lower-case"). + WithText("Type text and the bot will send you the lower case version"). + Keyboard("nav-start"). + ActionFunc(mutateMessage(strings.ToLower)), ) +func mutateMessage(fn func(string) string) tx.ActionFunc { + return func(c *tx.Context) { + for { + msg, err := c.ReadTextMessage() + if err == tx.NotAvailableErr { + break + } else if err != nil { + panic(err) + } + + err = c.Sendf("%s", fn(msg)) + if err != nil { + panic(err) + } + } + } +} + func main() { token := os.Getenv("BOT_TOKEN") diff --git a/src/tx/context.go b/src/tx/context.go index e112b1b..cbe889d 100644 --- a/src/tx/context.go +++ b/src/tx/context.go @@ -44,10 +44,7 @@ func (c *Context) handleUpdateChan(updates chan *Update) { continue } - if !ok { - } - - if ok { + if ok && btn.Action != nil { c.run(btn.Action) } } else if u.CallbackQuery != nil { diff --git a/src/tx/keyboard.go b/src/tx/keyboard.go index 0805164..96b2668 100644 --- a/src/tx/keyboard.go +++ b/src/tx/keyboard.go @@ -38,12 +38,16 @@ func NewKeyboard(id KeyboardId) *Keyboard { // Adds a new button row to the current keyboard. func (kbd *Keyboard) Row(btns ...*Button) *Keyboard { + // For empty row. We do not need that. + if len(btns) < 1 { + return kbd + } kbd.Rows = append(kbd.Rows, btns) return kbd } // Convert the Keyboard to the Telegram API type. -func (kbd *Keyboard) toTelegram() apix.ReplyKeyboardMarkup { +func (kbd *Keyboard) ToTelegram() apix.ReplyKeyboardMarkup { rows := [][]apix.KeyboardButton{} for _, row := range kbd.Rows { buttons := []apix.KeyboardButton{} @@ -56,7 +60,7 @@ func (kbd *Keyboard) toTelegram() apix.ReplyKeyboardMarkup { return apix.NewReplyKeyboard(rows...) } -func (kbd *Keyboard) toTelegramInline() apix.InlineKeyboardMarkup { +func (kbd *Keyboard) ToTelegramInline() apix.InlineKeyboardMarkup { rows := [][]apix.InlineKeyboardButton{} for _, row := range kbd.Rows { buttons := []apix.InlineKeyboardButton{} diff --git a/src/tx/screen.go b/src/tx/screen.go index b579f8a..9ee0278 100644 --- a/src/tx/screen.go +++ b/src/tx/screen.go @@ -80,7 +80,7 @@ func (s *Screen) Render(c *Context) error { if !ok { return KeyboardNotExistErr } - msg.ReplyMarkup = kbd.toTelegramInline() + msg.ReplyMarkup = kbd.ToTelegramInline() } _, err := c.B.Send(msg) @@ -101,7 +101,7 @@ func (s *Screen) Render(c *Context) error { if !ok { return KeyboardNotExistErr } - tkbd = kbd.toTelegram() + tkbd = kbd.ToTelegram() } msg.ReplyMarkup = tkbd