Fixed lack of checks for nil.
This commit is contained in:
parent
cd965fd811
commit
ee28fcf2f7
4 changed files with 55 additions and 21 deletions
|
@ -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")
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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{}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue