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-09-21 15:28:06 +03:00
|
|
|
"fmt"
|
2023-07-08 22:26:21 +03:00
|
|
|
|
2023-08-19 09:12:26 +03:00
|
|
|
"github.com/mojosa-software/got/tg"
|
2023-09-27 14:09:49 +03:00
|
|
|
//"math/rand"
|
|
|
|
//"strconv"
|
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-27 14:09:49 +03:00
|
|
|
args, ok := c.Arg.([]any)
|
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
|
|
|
}
|
|
|
|
|
2023-09-27 14:09:49 +03:00
|
|
|
func (w *MutateMessageWidget) Filter(u *tg.Update) bool {
|
2023-09-16 14:34:17 +03:00
|
|
|
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-22 11:17:18 +03:00
|
|
|
homeButton = 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(
|
2023-09-22 11:17:18 +03:00
|
|
|
backButton,
|
2023-08-15 16:02:14 +03:00
|
|
|
)
|
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-09-22 11:17:18 +03:00
|
|
|
backButton,
|
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.
|
2023-09-27 14:09:49 +03:00
|
|
|
tg.RenderFunc(func(c *tg.Context) tg.UI {return tg.UI {
|
2023-09-29 13:36:37 +03:00
|
|
|
tg.NewMessage(fmt.Sprintf(
|
|
|
|
fmt.Sprint(
|
|
|
|
"Hello, %s!\n",
|
|
|
|
"The testing bot started!\n",
|
|
|
|
"You can see the basics of usage in the ",
|
|
|
|
"cmd/test/main.go file!",
|
|
|
|
),
|
|
|
|
c.SentFrom().UserName,
|
|
|
|
)).Inline(
|
|
|
|
tg.NewKeyboard().Row(
|
2023-09-26 17:13:31 +03:00
|
|
|
tg.NewButton("GoT Github page").
|
|
|
|
WithUrl("https://github.com/mojosa-software/got"),
|
2023-09-29 13:36:37 +03:00
|
|
|
).Inline(),
|
|
|
|
),
|
2023-09-25 23:43:22 +03:00
|
|
|
|
2023-09-29 13:36:37 +03:00
|
|
|
tg.NewMessage("Choose your interest").Reply(
|
|
|
|
tg.NewKeyboard().Row(
|
|
|
|
tg.NewButton("Inc/Dec").Go("/inc-dec"),
|
|
|
|
).Row(
|
|
|
|
tg.NewButton("Mutate messages").Go("/mutate-messages"),
|
|
|
|
).Row(
|
|
|
|
tg.NewButton("Send location").Go("/send-location"),
|
|
|
|
).Reply(),
|
|
|
|
),
|
2023-09-26 17:13:31 +03:00
|
|
|
}}),
|
2023-09-21 12:03:54 +03:00
|
|
|
|
2023-09-21 14:54:31 +03:00
|
|
|
tg.NewNode(
|
2023-09-27 14:09:49 +03:00
|
|
|
"mutate-messages", tg.RenderFunc(func(c *tg.Context) tg.UI {
|
|
|
|
return tg.UI{
|
2023-09-29 13:36:37 +03:00
|
|
|
tg.NewMessage(
|
2023-09-27 14:09:49 +03:00
|
|
|
"Choose the function to mutate string",
|
2023-09-29 13:36:37 +03:00
|
|
|
).Reply(
|
|
|
|
tg.NewKeyboard().Row(
|
|
|
|
tg.NewButton("Upper case").Go("upper-case"),
|
|
|
|
tg.NewButton("Lower case").Go("lower-case"),
|
|
|
|
).Row(
|
|
|
|
backButton,
|
|
|
|
).Reply(),
|
2023-09-27 14:09:49 +03:00
|
|
|
),
|
|
|
|
}
|
|
|
|
}),
|
2023-09-21 14:54:31 +03:00
|
|
|
tg.NewNode(
|
2023-09-27 14:09:49 +03:00
|
|
|
"upper-case", tg.RenderFunc(func(c *tg.Context) tg.UI {
|
|
|
|
return tg.UI{
|
2023-09-29 13:36:37 +03:00
|
|
|
tg.NewMessage(
|
|
|
|
"Type a string and the bot will convert it to upper case",
|
|
|
|
).Reply(
|
|
|
|
backKeyboard.Reply(),
|
|
|
|
),
|
2023-09-27 14:09:49 +03:00
|
|
|
NewMutateMessageWidget(strings.ToUpper),
|
|
|
|
}
|
|
|
|
}),
|
2023-09-21 14:54:31 +03:00
|
|
|
),
|
|
|
|
tg.NewNode(
|
2023-09-27 14:09:49 +03:00
|
|
|
"lower-case", tg.RenderFunc(func(c *tg.Context) tg.UI {
|
|
|
|
return tg.UI{
|
2023-09-29 13:36:37 +03:00
|
|
|
tg.NewMessage(
|
|
|
|
"Type a string and the bot will convert it to lower case",
|
|
|
|
).Reply(
|
|
|
|
backKeyboard.Reply(),
|
|
|
|
),
|
2023-09-27 14:09:49 +03:00
|
|
|
NewMutateMessageWidget(strings.ToLower),
|
|
|
|
}
|
|
|
|
}),
|
2023-09-21 14:54:31 +03:00
|
|
|
),
|
|
|
|
),
|
|
|
|
|
2023-09-21 12:03:54 +03:00
|
|
|
tg.NewNode(
|
2023-09-27 14:09:49 +03:00
|
|
|
"inc-dec", tg.RenderFunc(func(c *tg.Context) tg.UI {
|
|
|
|
d := ExtractSessionData(c)
|
|
|
|
return tg.UI{
|
2023-09-29 13:36:37 +03:00
|
|
|
tg.NewMessage(fmt.Sprintf(
|
2023-09-27 14:09:49 +03:00
|
|
|
"Press the buttons to increment and decrement.\n" +
|
|
|
|
"Current counter value = %d", d.Counter,
|
2023-09-29 13:36:37 +03:00
|
|
|
)).Reply(
|
|
|
|
incDecKeyboard.Reply(),
|
|
|
|
),
|
2023-09-27 14:09:49 +03:00
|
|
|
}
|
|
|
|
}),
|
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(
|
2023-09-27 14:09:49 +03:00
|
|
|
"send-location", tg.RenderFunc(func(c *tg.Context) tg.UI {
|
|
|
|
return tg.UI {
|
2023-09-29 13:36:37 +03:00
|
|
|
tg.NewMessage(
|
|
|
|
"Press the button to display your counter",
|
|
|
|
).Inline(
|
2023-09-27 14:09:49 +03:00
|
|
|
tg.NewKeyboard().Row(
|
2023-09-29 13:36:37 +03:00
|
|
|
tg.NewButton(
|
|
|
|
"Check",
|
|
|
|
).WithData(
|
|
|
|
"check",
|
|
|
|
).WithAction(tg.Func(func(c *tg.Context) {
|
|
|
|
d := ExtractSessionData(c)
|
|
|
|
c.Sendf("Counter = %d", d.Counter)
|
|
|
|
})),
|
|
|
|
).Inline(),
|
|
|
|
),
|
2023-09-27 14:09:49 +03:00
|
|
|
|
2023-09-29 13:36:37 +03:00
|
|
|
tg.NewMessage(
|
2023-09-27 14:09:49 +03:00
|
|
|
"Press the button to send your location!",
|
2023-09-29 13:36:37 +03:00
|
|
|
).Reply(
|
|
|
|
sendLocationKeyboard,
|
2023-09-27 14:09:49 +03:00
|
|
|
),
|
|
|
|
}
|
|
|
|
}),
|
2023-09-21 12:03:54 +03:00
|
|
|
),
|
2023-09-27 14:09:49 +03:00
|
|
|
)).WithRoot(tg.NewCommandCompo().
|
|
|
|
WithPreStart(tg.Func(func(c *tg.Context){
|
|
|
|
c.Sendf("Please, use /start ")
|
|
|
|
})).WithCommands(
|
2023-09-22 11:17:18 +03:00
|
|
|
tg.NewCommand("info").
|
|
|
|
ActionFunc(func(c *tg.Context){
|
|
|
|
c.SendfHTML(`<a href="https://res.cloudinary.com/demo/image/upload/v1312461204/sample.jpg">cock</a><strong>cock</strong> die`)
|
|
|
|
}),
|
2023-09-21 12:03:54 +03:00
|
|
|
tg.NewCommand("start").
|
2023-09-21 15:28:06 +03:00
|
|
|
Desc(
|
|
|
|
"start or restart the bot or move to the start screen",
|
|
|
|
).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").
|
2023-09-22 11:17:18 +03:00
|
|
|
WithWidget(
|
2023-09-22 12:59:39 +03:00
|
|
|
tg.Func(func(c *tg.Context){
|
|
|
|
str := c.ReadString("Type a string and I will send it back")
|
|
|
|
c.Sendf2("You typed `%s`", str)
|
|
|
|
}),
|
2023-09-22 11:17:18 +03:00
|
|
|
),
|
2023-09-21 12:03:54 +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)
|
|
|
|
}),
|
|
|
|
tg.NewCommand("botname").
|
|
|
|
Desc("get the bot name").
|
2023-09-27 14:09:49 +03:00
|
|
|
WithAction(tg.Func(func(c *tg.Context) {
|
2023-09-21 12:03:54 +03:00
|
|
|
bd := c.Bot.Data.(*BotData)
|
|
|
|
c.Sendf("My name is %q", bd.Name)
|
2023-09-27 14:09:49 +03:00
|
|
|
})),
|
2023-09-25 23:43:22 +03:00
|
|
|
tg.NewCommand("dynamic").
|
|
|
|
Desc("check of the dynamic work").
|
2023-09-27 14:09:49 +03:00
|
|
|
WithWidget(tg.Func(func(c *tg.Context){
|
|
|
|
})),
|
|
|
|
))
|
2023-07-09 01:28:59 +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
|
|
|
}
|