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-12-14 21:16:26 +03:00
|
|
|
"github.com/di4f/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-10-11 13:41:08 +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
|
2023-10-05 17:51:24 +03:00
|
|
|
_, err := c.Sendf2("%s", w.Mutate(text))
|
|
|
|
if err != nil {
|
|
|
|
c.Sendf("debug: %q", err)
|
|
|
|
}
|
2023-09-09 07:28:06 +03:00
|
|
|
}
|
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-10-22 20:41:01 +03:00
|
|
|
homeButton = tg.NewButton("Home").Go("/")
|
|
|
|
backButton = tg.NewButton("Back").Go("-")
|
2023-09-21 14:54:31 +03:00
|
|
|
backKeyboard = tg.NewKeyboard().Row(
|
|
|
|
backButton,
|
|
|
|
)
|
|
|
|
|
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"+
|
2023-10-22 20:41:01 +03:00
|
|
|
"Latitude: %f\n"+
|
|
|
|
"Heading: %d"+
|
|
|
|
"",
|
2023-09-19 14:38:57 +03:00
|
|
|
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-10-22 20:41:01 +03:00
|
|
|
WithInitFunc(func(c *tg.Context) {
|
|
|
|
// The session initialization.
|
|
|
|
c.Session.Data = &SessionData{}
|
|
|
|
}).WithRootNode(tg.NewRootNode(
|
2023-09-21 12:03:54 +03:00
|
|
|
// The "/" widget.
|
2023-09-29 14:53:52 +03:00
|
|
|
tg.RenderFunc(func(c *tg.Context) tg.UI {
|
2023-10-22 20:41:01 +03:00
|
|
|
return tg.UI{
|
2023-09-29 14:53:52 +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(
|
|
|
|
tg.NewButton("GoT Github page").
|
|
|
|
WithUrl("https://github.com/mojosa-software/got"),
|
|
|
|
).Inline(),
|
2023-09-29 13:36:37 +03:00
|
|
|
),
|
2023-09-25 23:43:22 +03:00
|
|
|
|
2023-09-29 14:53:52 +03:00
|
|
|
tg.NewMessage("Choose your interest").Reply(
|
|
|
|
tg.NewKeyboard().Row(
|
2023-10-22 20:41:01 +03:00
|
|
|
tg.NewButton("Inc/Dec").Go("/inc-dec"),
|
|
|
|
).Row(
|
|
|
|
tg.NewButton("Mutate messages").Go("/mutate-messages"),
|
|
|
|
).Row(
|
|
|
|
tg.NewButton("Send location").Go("/send-location"),
|
2023-12-19 22:35:57 +03:00
|
|
|
).Row(
|
|
|
|
tg.NewButton("Dynamic panel").Go("panel"),
|
2023-10-22 20:41:01 +03:00
|
|
|
).Reply(),
|
2023-09-29 14:53:52 +03:00
|
|
|
),
|
2023-09-30 09:55:45 +03:00
|
|
|
|
2023-10-22 20:41:01 +03:00
|
|
|
tg.Func(func(c *tg.Context) {
|
2023-09-30 09:55:45 +03:00
|
|
|
for u := range c.Input() {
|
|
|
|
if u.EditedMessage != nil {
|
|
|
|
c.Sendf2("The new message is `%s`", u.EditedMessage.Text)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
2023-09-29 14:53:52 +03:00
|
|
|
}
|
|
|
|
}),
|
2023-09-21 12:03:54 +03:00
|
|
|
|
2023-12-19 22:35:57 +03:00
|
|
|
tg.NewNode(
|
|
|
|
"panel",
|
|
|
|
tg.RenderFunc(func(c *tg.Context) tg.UI {
|
|
|
|
var (
|
|
|
|
n = 0
|
|
|
|
ln = 4
|
|
|
|
panel *tg.PanelCompo
|
|
|
|
)
|
|
|
|
|
|
|
|
panel = tg.NewMessage(
|
|
|
|
"Some panel",
|
|
|
|
).Panel(c, tg.RowserFunc(func(c *tg.Context) []tg.ButtonRow{
|
|
|
|
btns := []tg.ButtonRow{
|
|
|
|
tg.ButtonRow{tg.NewButton("Static shit")},
|
|
|
|
}
|
|
|
|
for i:=0 ; i<ln ; i++ {
|
|
|
|
num := 1 + n * ln + i
|
|
|
|
btns = append(btns, tg.ButtonRow{
|
|
|
|
tg.NewButton("%d", num).WithAction(tg.Func(func(c *tg.Context){
|
|
|
|
c.Sendf("%d", num*num)
|
|
|
|
})),
|
|
|
|
tg.NewButton("%d", num*num),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
btns = append(btns, tg.ButtonRow{
|
|
|
|
tg.NewButton("Prev").WithAction(tg.ActionFunc(func(c *tg.Context){
|
|
|
|
n--
|
|
|
|
panel.Update(c)
|
|
|
|
})),
|
|
|
|
tg.NewButton("Next").WithAction(tg.ActionFunc(func(c *tg.Context){
|
|
|
|
n++
|
|
|
|
panel.Update(c)
|
|
|
|
})),
|
|
|
|
})
|
|
|
|
|
|
|
|
return btns
|
|
|
|
}))
|
|
|
|
|
|
|
|
return tg.UI{
|
|
|
|
panel,
|
|
|
|
tg.NewMessage("").Reply(
|
|
|
|
backKeyboard.Reply(),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
|
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"),
|
2023-10-05 17:51:24 +03:00
|
|
|
tg.NewButton("Escape chars").Go("escape"),
|
2023-09-29 13:36:37 +03:00
|
|
|
).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-10-05 17:51:24 +03:00
|
|
|
tg.NewNode(
|
|
|
|
"escape", tg.RenderFunc(func(c *tg.Context) tg.UI {
|
|
|
|
return tg.UI{
|
|
|
|
tg.NewMessage(
|
|
|
|
"Type a string and the bot will escape characters in it",
|
|
|
|
).Reply(
|
|
|
|
backKeyboard.Reply(),
|
|
|
|
),
|
|
|
|
NewMutateMessageWidget(tg.Escape2),
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
),
|
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 {
|
2023-09-29 14:53:52 +03:00
|
|
|
var (
|
|
|
|
kbd *tg.InlineCompo
|
2023-10-13 23:27:20 +03:00
|
|
|
//cntMsg *tg.MessageCompo
|
2023-09-29 14:53:52 +03:00
|
|
|
inline, std, onlyInc, onlyDec *tg.Inline
|
|
|
|
)
|
|
|
|
|
2023-09-27 14:09:49 +03:00
|
|
|
d := ExtractSessionData(c)
|
2023-09-29 14:53:52 +03:00
|
|
|
format := "Press the buttons to increment and decrement.\n" +
|
|
|
|
"Current counter value = %d"
|
|
|
|
|
|
|
|
incBtn := tg.NewButton("+").ActionFunc(func(c *tg.Context) {
|
2023-10-22 20:41:01 +03:00
|
|
|
d.Counter++
|
|
|
|
kbd.Text = fmt.Sprintf(format, d.Counter)
|
|
|
|
if d.Counter == 5 {
|
|
|
|
kbd.Inline = onlyDec
|
|
|
|
} else {
|
|
|
|
kbd.Inline = std
|
|
|
|
}
|
|
|
|
kbd.Update(c)
|
|
|
|
})
|
2023-09-29 14:53:52 +03:00
|
|
|
decBtn := tg.NewButton("-").ActionFunc(func(c *tg.Context) {
|
2023-10-22 20:41:01 +03:00
|
|
|
d.Counter--
|
|
|
|
kbd.Text = fmt.Sprintf(format, d.Counter)
|
|
|
|
if d.Counter == -5 {
|
|
|
|
kbd.Inline = onlyInc
|
|
|
|
} else {
|
|
|
|
kbd.Inline = std
|
|
|
|
}
|
|
|
|
kbd.Update(c)
|
|
|
|
//c.Sendf("%d", d.Counter)
|
|
|
|
})
|
2023-09-29 14:53:52 +03:00
|
|
|
|
|
|
|
onlyInc = tg.NewKeyboard().Row(incBtn).Inline()
|
|
|
|
onlyDec = tg.NewKeyboard().Row(decBtn).Inline()
|
|
|
|
std = tg.NewKeyboard().Row(incBtn, decBtn).Inline()
|
|
|
|
|
|
|
|
if d.Counter == 5 {
|
|
|
|
inline = onlyDec
|
|
|
|
} else if d.Counter == -5 {
|
|
|
|
inline = onlyInc
|
|
|
|
} else {
|
|
|
|
inline = std
|
|
|
|
}
|
|
|
|
|
|
|
|
kbd = tg.NewMessage(
|
|
|
|
fmt.Sprintf(format, d.Counter),
|
|
|
|
).Inline(inline)
|
|
|
|
|
2023-09-27 14:09:49 +03:00
|
|
|
return tg.UI{
|
2023-09-29 14:53:52 +03:00
|
|
|
kbd,
|
2023-09-30 09:55:45 +03:00
|
|
|
tg.NewMessage("Use the reply keyboard to get back").Reply(
|
2023-09-29 14:53:52 +03:00
|
|
|
backKeyboard.Reply(),
|
2023-09-29 13:36:37 +03:00
|
|
|
),
|
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 {
|
2023-10-22 20:41:01 +03:00
|
|
|
return tg.UI{
|
2023-09-29 13:36:37 +03:00
|
|
|
tg.NewMessage(
|
|
|
|
"Press the button to display your counter",
|
|
|
|
).Inline(
|
2023-10-22 20:41:01 +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().
|
2023-10-22 20:41:01 +03:00
|
|
|
WithUsage(tg.Func(func(c *tg.Context) {
|
|
|
|
c.Sendf("There is no such command %q", c.Message.Command())
|
|
|
|
})).WithPreStart(tg.Func(func(c *tg.Context) {
|
2023-09-27 14:09:49 +03:00
|
|
|
c.Sendf("Please, use /start ")
|
|
|
|
})).WithCommands(
|
2023-10-11 14:45:35 +03:00
|
|
|
tg.NewCommand("info", "info desc").
|
2023-10-22 20:41:01 +03:00
|
|
|
ActionFunc(func(c *tg.Context) {
|
2023-09-22 11:17:18 +03:00
|
|
|
c.SendfHTML(`<a href="https://res.cloudinary.com/demo/image/upload/v1312461204/sample.jpg">cock</a><strong>cock</strong> die`)
|
|
|
|
}),
|
2023-10-11 14:45:35 +03:00
|
|
|
tg.NewCommand(
|
|
|
|
"start",
|
|
|
|
"start or restart the bot or move to the start screen",
|
|
|
|
).Go("/"),
|
|
|
|
tg.NewCommand("hello", "sends the 'Hello, World!' message back").
|
2023-09-21 12:03:54 +03:00
|
|
|
ActionFunc(func(c *tg.Context) {
|
|
|
|
c.Sendf("Hello, World!")
|
|
|
|
}),
|
2023-10-11 14:45:35 +03:00
|
|
|
tg.NewCommand("read", "reads a string and sends it back").
|
2023-09-22 11:17:18 +03:00
|
|
|
WithWidget(
|
2023-10-22 20:41:01 +03:00
|
|
|
tg.Func(func(c *tg.Context) {
|
2023-09-22 12:59:39 +03:00
|
|
|
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-10-11 14:45:35 +03:00
|
|
|
tg.NewCommand("image", "sends a sample image").
|
2023-09-21 12:03:54 +03:00
|
|
|
ActionFunc(func(c *tg.Context) {
|
|
|
|
img := tg.NewFile("media/cat.jpg").Image().Caption("A cat!")
|
|
|
|
c.Send(img)
|
|
|
|
}),
|
2023-10-11 14:45:35 +03:00
|
|
|
tg.NewCommand("botname", "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-10-11 14:45:35 +03:00
|
|
|
tg.NewCommand("dynamic", "check of the dynamic work").
|
2023-10-22 20:41:01 +03:00
|
|
|
WithWidget(tg.Func(func(c *tg.Context) {
|
2023-09-27 14:09:49 +03:00
|
|
|
})),
|
2023-10-11 14:45:35 +03:00
|
|
|
tg.NewCommand("history", "print go history").
|
2023-10-22 20:41:01 +03:00
|
|
|
WithAction(tg.Func(func(c *tg.Context) {
|
2023-10-03 14:49:56 +03:00
|
|
|
c.Sendf("%q", c.History())
|
|
|
|
})),
|
2023-10-11 14:45:35 +03:00
|
|
|
tg.NewCommand("washington", "send location of the Washington").
|
2023-10-22 20:41:01 +03:00
|
|
|
WithAction(tg.Func(func(c *tg.Context) {
|
2023-10-11 14:45:35 +03:00
|
|
|
c.Sendf("Washington location")
|
2023-10-11 14:20:25 +03:00
|
|
|
c.Send(
|
|
|
|
tg.NewMessage("").Location(
|
|
|
|
47.751076, -120.740135,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
})),
|
2023-10-13 23:27:20 +03:00
|
|
|
tg.NewCommand("invoice", "invoice check").
|
2023-10-22 20:41:01 +03:00
|
|
|
WithAction(tg.Func(func(c *tg.Context) {
|
2023-10-13 23:27:20 +03:00
|
|
|
})),
|
2023-10-22 20:41:01 +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
|
|
|
}
|