tg/cmd/test/main.go

229 lines
5.2 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
2023-08-19 09:12:26 +03:00
"github.com/mojosa-software/got/tg"
2023-07-09 01:28:59 +03:00
)
type BotData struct {
Name string
}
2023-08-12 14:35:33 +03:00
type UserData struct {
Counter int
}
2023-08-15 16:02:14 +03:00
var (
2023-08-19 09:12:26 +03:00
startScreenButton = tg.NewButton("🏠 To the start screen").
2023-08-15 16:02:14 +03:00
ScreenChange("start")
2023-07-12 14:06:05 +03:00
incDecKeyboard = tg.NewInline().Row(
2023-08-19 09:12:26 +03:00
tg.NewButton("+").ActionFunc(func(c *tg.Context) {
d := c.Session.Value.(*UserData)
2023-08-12 14:35:33 +03:00
d.Counter++
c.Sendf("%d", d.Counter)
}),
2023-08-19 09:12:26 +03:00
tg.NewButton("-").ActionFunc(func(c *tg.Context) {
d := c.Session.Value.(*UserData)
2023-08-12 14:35:33 +03:00
d.Counter--
c.Sendf("%d", d.Counter)
}),
).Row(
startScreenButton,
2023-08-15 16:02:14 +03:00
)
2023-08-11 11:04:28 +03:00
navKeyboard = tg.NewReply().
2023-08-15 16:02:14 +03:00
WithOneTime(true).
Row(
2023-08-19 09:12:26 +03:00
tg.NewButton("Inc/Dec").ScreenChange("inc/dec"),
2023-08-15 16:02:14 +03:00
).Row(
2023-08-19 09:12:26 +03:00
tg.NewButton("Upper case").ScreenChange("upper-case"),
tg.NewButton("Lower case").ScreenChange("lower-case"),
).Row(
2023-08-19 09:12:26 +03:00
tg.NewButton("Send location").ScreenChange("send-location"),
2023-08-15 16:02:14 +03:00
)
2023-08-11 11:04:28 +03:00
sendLocationKeyboard = tg.NewReply().
2023-08-15 16:02:14 +03:00
Row(
2023-08-19 09:12:26 +03:00
tg.NewButton("Send location").
2023-08-15 16:02:14 +03:00
WithSendLocation(true).
2023-08-19 09:12:26 +03:00
ActionFunc(func(c *tg.Context) {
2023-08-15 16:02:14 +03:00
var err error
if c.Message.Location != nil {
l := c.Message.Location
_, err = c.Sendf(
2023-08-15 16:02:14 +03:00
"Longitude: %f\n"+
"Latitude: %f\n"+
"Heading: %d"+
"",
l.Longitude,
l.Latitude,
l.Heading,
)
} else {
_, err = c.Sendf("Somehow wrong location was sent")
2023-08-15 16:02:14 +03:00
}
if err != nil {
c.Sendf("%q", err)
2023-08-15 16:02:14 +03:00
}
}),
).Row(
startScreenButton,
)
2023-08-11 11:04:28 +03:00
// The keyboard to return to the start screen.
navToStartKeyboard = tg.NewReply().Row(
startScreenButton,
2023-08-15 16:02:14 +03:00
)
)
2023-08-19 09:12:26 +03:00
var beh = tg.NewBehaviour().
WithInitFunc(func(c *tg.Context) {
// The session initialization.
c.Session.Value = &UserData{}
2023-09-07 15:45:38 +03:00
}). // On any message update before the bot created session.
WithPreStartFunc(func(c *tg.Context){
c.Sendf("Please, use the /start command to start the bot")
2023-08-15 16:02:14 +03:00
}).WithScreens(
2023-08-19 09:12:26 +03:00
tg.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!",
).WithReply(navKeyboard).
2023-08-15 16:02:14 +03:00
// The inline keyboard with link to GitHub page.
WithInline(
tg.NewInline().Row(
2023-08-19 09:12:26 +03:00
tg.NewButton("GoT Github page").
2023-08-15 16:02:14 +03:00
WithUrl("https://github.com/mojosa-software/got"),
),
),
2023-08-11 11:04:28 +03:00
2023-08-19 09:12:26 +03:00
tg.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. ",
).
WithReply(&tg.ReplyKeyboard{Keyboard: incDecKeyboard.Keyboard}).
// The function will be called when reaching the screen.
2023-08-19 09:12:26 +03:00
ActionFunc(func(c *tg.Context) {
d := c.Session.Value.(*UserData)
2023-08-12 14:35:33 +03:00
c.Sendf("Current counter value = %d", d.Counter)
}),
2023-08-11 11:04:28 +03:00
2023-08-19 09:12:26 +03:00
tg.NewScreen("upper-case").
WithText("Type text and the bot will send you the upper case version to you").
WithReply(navToStartKeyboard).
2023-08-11 11:04:28 +03:00
ActionFunc(mutateMessage(strings.ToUpper)),
2023-08-19 09:12:26 +03:00
tg.NewScreen("lower-case").
2023-08-11 11:04:28 +03:00
WithText("Type text and the bot will send you the lower case version").
WithReply(navToStartKeyboard).
2023-08-11 11:04:28 +03:00
ActionFunc(mutateMessage(strings.ToLower)),
2023-08-15 16:02:14 +03:00
2023-08-19 09:12:26 +03:00
tg.NewScreen("send-location").
2023-08-15 16:02:14 +03:00
WithText("Send your location and I will tell where you are!").
WithReply(sendLocationKeyboard).
WithInline(
tg.NewInline().Row(
2023-08-19 09:12:26 +03:00
tg.NewButton("Check").
2023-08-15 16:02:14 +03:00
WithData("check").
2023-08-19 09:12:26 +03:00
ActionFunc(func(a *tg.Context) {
d := a.Session.Value.(*UserData)
2023-08-15 16:02:14 +03:00
a.Sendf("Counter = %d", d.Counter)
}),
),
),
2023-08-12 14:35:33 +03:00
).WithCommands(
2023-09-07 15:45:38 +03:00
tg.NewCommand("start").
Desc("start the bot").
ActionFunc(func(c *tg.Context){
c.ChangeScreen("start")
}),
2023-08-19 09:12:26 +03:00
tg.NewCommand("hello").
2023-08-12 14:35:33 +03:00
Desc("sends the 'Hello, World!' message back").
2023-08-19 09:12:26 +03:00
ActionFunc(func(c *tg.Context) {
c.Sendf("Hello, World!")
2023-08-12 14:35:33 +03:00
}),
2023-08-19 09:12:26 +03:00
tg.NewCommand("read").
2023-08-12 14:35:33 +03:00
Desc("reads a string and sends it back").
2023-08-19 09:12:26 +03:00
ActionFunc(func(c *tg.Context) {
c.Sendf("Type some text:")
2023-08-12 14:35:33 +03:00
msg, err := c.ReadTextMessage()
if err != nil {
return
}
c.Sendf("You typed %q", msg)
}),
tg.NewCommand("image").
Desc("sends a sample image").
ActionFunc(func(c *tg.Context) {
img := tg.NewFile("media/cat.jpg").Image().Caption("A cat!")
c.Send(img)
}),
tg.NewCommand("botname").
Desc("get the bot name").
ActionFunc(func(c *tg.Context) {
bd := c.Bot.Value().(*BotData)
c.Sendf("My name is %q", bd.Name)
}),
)
2023-07-09 01:28:59 +03:00
2023-08-19 09:12:26 +03:00
func mutateMessage(fn func(string) string) tg.ActionFunc {
return func(c *tg.Context) {
2023-08-11 11:04:28 +03:00
for {
msg, err := c.ReadTextMessage()
2023-08-19 09:12:26 +03:00
if err == tg.NotAvailableErr {
2023-08-11 11:04:28 +03:00
break
} else if err != nil {
panic(err)
}
_, err = c.Sendf("%s", fn(msg))
2023-08-11 11:04:28 +03:00
if err != nil {
panic(err)
}
}
}
}
2023-08-19 09:12:26 +03:00
var gBeh = tg.NewGroupBehaviour().
InitFunc(func(c *tg.GC) {
2023-08-13 15:37:36 +03:00
}).
WithCommands(
2023-08-19 09:12:26 +03:00
tg.NewGroupCommand("hello").ActionFunc(func(c *tg.GC) {
c.Send("Hello, World!")
2023-08-13 15:37:36 +03:00
}),
2023-08-19 09:12:26 +03:00
tg.NewGroupCommand("mycounter").ActionFunc(func(c *tg.GC) {
d := c.Session().Value.(*UserData)
c.Sendf("Your counter value is %d", d.Counter)
2023-08-13 15:37:36 +03:00
}),
)
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-19 09:12:26 +03:00
bot, err := tg.NewBot(token)
if err != nil {
log.Panic(err)
}
2023-08-13 15:37:36 +03:00
bot = bot.
WithBehaviour(beh).
WithGroupBehaviour(gBeh).
WithValue(&BotData{
Name: "Jay",
}).
Debug(true)
2023-07-08 22:26:21 +03:00
log.Printf("Authorized on account %s", bot.Api.Self.UserName)
bot.Run()
}