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-09-11 10:23:04 +03:00
|
|
|
type SessionData struct {
|
2023-08-12 14:35:33 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-09-20 22:48:35 +03:00
|
|
|
func (w *MutateMessageWidget) Serve(c *tg.Context) {
|
2023-09-20 23:38:29 +03:00
|
|
|
args, ok := c.Arg.(tg.ArgSlice)
|
2023-09-20 22:48:35 +03:00
|
|
|
if ok {
|
|
|
|
for _, arg := range args {
|
|
|
|
c.Sendf("%v", arg)
|
|
|
|
}
|
2023-09-13 12:52:23 +03:00
|
|
|
}
|
2023-09-20 22:48:35 +03:00
|
|
|
for u := range c.Input() {
|
2023-09-09 07:28:06 +03:00
|
|
|
text := u.Message.Text
|
|
|
|
c.Sendf("%s", w.Mutate(text))
|
|
|
|
}
|
2023-09-16 14:34:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *MutateMessageWidget) Filter(u *tg.Update, _ tg.MessageMap) bool {
|
|
|
|
if u.Message == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2023-09-09 07:28:06 +03:00
|
|
|
}
|
|
|
|
|
2023-09-11 10:23:04 +03:00
|
|
|
func ExtractSessionData(c *tg.Context) *SessionData {
|
|
|
|
return c.Session.Data.(*SessionData)
|
|
|
|
}
|
|
|
|
|
2023-08-15 16:02:14 +03:00
|
|
|
var (
|
2023-09-21 12:03:54 +03:00
|
|
|
startScreenButton = tg.NewButton("Home").Go("/")
|
2023-09-21 14:54:31 +03:00
|
|
|
backButton = tg.NewButton("Back").Go("..")
|
|
|
|
backKeyboard = tg.NewKeyboard().Row(
|
|
|
|
backButton,
|
|
|
|
)
|
|
|
|
|
2023-09-19 14:38:57 +03:00
|
|
|
incDecKeyboard = tg.NewKeyboard().Row(
|
2023-08-19 09:12:26 +03:00
|
|
|
tg.NewButton("+").ActionFunc(func(c *tg.Context) {
|
2023-09-11 10:23:04 +03:00
|
|
|
d := ExtractSessionData(c)
|
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-09-11 10:23:04 +03:00
|
|
|
d := ExtractSessionData(c)
|
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-19 14:38:57 +03:00
|
|
|
navKeyboard = tg.NewKeyboard().Row(
|
2023-09-21 12:03:54 +03:00
|
|
|
tg.NewButton("Inc/Dec").Go("/inc-dec"),
|
2023-09-19 14:38:57 +03:00
|
|
|
).Row(
|
2023-09-21 14:54:31 +03:00
|
|
|
tg.NewButton("Mutate messages").Go("/mutate-messages"),
|
2023-08-12 15:54:05 +03:00
|
|
|
).Row(
|
2023-09-21 12:03:54 +03:00
|
|
|
tg.NewButton("Send location").Go("/send-location"),
|
2023-09-19 14:38:57 +03:00
|
|
|
).Reply().WithOneTime(true)
|
2023-08-11 11:04:28 +03:00
|
|
|
|
2023-09-19 14:38:57 +03:00
|
|
|
sendLocationKeyboard = tg.NewKeyboard().Row(
|
|
|
|
tg.NewButton("Send location").
|
|
|
|
WithSendLocation(true).
|
|
|
|
ActionFunc(func(c *tg.Context) {
|
|
|
|
l := c.Message.Location
|
|
|
|
c.Sendf(
|
|
|
|
"Longitude: %f\n"+
|
|
|
|
"Latitude: %f\n"+
|
|
|
|
"Heading: %d"+
|
|
|
|
"",
|
|
|
|
l.Longitude,
|
|
|
|
l.Latitude,
|
|
|
|
l.Heading,
|
|
|
|
)
|
|
|
|
}),
|
|
|
|
).Row(
|
2023-08-15 16:02:14 +03:00
|
|
|
startScreenButton,
|
2023-09-19 14:38:57 +03:00
|
|
|
).Reply()
|
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-19 14:38:57 +03:00
|
|
|
navToStartKeyboard = tg.NewKeyboard().Row(
|
2023-08-11 10:41:17 +03:00
|
|
|
startScreenButton,
|
2023-09-19 14:38:57 +03:00
|
|
|
).Reply()
|
2023-08-15 16:02:14 +03:00
|
|
|
)
|
|
|
|
|
2023-08-19 09:12:26 +03:00
|
|
|
var beh = tg.NewBehaviour().
|
2023-09-21 12:03:54 +03:00
|
|
|
WithInitFunc(func(c *tg.Context) {
|
|
|
|
// The session initialization.
|
|
|
|
c.Session.Data = &SessionData{}
|
|
|
|
}).WithRootNode(tg.NewRootNode(
|
|
|
|
// The "/" widget.
|
|
|
|
tg.NewPage().
|
|
|
|
WithInline(
|
|
|
|
tg.NewKeyboard().Row(
|
|
|
|
tg.NewButton("GoT Github page").
|
|
|
|
WithUrl("https://github.com/mojosa-software/got"),
|
|
|
|
).Inline().Widget("The bot started!"),
|
|
|
|
).WithReply(
|
|
|
|
navKeyboard.Widget("Choose what you are interested in"),
|
2023-08-15 16:02:14 +03:00
|
|
|
),
|
2023-09-21 12:03:54 +03:00
|
|
|
|
2023-09-21 14:54:31 +03:00
|
|
|
tg.NewNode(
|
|
|
|
"mutate-messages", tg.NewPage().WithReply(
|
|
|
|
tg.NewKeyboard().Row(
|
|
|
|
tg.NewButton("Upper case").Go("upper-case"),
|
|
|
|
tg.NewButton("Lower case").Go("lower-case"),
|
|
|
|
).Row(
|
|
|
|
backButton,
|
|
|
|
).Reply().Widget(
|
|
|
|
"Choose the function to mutate string",
|
|
|
|
),
|
|
|
|
),
|
|
|
|
tg.NewNode(
|
|
|
|
"upper-case", tg.NewPage().WithReply(
|
|
|
|
backKeyboard.Reply().Widget(
|
|
|
|
"Type a string and the bot will convert it to upper case",
|
|
|
|
),
|
|
|
|
).WithSub(
|
|
|
|
NewMutateMessageWidget(strings.ToUpper),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
tg.NewNode(
|
|
|
|
"lower-case", tg.NewPage().WithReply(
|
|
|
|
backKeyboard.Reply().Widget(
|
|
|
|
"Type a string and the bot will convert it to lower case",
|
|
|
|
),
|
|
|
|
).WithSub(
|
|
|
|
NewMutateMessageWidget(strings.ToLower),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
|
2023-09-21 12:03:54 +03:00
|
|
|
tg.NewNode(
|
|
|
|
"inc-dec", tg.NewPage().WithReply(
|
2023-09-19 14:38:57 +03:00
|
|
|
incDecKeyboard.Reply().Widget("Press the buttons to increment and decrement"),
|
2023-09-09 07:28:06 +03:00
|
|
|
).ActionFunc(func(c *tg.Context) {
|
|
|
|
// The function will be calleb before serving page.
|
2023-09-11 10:23:04 +03:00
|
|
|
d := ExtractSessionData(c)
|
2023-09-09 07:28:06 +03:00
|
|
|
c.Sendf("Current counter value = %d", d.Counter)
|
|
|
|
}),
|
2023-09-21 12:03:54 +03:00
|
|
|
),
|
2023-08-11 11:04:28 +03:00
|
|
|
|
2023-09-21 12:03:54 +03:00
|
|
|
tg.NewNode(
|
|
|
|
"send-location", tg.NewPage().WithReply(
|
|
|
|
sendLocationKeyboard.Widget("Press the button to send your location!"),
|
|
|
|
).WithInline(
|
|
|
|
tg.NewKeyboard().Row(
|
|
|
|
tg.NewButton(
|
|
|
|
"Check",
|
|
|
|
).WithData(
|
|
|
|
"check",
|
|
|
|
).ActionFunc(func(c *tg.Context) {
|
|
|
|
d := ExtractSessionData(c)
|
|
|
|
c.Sendf("Counter = %d", d.Counter)
|
|
|
|
}),
|
|
|
|
).Inline().Widget("Press the button to display your counter"),
|
2023-08-15 16:02:14 +03:00
|
|
|
),
|
2023-09-21 12:03:54 +03:00
|
|
|
),
|
|
|
|
)).WithCommands(
|
|
|
|
tg.NewCommand("start").
|
|
|
|
Desc("start or restart the bot or move to the start screen").
|
|
|
|
ActionFunc(func(c *tg.Context){
|
2023-09-21 14:54:31 +03:00
|
|
|
c.Go("/")
|
2023-09-21 12:03:54 +03:00
|
|
|
}),
|
|
|
|
tg.NewCommand("hello").
|
|
|
|
Desc("sends the 'Hello, World!' message back").
|
|
|
|
ActionFunc(func(c *tg.Context) {
|
|
|
|
c.Sendf("Hello, World!")
|
|
|
|
}),
|
|
|
|
tg.NewCommand("read").
|
|
|
|
Desc("reads a string and sends it back").
|
|
|
|
WidgetFunc(func(c *tg.Context) {
|
|
|
|
c.Sendf("Type text and I will send it back to you")
|
|
|
|
for u := range c.Input() {
|
|
|
|
if u.Message == nil {
|
|
|
|
continue
|
2023-09-11 13:00:38 +03:00
|
|
|
}
|
2023-09-21 12:03:54 +03:00
|
|
|
c.Sendf("You typed %q", u.Message.Text)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
c.Sendf("Done")
|
|
|
|
}),
|
|
|
|
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.Data.(*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
|
|
|
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-09-11 13:00:38 +03:00
|
|
|
c.Sendf("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-09-11 10:23:04 +03:00
|
|
|
d := c.Session().Data.(*SessionData)
|
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-09-21 12:03:54 +03:00
|
|
|
log.Println(beh.Screens)
|
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
|
|
|
Debug(true)
|
2023-07-08 22:26:21 +03:00
|
|
|
|
2023-09-11 13:00:38 +03:00
|
|
|
bot.Data = &BotData{
|
|
|
|
Name: "Jay",
|
|
|
|
}
|
|
|
|
|
2023-08-19 12:47:33 +03:00
|
|
|
log.Printf("Authorized on account %s", bot.Api.Self.UserName)
|
2023-09-11 13:00:38 +03:00
|
|
|
err = bot.Run()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-08-10 15:49:25 +03:00
|
|
|
}
|