package main import ( "log" "os" "strings" "fmt" "github.com/di4f/tg" ) type BotData struct { Name string } type SessionData struct { 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) } var ( 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"+ "Latitude: %f\n"+ "Heading: %d"+ "", l.Longitude, l.Latitude, l.Heading, ) }), ).Row( backButton, ).Reply() ) var beh = tg.NewBehaviour(). WithInitFunc(func(c *tg.Context) { // The session initialization. c.Session.Data = &SessionData{} }).WithRootNode(tg.NewRootNode( // The "/" widget. tg.RenderFunc(func(c *tg.Context) tg.UI { return tg.UI{ 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(), ), 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"), ).Row( tg.NewButton("Dynamic panel").Go("panel"), ).Reply(), ), tg.Func(func(c *tg.Context) { for u := range c.Input() { if u.EditedMessage != nil { c.Sendf2("The new message is `%s`", u.EditedMessage.Text) } } }), } }), 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 ; icockcock 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"). WithWidget( tg.Func(func(c *tg.Context) { str := c.ReadString("Type a string and I will send it back") c.Sendf2("You typed `%s`", str) }), ), tg.NewCommand("cat", "sends a sample image of cat"). ActionFunc(func(c *tg.Context) { f, err := os.Open("media/cat.jpg") if err != nil { c.Sendf("err: %s", err) return } defer f.Close() photo := tg.NewFile(f).Photo().Name("cat.jpg").Caption("A cat!") c.Send(photo) }), tg.NewCommand("document", "sends a sample text document"). ActionFunc(func(c *tg.Context) { f, err := os.Open("media/hello.txt") if err != nil { c.Sendf("err: %s", err) return } defer f.Close() doc := tg.NewFile(f).Document().Name("hello.txt").Caption("The document") c.Send(doc) }), 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"). WithWidget(tg.Func(func(c *tg.Context) { })), tg.NewCommand("history", "print go history"). WithAction(tg.Func(func(c *tg.Context) { c.Sendf("%q", c.History()) })), tg.NewCommand("washington", "send location of the Washington"). 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"). WithAction(tg.Func(func(c *tg.Context) { })), )) func main() { log.Println(beh.Screens) token := os.Getenv("BOT_TOKEN") bot, err := tg.NewBot(token) if err != nil { log.Panic(err) } bot = bot. WithBehaviour(beh). Debug(true) bot.Data = &BotData{ Name: "Jay", } log.Printf("Authorized on account %s", bot.Api.Self.UserName) err = bot.Run() if err != nil { panic(err) } }