Fixed lack of checks for nil.

This commit is contained in:
Andrey Parhomenko 2023-08-11 11:04:28 +03:00
parent cd965fd811
commit ee28fcf2f7
4 changed files with 55 additions and 21 deletions

View file

@ -13,12 +13,14 @@ var startScreenButton = tx.NewButton().
ScreenChange("start") ScreenChange("start")
var beh = tx.NewBehaviour(). var beh = tx.NewBehaviour().
// The function will be called every time // The function will be called every time
// the bot is started. // the bot is started.
OnStartFunc(func(c *tx.Context) { OnStartFunc(func(c *tx.Context) {
c.V["counter"] = new(int) c.V["counter"] = new(int)
c.ChangeScreen("start") c.ChangeScreen("start")
}).WithKeyboards( }).WithKeyboards(
// Increment/decrement keyboard. // Increment/decrement keyboard.
tx.NewKeyboard("inc/dec").Row( tx.NewKeyboard("inc/dec").Row(
tx.NewButton().WithText("+").ActionFunc(func(c *tx.Context) { tx.NewButton().WithText("+").ActionFunc(func(c *tx.Context) {
@ -34,6 +36,7 @@ var beh = tx.NewBehaviour().
).Row( ).Row(
startScreenButton, startScreenButton,
), ),
// The navigational keyboard. // The navigational keyboard.
tx.NewKeyboard("nav").Row( tx.NewKeyboard("nav").Row(
tx.NewButton().WithText("Inc/Dec").ScreenChange("inc/dec"), 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("Upper case").ScreenChange("upper-case"),
tx.NewButton().WithText("Lower case").ScreenChange("lower-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. // The keyboard to return to the start screen.
tx.NewKeyboard("nav-start").Row( tx.NewKeyboard("nav-start").Row(
startScreenButton, startScreenButton,
), ),
).WithScreens( ).WithScreens(
tx.NewScreen("start"). tx.NewScreen("start").
WithText("The bot started!"). WithText(
Keyboard("nav"), "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"). tx.NewScreen("inc/dec").
WithText( WithText(
"The screen shows how"+ "The screen shows how"+
"user separated data works"+ "user separated data works"+
"by saving the counter for each of them", "by saving the counter for each of users"+
"separately.",
). ).
Keyboard("inc/dec"). Keyboard("inc/dec").
// The function will be called when reaching the screen. // The function will be called when reaching the screen.
ActionFunc(func(c *tx.Context) { ActionFunc(func(c *tx.Context) {
counter := c.V["counter"].(*int) counter := c.V["counter"].(*int)
c.Sendf("Current counter value equals %d", *counter) c.Sendf("Current counter value = %d", *counter)
}), }),
tx.NewScreen("upper-case"). tx.NewScreen("upper-case").
WithText("Type text and the bot will send you the upper case version to you"). WithText("Type text and the bot will send you the upper case version to you").
Keyboard("nav-start"). Keyboard("nav-start").
ActionFunc(func(c *tx.Context) { ActionFunc(mutateMessage(strings.ToUpper)),
for {
s, err := c.ReadTextMessage() tx.NewScreen("lower-case").
if err == tx.NotAvailableErr { WithText("Type text and the bot will send you the lower case version").
break Keyboard("nav-start").
} ActionFunc(mutateMessage(strings.ToLower)),
c.Sendf("%s", strings.ToUpper(s))
}
}),
) )
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() { func main() {
token := os.Getenv("BOT_TOKEN") token := os.Getenv("BOT_TOKEN")

View file

@ -44,10 +44,7 @@ func (c *Context) handleUpdateChan(updates chan *Update) {
continue continue
} }
if !ok { if ok && btn.Action != nil {
}
if ok {
c.run(btn.Action) c.run(btn.Action)
} }
} else if u.CallbackQuery != nil { } else if u.CallbackQuery != nil {

View file

@ -38,12 +38,16 @@ func NewKeyboard(id KeyboardId) *Keyboard {
// Adds a new button row to the current keyboard. // Adds a new button row to the current keyboard.
func (kbd *Keyboard) Row(btns ...*Button) *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) kbd.Rows = append(kbd.Rows, btns)
return kbd return kbd
} }
// Convert the Keyboard to the Telegram API type. // Convert the Keyboard to the Telegram API type.
func (kbd *Keyboard) toTelegram() apix.ReplyKeyboardMarkup { func (kbd *Keyboard) ToTelegram() apix.ReplyKeyboardMarkup {
rows := [][]apix.KeyboardButton{} rows := [][]apix.KeyboardButton{}
for _, row := range kbd.Rows { for _, row := range kbd.Rows {
buttons := []apix.KeyboardButton{} buttons := []apix.KeyboardButton{}
@ -56,7 +60,7 @@ func (kbd *Keyboard) toTelegram() apix.ReplyKeyboardMarkup {
return apix.NewReplyKeyboard(rows...) return apix.NewReplyKeyboard(rows...)
} }
func (kbd *Keyboard) toTelegramInline() apix.InlineKeyboardMarkup { func (kbd *Keyboard) ToTelegramInline() apix.InlineKeyboardMarkup {
rows := [][]apix.InlineKeyboardButton{} rows := [][]apix.InlineKeyboardButton{}
for _, row := range kbd.Rows { for _, row := range kbd.Rows {
buttons := []apix.InlineKeyboardButton{} buttons := []apix.InlineKeyboardButton{}

View file

@ -80,7 +80,7 @@ func (s *Screen) Render(c *Context) error {
if !ok { if !ok {
return KeyboardNotExistErr return KeyboardNotExistErr
} }
msg.ReplyMarkup = kbd.toTelegramInline() msg.ReplyMarkup = kbd.ToTelegramInline()
} }
_, err := c.B.Send(msg) _, err := c.B.Send(msg)
@ -101,7 +101,7 @@ func (s *Screen) Render(c *Context) error {
if !ok { if !ok {
return KeyboardNotExistErr return KeyboardNotExistErr
} }
tkbd = kbd.toTelegram() tkbd = kbd.ToTelegram()
} }
msg.ReplyMarkup = tkbd msg.ReplyMarkup = tkbd