2023-08-10 15:49:25 +03:00
|
|
|
package tx
|
2023-07-09 01:28:59 +03:00
|
|
|
|
2023-07-12 00:33:51 +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 {
|
2023-08-10 15:49:25 +03:00
|
|
|
Id ScreenId
|
2023-08-15 16:02:14 +03:00
|
|
|
// The text to be displayed when the screen is
|
|
|
|
// reached.
|
|
|
|
Text string
|
2023-07-12 00:33:51 +03:00
|
|
|
// The keyboard to be sent in the message part.
|
2023-08-15 16:02:14 +03:00
|
|
|
InlineKeyboard *Keyboard
|
2023-07-12 02:02:33 +03:00
|
|
|
// Keyboard to be displayed on the screen.
|
2023-08-15 16:02:14 +03:00
|
|
|
Keyboard *Keyboard
|
2023-08-11 10:41:17 +03:00
|
|
|
// Action called on the reaching the screen.
|
2023-08-16 17:25:56 +03:00
|
|
|
Action *action
|
2023-07-09 01:28:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Map structure for the screens.
|
2023-08-10 15:49:25 +03:00
|
|
|
type ScreenMap map[ScreenId]*Screen
|
2023-07-09 01:28:59 +03:00
|
|
|
|
|
|
|
// Returns the new screen with specified Text and Keyboard.
|
2023-08-10 15:49:25 +03:00
|
|
|
func NewScreen(id ScreenId) *Screen {
|
|
|
|
return &Screen{
|
|
|
|
Id: id,
|
2023-07-09 01:28:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-10 15:49:25 +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 {
|
2023-08-10 15:49:25 +03:00
|
|
|
s.Text = text
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2023-08-15 16:02:14 +03:00
|
|
|
func (s *Screen) WithInlineKeyboard(ikbd *Keyboard) *Screen {
|
|
|
|
s.InlineKeyboard = ikbd
|
2023-08-10 15:49:25 +03:00
|
|
|
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
|
2023-08-10 15:49:25 +03:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2023-08-11 10:41:17 +03:00
|
|
|
func (s *Screen) WithAction(a Action) *Screen {
|
2023-08-16 17:25:56 +03:00
|
|
|
s.Action = newAction(a)
|
2023-08-11 10:41:17 +03:00
|
|
|
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.
|
2023-08-18 13:46:10 +03:00
|
|
|
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-07-12 00:33:51 +03:00
|
|
|
}
|
|
|
|
}
|
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()
|
2023-08-18 13:46:10 +03:00
|
|
|
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)
|
2023-08-18 13:46:10 +03:00
|
|
|
if _, err := c.Bot.Send(msg); err != nil {
|
2023-08-15 16:02:14 +03:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ch[0] = msg
|
2023-07-12 02:02:33 +03:00
|
|
|
}
|
2023-08-10 15:49:25 +03:00
|
|
|
|
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-07-12 02:02:33 +03:00
|
|
|
}
|
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-10 15:49:25 +03:00
|
|
|
|
2023-08-15 16:02:14 +03:00
|
|
|
for _, m := range ch {
|
|
|
|
if m != nil {
|
2023-08-18 13:46:10 +03:00
|
|
|
if _, err := c.Bot.Send(m); err != nil {
|
2023-08-15 16:02:14 +03:00
|
|
|
return err
|
|
|
|
}
|
2023-07-12 00:33:51 +03:00
|
|
|
}
|
2023-08-10 15:49:25 +03:00
|
|
|
}
|
|
|
|
|
2023-07-12 02:02:33 +03:00
|
|
|
return nil
|
2023-07-12 00:33:51 +03:00
|
|
|
}
|