2023-09-11 13:37:04 +03:00
|
|
|
package tg
|
|
|
|
|
|
|
|
import (
|
2023-09-16 14:34:17 +03:00
|
|
|
//tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
2023-09-11 13:37:04 +03:00
|
|
|
)
|
2023-09-25 23:43:22 +03:00
|
|
|
/*
|
2023-09-11 13:37:04 +03:00
|
|
|
type Page struct {
|
2023-09-25 19:52:57 +03:00
|
|
|
Action Action
|
2023-09-11 13:37:04 +03:00
|
|
|
Text string
|
|
|
|
SubWidget Widget
|
2023-09-25 19:52:57 +03:00
|
|
|
Inline *InlineWidget
|
|
|
|
Reply *ReplyWidget
|
2023-09-11 13:37:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return new page with the specified text.
|
2023-09-21 12:03:54 +03:00
|
|
|
func NewPage() *Page {
|
2023-09-11 13:37:04 +03:00
|
|
|
ret := &Page{}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2023-09-21 12:03:54 +03:00
|
|
|
func (p *Page) WithText(text string) *Page {
|
|
|
|
p.Text = text
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2023-09-11 13:37:04 +03:00
|
|
|
// Set the inline keyboard.
|
2023-09-25 19:52:57 +03:00
|
|
|
func (p *Page) WithInline(inline *InlineWidget) *Page {
|
2023-09-11 13:37:04 +03:00
|
|
|
p.Inline = inline
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the reply keyboard.
|
2023-09-25 19:52:57 +03:00
|
|
|
func (p *Page) WithReply(reply *ReplyWidget) *Page {
|
2023-09-11 13:37:04 +03:00
|
|
|
p.Reply = reply
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the action to be run before serving.
|
|
|
|
func (p *Page) WithAction(a Action) *Page {
|
|
|
|
p.Action = a
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alias to with action to simpler define actions.
|
|
|
|
func (p *Page) ActionFunc(fn ActionFunc) *Page {
|
|
|
|
return p.WithAction(fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the sub widget that will get the skipped
|
|
|
|
// updates.
|
|
|
|
func (p *Page) WithSub(sub Widget) *Page {
|
|
|
|
p.SubWidget = sub
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2023-09-16 14:34:17 +03:00
|
|
|
|
|
|
|
func (p *Page) Render(
|
|
|
|
sid SessionId, bot *Bot,
|
|
|
|
) ([]*SendConfig) {
|
|
|
|
reply := p.Reply
|
|
|
|
inline := p.Inline
|
|
|
|
|
|
|
|
ret := []*SendConfig{}
|
|
|
|
|
|
|
|
if p.Text != "" {
|
|
|
|
cfg := NewMessage(p.Text).SendConfig(sid, bot).
|
|
|
|
WithName("page/text")
|
|
|
|
ret = append(ret, cfg)
|
|
|
|
}
|
|
|
|
if inline != nil {
|
|
|
|
cfg := inline.SendConfig(sid, bot).
|
|
|
|
WithName("page/inline")
|
|
|
|
ret = append(ret, cfg)
|
|
|
|
}
|
|
|
|
if p.Reply != nil {
|
|
|
|
cfg := reply.SendConfig(sid, bot).
|
|
|
|
WithName("page/reply")
|
|
|
|
ret = append(ret, cfg)
|
2023-09-11 13:37:04 +03:00
|
|
|
}
|
|
|
|
|
2023-09-16 14:34:17 +03:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Page) Filter(
|
|
|
|
u *Update, msgs MessageMap,
|
|
|
|
) bool {
|
|
|
|
return false
|
|
|
|
}
|
2023-09-11 13:37:04 +03:00
|
|
|
|
2023-09-20 22:48:35 +03:00
|
|
|
func (p *Page) Serve(c *Context) {
|
2023-09-25 19:27:25 +03:00
|
|
|
pth := c.Path()
|
2023-09-11 13:37:04 +03:00
|
|
|
if p.Action != nil {
|
2023-09-16 14:34:17 +03:00
|
|
|
c.Run(p.Action, c.Update)
|
2023-09-25 19:27:25 +03:00
|
|
|
if pth != c.Path() {
|
|
|
|
// If we went somewhere else then do nothing.
|
|
|
|
return
|
|
|
|
}
|
2023-09-11 13:37:04 +03:00
|
|
|
}
|
2023-09-20 22:48:35 +03:00
|
|
|
msgs, _ := c.Render(p)
|
|
|
|
inlineMsg := msgs["page/inline"]
|
2023-09-11 13:37:04 +03:00
|
|
|
|
2023-09-20 23:38:29 +03:00
|
|
|
subUpdates := c.RunWidget(p.SubWidget, c.Arg)
|
2023-09-16 14:34:17 +03:00
|
|
|
defer subUpdates.Close()
|
|
|
|
|
2023-09-20 22:48:35 +03:00
|
|
|
inlineUpdates := c.RunWidget(p.Inline)
|
2023-09-16 14:34:17 +03:00
|
|
|
defer inlineUpdates.Close()
|
|
|
|
|
2023-09-20 22:48:35 +03:00
|
|
|
replyUpdates := c.RunWidget(p.Reply)
|
2023-09-16 14:34:17 +03:00
|
|
|
defer replyUpdates.Close()
|
|
|
|
|
|
|
|
subFilter, subFilterOk := p.SubWidget.(Filterer)
|
2023-09-20 22:48:35 +03:00
|
|
|
for u := range c.Input() {
|
2023-09-16 14:34:17 +03:00
|
|
|
switch {
|
|
|
|
case !p.Inline.Filter(u, MessageMap{"": inlineMsg}) :
|
|
|
|
inlineUpdates.Send(u)
|
|
|
|
case !p.Reply.Filter(u, msgs) :
|
|
|
|
replyUpdates.Send(u )
|
|
|
|
case p.SubWidget != nil :
|
|
|
|
if subFilterOk {
|
2023-09-16 15:40:30 +03:00
|
|
|
if !subFilter.Filter(u, msgs) {
|
2023-09-16 14:34:17 +03:00
|
|
|
subUpdates.Send(u)
|
2023-09-11 13:37:04 +03:00
|
|
|
}
|
2023-09-16 14:34:17 +03:00
|
|
|
} else {
|
|
|
|
subUpdates.Send(u)
|
2023-09-11 13:37:04 +03:00
|
|
|
}
|
2023-09-16 14:34:17 +03:00
|
|
|
default:
|
2023-09-11 13:37:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-09-25 23:43:22 +03:00
|
|
|
*/
|
2023-09-11 13:37:04 +03:00
|
|
|
|