diff --git a/cmd/test/main.go b/cmd/test/main.go index 61e0b13..95b3931 100644 --- a/cmd/test/main.go +++ b/cmd/test/main.go @@ -7,6 +7,8 @@ import ( "fmt" "github.com/mojosa-software/got/tg" + "math/rand" + "strconv" ) type BotData struct { @@ -73,13 +75,7 @@ var ( backButton, ) - navKeyboard = 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() + navKeyboard = sendLocationKeyboard = tg.NewKeyboard().Row( tg.NewButton("Send location"). @@ -107,21 +103,34 @@ WithInitFunc(func(c *tg.Context) { c.Session.Data = &SessionData{} }).WithRootNode(tg.NewRootNode( // The "/" widget. - tg.NewPage(). - WithInline( + tg.WidgetFunc(func(c *tg.Context) tg.UIs { + return tg.UIs{ + tg.NewKeyboard().Row( - tg.NewButton("GoT Github page"). - WithUrl("https://github.com/mojosa-software/got"), - ).Inline().Widget( - fmt.Sprint( - "The testing bot started!\n", - "You can see the basics of usage in the ", - "cmd/test/main.go file!", + tg.NewButton("GoT Github page"). + WithUrl("https://github.com/mojosa-software/got"), + ).Inline().Widget( + fmt.Sprintf( + "Hello, %s" + "The testing bot started!\n", + "You can see the basics of usage in the ", + "cmd/test/main.go file!", + c.SentFrom().UserName, + ), ), - ), - ).WithReply( - navKeyboard.Widget("Choose what you are interested in"), - ), + + 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().Widget( + "Choose the point of your interest", + ), + + } + ) tg.NewNode( "mutate-messages", tg.NewPage().WithReply( @@ -216,6 +225,32 @@ WithInitFunc(func(c *tg.Context) { bd := c.Bot.Data.(*BotData) c.Sendf("My name is %q", bd.Name) }), + tg.NewCommand("dynamic"). + Desc("check of the dynamic work"). + WidgetFunc(func(c *tg.Context){ + nRow, nBtn := rand.Int()%10, rand.Int()%5 + rows := []tg.ButtonRow{} + for i:=0 ; i") + sid := c.Session.Id.ToApi() + if compo == nil { + msgConfig := tgbotapi.NewMessage(sid, ">") msgConfig.ReplyMarkup = tgbotapi.NewRemoveKeyboard(true) return &SendConfig{ Message: &msgConfig, } } var text string - if widget.Text != "" { - text = widget.Text + if compo.Text != "" { + text = compo.Text } else { text = ">" } - msgConfig := tgbotapi.NewMessage(sid.ToApi(), text) - msgConfig.ReplyMarkup = widget.ToApi() + msgConfig := tgbotapi.NewMessage(sid, text) + msgConfig.ReplyMarkup = compo.ToApi() ret := &SendConfig{} ret.Message = &msgConfig return ret } -func (widget *ReplyWidget) Filter( +func (compo *ReplyCompo) Filter( u *Update, - msgs MessageMap, ) bool { - if widget == nil { + if compo == nil || u.Message == nil { return true } - if u.Message == nil { - return true - } - - _, ok := widget.ButtonMap()[u.Message.Text] if !ok { if u.Message.Location != nil { @@ -119,11 +113,8 @@ func (widget *ReplyWidget) Filter( } // Implementing the Widget interface. -func (widget *ReplyWidget) Serve(c *Context) { +func (compo *ReplyCompo) Serve(c *Context) { for u := range c.Input() { - if u.Message == nil || u.Message.Text == "" { - continue - } var btn *Button text := u.Message.Text btns := widget.ButtonMap() diff --git a/tg/send.go b/tg/send.go index 13e1819..ea4606e 100644 --- a/tg/send.go +++ b/tg/send.go @@ -5,16 +5,12 @@ import ( ) type MessageId int64 -type Image any - -// Implementing the interface lets the -// value to be sent. -type Sendable interface { - SendConfig(SessionId, *Bot) *SendConfig -} +// Implementing the interface provides +// way to define what message will be +// sent to the side of a user. type Renderable interface { - Render(SessionId, *Bot) ([]*SendConfig) + Render(*Context) (*SendConfig) } type Errorer interface { diff --git a/tg/server.go b/tg/server.go new file mode 100644 index 0000000..4d36063 --- /dev/null +++ b/tg/server.go @@ -0,0 +1,8 @@ +package tg + +// Implementing the interface provides +// the way to define how to handle updates. +type Server interface { + Serve(*Context) +} + diff --git a/tg/widget.go b/tg/widget.go index 10227d8..adeeb1f 100644 --- a/tg/widget.go +++ b/tg/widget.go @@ -1,77 +1,10 @@ package tg -import ( - //tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5" - //"fmt" -) - type Maker[V any] interface { Make(*Context) V } -type MakeFunc[V any] func(*Context) V -func (fn MakeFunc[V]) Make(c *Context) V { - return fn(c) -} - -type ArgMap = map[string] any -type ArgSlice = []any -type ArgList[V any] []V - -// Implementing the interface provides -// ability to build your own widgets, -// aka components. -type Widget interface { - // When the update channel is closed - // widget MUST end its work. - // Mostly made by looping over the - // updates range. - Serve(*Context) -} - -type DynamicWidget[W Widget] interface { - Maker[W] -} - -// Implementing the interface provides ability to -// be used as the root widget for contexts. -type RootWidget interface { - Widget - SetSub(Widget) -} - -// Implementing the interface provides way -// to know exactly what kind of updates -// the widget needs. -type Filterer interface { - // Return true if should filter the update - // and not send it inside the widget. - Filter(*Update, MessageMap) bool -} - -type FilterFunc func(*Update, MessageMap) bool -func (f FilterFunc) Filter( - u *Update, msgs MessageMap, -) bool { - return f(u, msgs) -} - -// General type function for faster typing. -type Func func(*Context) -func (f Func) Act(c *Context) { - f(c) -} -func (f Func) Serve(c *Context) { - f(c) -} - - -// The function that implements the Widget -// interface. -type WidgetFunc func(*Context) - -func (wf WidgetFunc) Serve(c *Context) { - wf(c) +type RootHandler interface { }