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-09-09 07:28:06 +03:00
|
|
|
type MutateMessageWidget struct {
|
|
|
|
Mutate func(string) string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMutateMessageWidget(fn func(string) string) *MutateMessageWidget {
|
|
|
|
ret := &MutateMessageWidget{}
|
|
|
|
ret.Mutate = fn
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *MutateMessageWidget) Serve(c *tg.Context, updates chan *tg.Update) {
|
|
|
|
for u := range updates {
|
|
|
|
if u.Message == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
text := u.Message.Text
|
|
|
|
c.Sendf("%s", w.Mutate(text))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-09-09 07:28:06 +03:00
|
|
|
incDecKeyboard = tg.NewReply().Row(
|
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
|
|
|
}),
|
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-09-08 17:37:32 +03:00
|
|
|
navKeyboard = tg.NewReply().
|
2023-08-15 16:02:14 +03:00
|
|
|
WithOneTime(true).
|
|
|
|
Row(
|
2023-09-09 07:28:06 +03:00
|
|
|
tg.NewButton("Inc/Dec").ScreenChange("start/inc-dec"),
|
2023-08-15 16:02:14 +03:00
|
|
|
).Row(
|
2023-09-09 07:28:06 +03:00
|
|
|
tg.NewButton("Upper case").ScreenChange("start/upper-case"),
|
|
|
|
tg.NewButton("Lower case").ScreenChange("start/lower-case"),
|
2023-08-12 15:54:05 +03:00
|
|
|
).Row(
|
2023-09-09 07:28:06 +03:00
|
|
|
tg.NewButton("Send location").ScreenChange("start/send-location"),
|
2023-08-15 16:02:14 +03:00
|
|
|
)
|
2023-08-11 11:04:28 +03:00
|
|
|
|
2023-09-08 17:37:32 +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
|
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-09-09 07:28:06 +03:00
|
|
|
_, err = c.Sendf("Somehow location was not sent")
|
2023-08-15 16:02:14 +03:00
|
|
|
}
|
|
|
|
if err != nil {
|
2023-09-08 17:37:32 +03:00
|
|
|
c.Sendf("%q", err)
|
2023-08-15 16:02:14 +03:00
|
|
|
}
|
|
|
|
}),
|
|
|
|
).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-09-08 17:37:32 +03:00
|
|
|
navToStartKeyboard = tg.NewReply().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-16 17:25:56 +03:00
|
|
|
|
2023-09-07 15:45:38 +03:00
|
|
|
}). // On any message update before the bot created session.
|
|
|
|
WithPreStartFunc(func(c *tg.Context){
|
2023-09-08 17:37:32 +03:00
|
|
|
c.Sendf("Please, use the /start command to start the bot")
|
2023-08-15 16:02:14 +03:00
|
|
|
}).WithScreens(
|
2023-09-09 07:28:06 +03:00
|
|
|
tg.NewScreen("start", tg.NewPage(
|
|
|
|
"The bot started!",
|
|
|
|
).WithInline(
|
|
|
|
tg.NewInline().Row(
|
|
|
|
tg.NewButton("GoT Github page").
|
|
|
|
WithUrl("https://github.com/mojosa-software/got"),
|
|
|
|
),
|
|
|
|
).WithReply(
|
|
|
|
navKeyboard,
|
2023-08-15 16:02:14 +03:00
|
|
|
),
|
|
|
|
),
|
2023-09-09 07:28:06 +03:00
|
|
|
tg.NewScreen("start/inc-dec", tg.NewPage(
|
|
|
|
"The screen shows how "+
|
|
|
|
"user separated data works "+
|
|
|
|
"by saving the counter for each of users "+
|
|
|
|
"separately. ",
|
|
|
|
).WithReply(
|
|
|
|
incDecKeyboard,
|
|
|
|
).ActionFunc(func(c *tg.Context) {
|
|
|
|
// The function will be calleb before serving page.
|
|
|
|
d := c.Session.Value.(*UserData)
|
|
|
|
c.Sendf("Current counter value = %d", d.Counter)
|
|
|
|
}),
|
|
|
|
),
|
2023-08-11 11:04:28 +03:00
|
|
|
|
2023-09-09 07:28:06 +03:00
|
|
|
tg.NewScreen("start/upper-case", tg.NewPage(
|
|
|
|
"Type text and the bot will send you the upper case version to you",
|
|
|
|
).WithReply(
|
|
|
|
navToStartKeyboard,
|
|
|
|
).WithSub(
|
|
|
|
NewMutateMessageWidget(strings.ToUpper),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
|
|
|
|
tg.NewScreen("start/lower-case", tg.NewPage(
|
|
|
|
"Type text and the bot will send you the lower case version",
|
|
|
|
).WithReply(
|
|
|
|
navToStartKeyboard,
|
|
|
|
).WithSub(
|
|
|
|
NewMutateMessageWidget(strings.ToLower),
|
|
|
|
),
|
|
|
|
),
|
2023-08-11 11:04:28 +03:00
|
|
|
|
2023-09-09 07:28:06 +03:00
|
|
|
tg.NewScreen("start/send-location", tg.NewPage(
|
|
|
|
"Send your location and I will tell where you are!",
|
|
|
|
).WithReply(
|
|
|
|
sendLocationKeyboard,
|
|
|
|
).WithInline(
|
|
|
|
tg.NewInline().Row(
|
|
|
|
tg.NewButton(
|
|
|
|
"Check",
|
|
|
|
).WithData(
|
|
|
|
"check",
|
|
|
|
).ActionFunc(func(c *tg.Context) {
|
|
|
|
d := c.Session.Value.(*UserData)
|
|
|
|
c.Sendf("Counter = %d", d.Counter)
|
2023-08-15 16:02:14 +03:00
|
|
|
}),
|
2023-09-09 07:28:06 +03:00
|
|
|
),
|
2023-08-15 16:02:14 +03:00
|
|
|
),
|
|
|
|
),
|
2023-08-12 14:35:33 +03:00
|
|
|
).WithCommands(
|
2023-09-07 15:45:38 +03:00
|
|
|
tg.NewCommand("start").
|
2023-09-09 07:28:06 +03:00
|
|
|
Desc("start or restart the bot or move to the start screen").
|
2023-09-07 15:45:38 +03:00
|
|
|
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) {
|
2023-09-08 17:37:32 +03:00
|
|
|
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) {
|
2023-09-09 07:28:06 +03:00
|
|
|
/*c.Sendf("Type some text:")
|
2023-08-12 14:35:33 +03:00
|
|
|
msg, err := c.ReadTextMessage()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-09-09 07:28:06 +03:00
|
|
|
c.Sendf("You typed %q", msg)*/
|
2023-08-12 14:35:33 +03:00
|
|
|
}),
|
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
|
|
|
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()
|
|
|
|
}
|