tg/cmd/test/main.go

344 lines
7.6 KiB
Go
Raw Normal View History

2023-07-03 17:22:45 +03:00
package main
2023-07-08 22:26:21 +03:00
import (
"log"
"os"
"strings"
"fmt"
2023-07-08 22:26:21 +03:00
2023-11-20 21:24:35 +03:00
"github.com/omnipunk/tg"
2023-07-09 01:28:59 +03:00
)
type BotData struct {
Name string
}
type SessionData struct {
2023-08-12 14:35:33 +03:00
Counter int
}
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) {
args, ok := c.Arg().([]any)
if ok {
for _, arg := range args {
c.Sendf("%v", arg)
}
}
for u := range c.Input() {
text := u.Message.Text
_, err := c.Sendf2("%s", w.Mutate(text))
if err != nil {
c.Sendf("debug: %q", err)
}
}
}
func (w *MutateMessageWidget) Filter(u *tg.Update) bool {
if u.Message == nil {
return true
}
return false
}
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("-")
backKeyboard = tg.NewKeyboard().Row(
backButton,
)
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"+
"",
l.Longitude,
l.Latitude,
l.Heading,
)
}),
).Row(
2023-09-22 11:17:18 +03:00
backButton,
).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(
// 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 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"),
).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
}
}),
tg.NewNode(
"mutate-messages", tg.RenderFunc(func(c *tg.Context) tg.UI {
return tg.UI{
tg.NewMessage(
"Choose the function to mutate string",
).Reply(
tg.NewKeyboard().Row(
tg.NewButton("Upper case").Go("upper-case"),
tg.NewButton("Lower case").Go("lower-case"),
tg.NewButton("Escape chars").Go("escape"),
).Row(
backButton,
).Reply(),
),
}
}),
tg.NewNode(
"upper-case", tg.RenderFunc(func(c *tg.Context) tg.UI {
return tg.UI{
tg.NewMessage(
"Type a string and the bot will convert it to upper case",
).Reply(
backKeyboard.Reply(),
),
NewMutateMessageWidget(strings.ToUpper),
}
}),
),
tg.NewNode(
"lower-case", tg.RenderFunc(func(c *tg.Context) tg.UI {
return tg.UI{
tg.NewMessage(
"Type a string and the bot will convert it to lower case",
).Reply(
backKeyboard.Reply(),
),
NewMutateMessageWidget(strings.ToLower),
}
}),
),
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),
}
}),
),
),
tg.NewNode(
"inc-dec", tg.RenderFunc(func(c *tg.Context) tg.UI {
2023-09-29 14:53:52 +03:00
var (
kbd *tg.InlineCompo
//cntMsg *tg.MessageCompo
2023-09-29 14:53:52 +03:00
inline, std, onlyInc, onlyDec *tg.Inline
)
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)
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-08-11 11:04:28 +03:00
tg.NewNode(
"send-location", tg.RenderFunc(func(c *tg.Context) tg.UI {
2023-10-22 20:41:01 +03:00
return tg.UI{
tg.NewMessage(
"Press the button to display your counter",
).Inline(
2023-10-22 20:41:01 +03:00
tg.NewKeyboard().Row(
tg.NewButton(
"Check",
).WithData(
"check",
).WithAction(tg.Func(func(c *tg.Context) {
d := ExtractSessionData(c)
c.Sendf("Counter = %d", d.Counter)
})),
).Inline(),
),
tg.NewMessage(
"Press the button to send your location!",
).Reply(
sendLocationKeyboard,
),
}
}),
),
)).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) {
c.Sendf("Please, use /start ")
})).WithCommands(
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`)
}),
tg.NewCommand(
"start",
"start or restart the bot or move to the start screen",
).Go("/"),
tg.NewCommand("hello", "sends the 'Hello, World!' message back").
ActionFunc(func(c *tg.Context) {
c.Sendf("Hello, World!")
}),
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
),
tg.NewCommand("image", "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", "get the bot name").
WithAction(tg.Func(func(c *tg.Context) {
bd := c.Bot.Data.(*BotData)
c.Sendf("My name is %q", bd.Name)
})),
tg.NewCommand("dynamic", "check of the dynamic work").
2023-10-22 20:41:01 +03:00
WithWidget(tg.Func(func(c *tg.Context) {
})),
tg.NewCommand("history", "print go history").
2023-10-22 20:41:01 +03:00
WithAction(tg.Func(func(c *tg.Context) {
c.Sendf("%q", c.History())
})),
tg.NewCommand("washington", "send location of the Washington").
2023-10-22 20:41:01 +03:00
WithAction(tg.Func(func(c *tg.Context) {
c.Sendf("Washington location")
c.Send(
tg.NewMessage("").Location(
47.751076, -120.740135,
),
)
})),
tg.NewCommand("invoice", "invoice check").
2023-10-22 20:41:01 +03:00
WithAction(tg.Func(func(c *tg.Context) {
})),
2023-10-22 20:41:01 +03:00
))
2023-07-03 17:22:45 +03:00
func main() {
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)
if err != nil {
log.Panic(err)
}
2023-08-13 15:37:36 +03:00
bot = bot.
WithBehaviour(beh).
Debug(true)
2023-07-08 22:26:21 +03:00
bot.Data = &BotData{
Name: "Jay",
}
log.Printf("Authorized on account %s", bot.Api.Self.UserName)
err = bot.Run()
if err != nil {
panic(err)
}
}