tg/cmd/test/main.go

182 lines
3.9 KiB
Go
Raw Normal View History

2023-07-03 17:22:45 +03:00
package main
2023-07-08 22:26:21 +03:00
import (
"log"
"os"
"strings"
2023-07-08 22:26:21 +03:00
"github.com/mojosa-software/got/src/tx"
2023-07-09 01:28:59 +03:00
)
2023-08-12 14:35:33 +03:00
type UserData struct {
Counter int
}
var startScreenButton = tx.NewButton("🏠 To the start screen").
ScreenChange("start")
2023-07-12 14:06:05 +03:00
var beh = tx.NewBehaviour().
2023-08-11 11:04:28 +03:00
// The function will be called every time
// the bot is started.
OnStartFunc(func(c *tx.A) {
2023-08-12 14:35:33 +03:00
c.V = &UserData{}
c.ChangeScreen("start")
}).WithKeyboards(
2023-08-11 11:04:28 +03:00
// Increment/decrement keyboard.
tx.NewKeyboard("inc/dec").Row(
tx.NewButton("+").ActionFunc(func(c *tx.A) {
2023-08-12 14:35:33 +03:00
d := c.V.(*UserData)
d.Counter++
c.Sendf("%d", d.Counter)
}),
tx.NewButton("-").ActionFunc(func(c *tx.A) {
2023-08-12 14:35:33 +03:00
d := c.V.(*UserData)
d.Counter--
c.Sendf("%d", d.Counter)
}),
).Row(
startScreenButton,
),
2023-08-11 11:04:28 +03:00
// The navigational keyboard.
tx.NewKeyboard("nav").Row(
tx.NewButton("Inc/Dec").ScreenChange("inc/dec"),
).Row(
tx.NewButton("Upper case").ScreenChange("upper-case"),
tx.NewButton("Lower case").ScreenChange("lower-case"),
).Row(
tx.NewButton("Send location").
WithSendLocation(true).
ActionFunc(func(c *tx.A) {
var err error
if c.U.Message.Location != nil {
l := c.U.Message.Location
err = c.Sendf(
"Longitude: %f\n"+
"Latitude: %f\n"+
"Heading: %d"+
"",
l.Longitude,
l.Latitude,
l.Heading,
)
} else {
err = c.Send("Somehow wrong location was sent")
}
if err != nil {
c.Send(err)
}
}),
),
2023-08-11 11:04:28 +03:00
tx.NewKeyboard("istart").Row(
tx.NewButton("GoT Github page").
2023-08-11 11:04:28 +03:00
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").
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"),
tx.NewScreen("inc/dec").
WithText(
2023-08-12 14:35:33 +03:00
"The screen shows how "+
"user separated data works "+
"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.A) {
2023-08-12 14:35:33 +03:00
d := c.V.(*UserData)
c.Sendf("Current counter value = %d", d.Counter)
}),
2023-08-11 11:04:28 +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-08-12 14:35:33 +03:00
).WithCommands(
tx.NewCommand("hello").
Desc("sends the 'Hello, World!' message back").
ActionFunc(func(c *tx.A) {
2023-08-12 14:35:33 +03:00
c.Send("Hello, World!")
}),
tx.NewCommand("read").
Desc("reads a string and sends it back").
ActionFunc(func(c *tx.A) {
2023-08-12 14:35:33 +03:00
c.Send("Type some text:")
msg, err := c.ReadTextMessage()
if err != nil {
return
}
c.Sendf("You typed %q", msg)
}),
)
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.A) {
2023-08-11 11:04:28 +03:00
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-08-13 15:37:36 +03:00
var gBeh = tx.NewGroupBehaviour().
InitFunc(func(a *tx.GA) {
}).
WithCommands(
tx.NewGroupCommand("hello").ActionFunc(func(a *tx.GA) {
a.Send("Hello, World!")
}),
tx.NewGroupCommand("mycounter").ActionFunc(func(a *tx.GA) {
d := a.GetSessionValue().(*UserData)
a.Sendf("Your counter value is %d", d.Counter)
}),
)
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-13 15:37:36 +03:00
bot, err := tx.NewBot(token)
if err != nil {
log.Panic(err)
}
2023-08-13 15:37:36 +03:00
bot = bot.
WithBehaviour(beh).
WithGroupBehaviour(gBeh)
2023-07-08 22:26:21 +03:00
bot.Debug = true
2023-07-08 22:26:21 +03:00
log.Printf("Authorized on account %s", bot.Self.UserName)
bot.Run()
}