page.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package tg
  2. import (
  3. //tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
  4. )
  5. /*
  6. type Page struct {
  7. Action Action
  8. Text string
  9. SubWidget Widget
  10. Inline *InlineWidget
  11. Reply *ReplyWidget
  12. }
  13. // Return new page with the specified text.
  14. func NewPage() *Page {
  15. ret := &Page{}
  16. return ret
  17. }
  18. func (p *Page) WithText(text string) *Page {
  19. p.Text = text
  20. return p
  21. }
  22. // Set the inline keyboard.
  23. func (p *Page) WithInline(inline *InlineWidget) *Page {
  24. p.Inline = inline
  25. return p
  26. }
  27. // Set the reply keyboard.
  28. func (p *Page) WithReply(reply *ReplyWidget) *Page {
  29. p.Reply = reply
  30. return p
  31. }
  32. // Set the action to be run before serving.
  33. func (p *Page) WithAction(a Action) *Page {
  34. p.Action = a
  35. return p
  36. }
  37. // Alias to with action to simpler define actions.
  38. func (p *Page) ActionFunc(fn ActionFunc) *Page {
  39. return p.WithAction(fn)
  40. }
  41. // Set the sub widget that will get the skipped
  42. // updates.
  43. func (p *Page) WithSub(sub Widget) *Page {
  44. p.SubWidget = sub
  45. return p
  46. }
  47. func (p *Page) Render(
  48. sid SessionId, bot *Bot,
  49. ) ([]*SendConfig) {
  50. reply := p.Reply
  51. inline := p.Inline
  52. ret := []*SendConfig{}
  53. if p.Text != "" {
  54. cfg := NewMessage(p.Text).SendConfig(sid, bot).
  55. WithName("page/text")
  56. ret = append(ret, cfg)
  57. }
  58. if inline != nil {
  59. cfg := inline.SendConfig(sid, bot).
  60. WithName("page/inline")
  61. ret = append(ret, cfg)
  62. }
  63. if p.Reply != nil {
  64. cfg := reply.SendConfig(sid, bot).
  65. WithName("page/reply")
  66. ret = append(ret, cfg)
  67. }
  68. return ret
  69. }
  70. func (p *Page) Filter(
  71. u *Update, msgs MessageMap,
  72. ) bool {
  73. return false
  74. }
  75. func (p *Page) Serve(c *Context) {
  76. pth := c.Path()
  77. if p.Action != nil {
  78. c.Run(p.Action, c.Update)
  79. if pth != c.Path() {
  80. // If we went somewhere else then do nothing.
  81. return
  82. }
  83. }
  84. msgs, _ := c.Render(p)
  85. inlineMsg := msgs["page/inline"]
  86. subUpdates := c.RunWidget(p.SubWidget, c.Arg)
  87. defer subUpdates.Close()
  88. inlineUpdates := c.RunWidget(p.Inline)
  89. defer inlineUpdates.Close()
  90. replyUpdates := c.RunWidget(p.Reply)
  91. defer replyUpdates.Close()
  92. subFilter, subFilterOk := p.SubWidget.(Filterer)
  93. for u := range c.Input() {
  94. switch {
  95. case !p.Inline.Filter(u, MessageMap{"": inlineMsg}) :
  96. inlineUpdates.Send(u)
  97. case !p.Reply.Filter(u, msgs) :
  98. replyUpdates.Send(u )
  99. case p.SubWidget != nil :
  100. if subFilterOk {
  101. if !subFilter.Filter(u, msgs) {
  102. subUpdates.Send(u)
  103. }
  104. } else {
  105. subUpdates.Send(u)
  106. }
  107. default:
  108. }
  109. }
  110. }
  111. */