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-19 09:12:26 +03:00
|
|
|
"github.com/mojosa-software/got/tg"
|
2023-07-09 01:28:59 +03:00
|
|
|
)
|
|
|
|
|
2023-08-19 13:34:21 +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
|
|
|
|
2023-08-19 09:12:26 +03:00
|
|
|
incDecKeyboard = tg.NewKeyboard("").Row(
|
|
|
|
tg.NewButton("+").ActionFunc(func(c *tg.Context) {
|
2023-08-19 12:47:33 +03:00
|
|
|
d := c.Session.Value.(*UserData)
|
2023-08-12 14:35:33 +03:00
|
|
|
d.Counter++
|
|
|
|
c.Sendf("%d", d.Counter)
|
2023-08-11 10:41:17 +03:00
|
|
|
}),
|
2023-08-19 09:12:26 +03:00
|
|
|
tg.NewButton("-").ActionFunc(func(c *tg.Context) {
|
2023-08-19 12:47:33 +03:00
|
|
|
d := c.Session.Value.(*UserData)
|
2023-08-12 14:35:33 +03:00
|
|
|
d.Counter--
|
|
|
|
c.Sendf("%d", d.Counter)
|
2023-08-11 10:41:17 +03:00
|
|
|
}),
|
|
|
|
).Row(
|
|
|
|
startScreenButton,
|
2023-08-15 16:02:14 +03:00
|
|
|
)
|
2023-08-11 11:04:28 +03:00
|
|
|
|
2023-08-19 09:12:26 +03:00
|
|
|
navKeyboard = tg.NewKeyboard("Choose your interest").
|
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"),
|
2023-08-12 15:54:05 +03:00
|
|
|
).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
|
|
|
|
2023-08-19 09:12:26 +03:00
|
|
|
sendLocationKeyboard = tg.NewKeyboard("Press the button to send your location").
|
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
|
2023-08-18 14:20:33 +03:00
|
|
|
if c.Message.Location != nil {
|
|
|
|
l := c.Message.Location
|
2023-08-19 12:47:33 +03:00
|
|
|
_, 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 {
|
2023-08-19 12:47:33 +03:00
|
|
|
_, err = c.Send("Somehow wrong location was sent")
|
2023-08-15 16:02:14 +03:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
c.Send(err)
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
).Row(
|
|
|
|
startScreenButton,
|
|
|
|
)
|
2023-08-11 11:04:28 +03:00
|
|
|
|
2023-08-11 10:41:17 +03:00
|
|
|
// The keyboard to return to the start screen.
|
2023-08-19 09:12:26 +03:00
|
|
|
navToStartKeyboard = tg.NewKeyboard("").Row(
|
2023-08-11 10:41:17 +03:00
|
|
|
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) {
|
2023-08-16 17:25:56 +03:00
|
|
|
// The session initialization.
|
2023-08-19 12:47:33 +03:00
|
|
|
c.Session.Value = &UserData{}
|
2023-08-15 16:02:14 +03:00
|
|
|
c.ChangeScreen("start")
|
2023-08-16 17:25:56 +03:00
|
|
|
|
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!",
|
2023-08-15 16:02:14 +03:00
|
|
|
).WithKeyboard(navKeyboard).
|
|
|
|
// The inline keyboard with link to GitHub page.
|
|
|
|
WithIKeyboard(
|
2023-08-19 09:12:26 +03:00
|
|
|
tg.NewKeyboard("istart").Row(
|
|
|
|
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").
|
2023-08-11 10:41:17 +03:00
|
|
|
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. ",
|
2023-08-11 10:41:17 +03:00
|
|
|
).
|
2023-08-15 16:02:14 +03:00
|
|
|
WithKeyboard(incDecKeyboard).
|
2023-08-11 10:41:17 +03:00
|
|
|
// The function will be called when reaching the screen.
|
2023-08-19 09:12:26 +03:00
|
|
|
ActionFunc(func(c *tg.Context) {
|
2023-08-19 12:47:33 +03:00
|
|
|
d := c.Session.Value.(*UserData)
|
2023-08-12 14:35:33 +03:00
|
|
|
c.Sendf("Current counter value = %d", d.Counter)
|
2023-08-11 10:41:17 +03:00
|
|
|
}),
|
2023-08-11 11:04:28 +03:00
|
|
|
|
2023-08-19 09:12:26 +03:00
|
|
|
tg.NewScreen("upper-case").
|
2023-08-11 10:41:17 +03:00
|
|
|
WithText("Type text and the bot will send you the upper case version to you").
|
2023-08-15 16:02:14 +03:00
|
|
|
WithKeyboard(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").
|
2023-08-15 16:02:14 +03:00
|
|
|
WithKeyboard(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!").
|
|
|
|
WithKeyboard(sendLocationKeyboard).
|
|
|
|
WithIKeyboard(
|
2023-08-19 09:12:26 +03:00
|
|
|
tg.NewKeyboard("").Row(
|
|
|
|
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) {
|
2023-08-19 12:47:33 +03:00
|
|
|
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-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) {
|
2023-08-12 14:35:33 +03:00
|
|
|
c.Send("Hello, World!")
|
|
|
|
}),
|
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) {
|
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-08-19 10:19:31 +03:00
|
|
|
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)
|
|
|
|
}),
|
2023-08-19 13:34:21 +03:00
|
|
|
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-12 02:02:33 +03:00
|
|
|
)
|
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)
|
|
|
|
}
|
|
|
|
|
2023-08-19 12:47:33 +03:00
|
|
|
_, 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) {
|
2023-08-18 13:46:10 +03:00
|
|
|
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) {
|
2023-08-19 12:47:33 +03:00
|
|
|
d := c.Session().Value.(*UserData)
|
2023-08-18 13:46:10 +03:00
|
|
|
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)
|
2023-08-10 15:49:25 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
2023-08-13 15:37:36 +03:00
|
|
|
bot = bot.
|
|
|
|
WithBehaviour(beh).
|
2023-08-19 12:47:33 +03:00
|
|
|
WithGroupBehaviour(gBeh).
|
2023-08-19 13:34:21 +03:00
|
|
|
WithValue(&BotData{
|
|
|
|
Name: "Jay",
|
|
|
|
}).
|
2023-08-19 12:47:33 +03:00
|
|
|
Debug(true)
|
2023-07-08 22:26:21 +03:00
|
|
|
|
2023-08-19 12:47:33 +03:00
|
|
|
log.Printf("Authorized on account %s", bot.Api.Self.UserName)
|
2023-08-10 15:49:25 +03:00
|
|
|
bot.Run()
|
|
|
|
}
|