2023-07-03 17:22:45 +03:00
|
|
|
package main
|
|
|
|
|
2023-07-08 22:26:21 +03:00
|
|
|
import (
|
2023-08-10 15:49:25 +03:00
|
|
|
"log"
|
|
|
|
"os"
|
2023-08-11 10:41:17 +03:00
|
|
|
"strings"
|
2023-07-08 22:26:21 +03:00
|
|
|
|
2023-08-10 15:49:25 +03:00
|
|
|
"github.com/mojosa-software/got/src/tx"
|
2023-07-09 01:28:59 +03:00
|
|
|
)
|
|
|
|
|
2023-08-11 10:41:17 +03:00
|
|
|
var startScreenButton = tx.NewButton().
|
|
|
|
WithText("🏠 To the start screen").
|
|
|
|
ScreenChange("start")
|
2023-07-12 14:06:05 +03:00
|
|
|
|
2023-08-10 15:49:25 +03:00
|
|
|
var beh = tx.NewBehaviour().
|
2023-08-11 11:04:28 +03:00
|
|
|
|
2023-08-11 10:41:17 +03:00
|
|
|
// The function will be called every time
|
|
|
|
// the bot is started.
|
2023-08-10 15:49:25 +03:00
|
|
|
OnStartFunc(func(c *tx.Context) {
|
2023-07-12 14:59:07 +03:00
|
|
|
c.V["counter"] = new(int)
|
|
|
|
c.ChangeScreen("start")
|
2023-08-10 15:49:25 +03:00
|
|
|
}).WithKeyboards(
|
2023-08-11 11:04:28 +03:00
|
|
|
|
2023-08-11 10:41:17 +03:00
|
|
|
// Increment/decrement keyboard.
|
|
|
|
tx.NewKeyboard("inc/dec").Row(
|
|
|
|
tx.NewButton().WithText("+").ActionFunc(func(c *tx.Context) {
|
|
|
|
counter := c.V["counter"].(*int)
|
|
|
|
*counter++
|
|
|
|
c.Sendf("%d", *counter)
|
|
|
|
}),
|
|
|
|
tx.NewButton().WithText("-").ActionFunc(func(c *tx.Context) {
|
|
|
|
counter := c.V["counter"].(*int)
|
|
|
|
*counter--
|
|
|
|
c.Sendf("%d", *counter)
|
|
|
|
}),
|
|
|
|
).Row(
|
|
|
|
startScreenButton,
|
|
|
|
),
|
2023-08-11 11:04:28 +03:00
|
|
|
|
2023-08-11 10:41:17 +03:00
|
|
|
// The navigational keyboard.
|
|
|
|
tx.NewKeyboard("nav").Row(
|
|
|
|
tx.NewButton().WithText("Inc/Dec").ScreenChange("inc/dec"),
|
|
|
|
).Row(
|
|
|
|
tx.NewButton().WithText("Upper case").ScreenChange("upper-case"),
|
|
|
|
tx.NewButton().WithText("Lower case").ScreenChange("lower-case"),
|
|
|
|
),
|
2023-08-11 11:04:28 +03:00
|
|
|
|
|
|
|
tx.NewKeyboard("istart").Row(
|
|
|
|
tx.NewButton().WithText("GoT Github page").
|
|
|
|
WithUrl("https://github.com/mojosa-software/got"),
|
|
|
|
),
|
|
|
|
|
2023-08-11 10:41:17 +03:00
|
|
|
// The keyboard to return to the start screen.
|
|
|
|
tx.NewKeyboard("nav-start").Row(
|
|
|
|
startScreenButton,
|
|
|
|
),
|
2023-08-10 15:49:25 +03:00
|
|
|
).WithScreens(
|
2023-08-11 10:41:17 +03:00
|
|
|
tx.NewScreen("start").
|
2023-08-11 11:04:28 +03:00
|
|
|
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"),
|
|
|
|
|
2023-08-11 10:41:17 +03:00
|
|
|
tx.NewScreen("inc/dec").
|
|
|
|
WithText(
|
|
|
|
"The screen shows how"+
|
|
|
|
"user separated data works"+
|
2023-08-11 11:04:28 +03:00
|
|
|
"by saving the counter for each of users"+
|
|
|
|
"separately.",
|
2023-08-11 10:41:17 +03:00
|
|
|
).
|
|
|
|
Keyboard("inc/dec").
|
|
|
|
// The function will be called when reaching the screen.
|
|
|
|
ActionFunc(func(c *tx.Context) {
|
|
|
|
counter := c.V["counter"].(*int)
|
2023-08-11 11:04:28 +03:00
|
|
|
c.Sendf("Current counter value = %d", *counter)
|
2023-08-11 10:41:17 +03:00
|
|
|
}),
|
2023-08-11 11:04:28 +03:00
|
|
|
|
2023-08-11 10:41:17 +03:00
|
|
|
tx.NewScreen("upper-case").
|
|
|
|
WithText("Type text and the bot will send you the upper case version to you").
|
|
|
|
Keyboard("nav-start").
|
2023-08-11 11:04:28 +03:00
|
|
|
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)),
|
2023-07-12 02:02:33 +03:00
|
|
|
)
|
2023-07-09 01:28:59 +03:00
|
|
|
|
2023-08-11 11:04:28 +03:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-03 17:22:45 +03:00
|
|
|
func main() {
|
2023-07-08 22:26:21 +03:00
|
|
|
token := os.Getenv("BOT_TOKEN")
|
|
|
|
|
2023-08-10 15:49:25 +03:00
|
|
|
bot, err := tx.NewBot(token, beh, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2023-07-08 22:26:21 +03:00
|
|
|
|
2023-08-10 15:49:25 +03:00
|
|
|
bot.Debug = true
|
2023-07-08 22:26:21 +03:00
|
|
|
|
2023-08-10 15:49:25 +03:00
|
|
|
log.Printf("Authorized on account %s", bot.Self.UserName)
|
|
|
|
bot.Run()
|
|
|
|
}
|