tg/src/tx/screen.go

132 lines
2.6 KiB
Go
Raw Normal View History

package tx
2023-07-09 01:28:59 +03:00
import (
apix "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
2023-07-09 01:28:59 +03:00
// Unique identifier for the screen.
type ScreenId string
// Screen statement of the bot.
// Mostly what buttons to show.
type Screen struct {
Id ScreenId
2023-08-15 16:02:14 +03:00
// The text to be displayed when the screen is
// reached.
Text string
// The keyboard to be sent in the message part.
2023-08-15 16:02:14 +03:00
InlineKeyboard *Keyboard
// Keyboard to be displayed on the screen.
2023-08-15 16:02:14 +03:00
Keyboard *Keyboard
// Action called on the reaching the screen.
Action *action
2023-07-09 01:28:59 +03:00
}
// Map structure for the screens.
type ScreenMap map[ScreenId]*Screen
2023-07-09 01:28:59 +03:00
// Returns the new screen with specified Text and Keyboard.
func NewScreen(id ScreenId) *Screen {
return &Screen{
Id: id,
2023-07-09 01:28:59 +03:00
}
}
// Returns the screen with specified text printing on appearing.
2023-08-15 16:02:14 +03:00
func (s *Screen) WithText(text string) *Screen {
s.Text = text
return s
}
2023-08-15 16:02:14 +03:00
func (s *Screen) WithInlineKeyboard(ikbd *Keyboard) *Screen {
s.InlineKeyboard = ikbd
return s
}
2023-08-15 16:02:14 +03:00
func (s *Screen) WithIKeyboard(ikbd *Keyboard) *Screen {
return s.WithInlineKeyboard(ikbd)
}
func (s *Screen) WithKeyboard(kbd *Keyboard) *Screen {
s.Keyboard = kbd
return s
}
func (s *Screen) WithAction(a Action) *Screen {
s.Action = newAction(a)
return s
}
func (s *Screen) ActionFunc(a ActionFunc) *Screen {
return s.WithAction(a)
}
2023-07-12 14:06:05 +03:00
// Renders output of the screen only to the side of the user.
func (s *Screen) Render(c *context) error {
2023-07-12 14:06:05 +03:00
id := c.Id.ToTelegram()
2023-08-15 16:02:14 +03:00
kbd := s.Keyboard
iKbd := s.InlineKeyboard
var ch [2]apix.Chattable
var txt string
// Screen text and inline keyboard.
if s.Text != "" {
txt = s.Text
} else if iKbd != nil {
if iKbd.Text != "" {
txt = iKbd.Text
} else {
// Default to send the keyboard.
txt = ">"
}
}
2023-08-15 16:02:14 +03:00
if txt != "" {
msg := apix.NewMessage(id, txt)
if iKbd != nil {
msg.ReplyMarkup = iKbd.toTelegramInline()
} else if kbd != nil {
msg.ReplyMarkup = kbd.toTelegramReply()
if _, err := c.Bot.Send(msg); err != nil {
2023-08-15 16:02:14 +03:00
return err
}
return nil
} else {
msg.ReplyMarkup = apix.NewRemoveKeyboard(true)
if _, err := c.Bot.Send(msg); err != nil {
2023-08-15 16:02:14 +03:00
return err
}
return nil
}
ch[0] = msg
}
2023-08-15 16:02:14 +03:00
// Screen text and reply keyboard.
txt = ""
if kbd != nil {
if kbd.Text != "" {
txt = kbd.Text
} else {
txt = ">"
}
2023-08-15 16:02:14 +03:00
msg := apix.NewMessage(id, txt)
msg.ReplyMarkup = kbd.toTelegramReply()
ch[1] = msg
} else {
// Removing keyboard if there is none.
msg := apix.NewMessage(id, ">")
msg.ReplyMarkup = apix.NewRemoveKeyboard(true)
ch[1] = msg
}
2023-08-15 16:02:14 +03:00
for _, m := range ch {
if m != nil {
if _, err := c.Bot.Send(m); err != nil {
2023-08-15 16:02:14 +03:00
return err
}
}
}
return nil
}