2023-07-09 01:28:59 +03:00
|
|
|
package behx
|
|
|
|
|
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
|
|
|
|
|
|
|
|
// Should be replaced with something that can be
|
|
|
|
// dinamicaly rendered. (WIP)
|
|
|
|
type ScreenText string
|
|
|
|
|
|
|
|
// Screen statement of the bot.
|
|
|
|
// Mostly what buttons to show.
|
|
|
|
type Screen struct {
|
|
|
|
// Text to be sent to the user when changing to the screen.
|
|
|
|
Text ScreenText
|
2023-07-12 02:02:33 +03:00
|
|
|
|
2023-07-12 00:33:51 +03:00
|
|
|
// The keyboard to be sent in the message part.
|
2023-07-12 02:02:33 +03:00
|
|
|
InlineKeyboardId KeyboardId
|
|
|
|
|
|
|
|
// Keyboard to be displayed on the screen.
|
|
|
|
KeyboardId KeyboardId
|
2023-07-09 01:28:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Map structure for the screens.
|
2023-07-12 00:33:51 +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-07-12 02:02:33 +03:00
|
|
|
func NewScreen(text ScreenText, ikbd KeyboardId, kbd KeyboardId) *Screen {
|
2023-07-09 01:28:59 +03:00
|
|
|
return &Screen {
|
|
|
|
Text: text,
|
2023-07-12 02:02:33 +03:00
|
|
|
InlineKeyboardId: ikbd,
|
|
|
|
KeyboardId: kbd,
|
2023-07-09 01:28:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-12 00:33:51 +03:00
|
|
|
// Rendering the screen text to string to be sent or printed.
|
|
|
|
func (st ScreenText) String() string {
|
|
|
|
return string(st)
|
|
|
|
}
|
|
|
|
|
2023-07-12 14:06:05 +03:00
|
|
|
// Renders output of the screen only to the side of the user.
|
2023-07-12 02:02:33 +03:00
|
|
|
func (s *Screen) Render(c *Context) error {
|
2023-07-12 14:06:05 +03:00
|
|
|
id := c.Id.ToTelegram()
|
2023-07-12 02:02:33 +03:00
|
|
|
|
2023-07-12 00:33:51 +03:00
|
|
|
msg := apix.NewMessage(id, s.Text.String())
|
|
|
|
|
2023-07-12 02:02:33 +03:00
|
|
|
if s.InlineKeyboardId != "" {
|
|
|
|
kbd, ok := c.B.Keyboards[s.InlineKeyboardId]
|
|
|
|
if !ok {
|
|
|
|
return KeyboardNotExistErr
|
2023-07-12 00:33:51 +03:00
|
|
|
}
|
2023-07-12 02:02:33 +03:00
|
|
|
msg.ReplyMarkup = kbd.ToTelegramInline()
|
2023-07-12 00:33:51 +03:00
|
|
|
}
|
|
|
|
|
2023-07-12 02:02:33 +03:00
|
|
|
_, err := c.B.Send(msg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-12 14:06:05 +03:00
|
|
|
msg = apix.NewMessage(id, ">")
|
|
|
|
// Checking if we need to resend the keyboard.
|
|
|
|
if s.KeyboardId != c.KeyboardId {
|
|
|
|
// Remove keyboard by default.
|
|
|
|
var tkbd any
|
|
|
|
tkbd = apix.NewRemoveKeyboard(true)
|
2023-07-12 02:02:33 +03:00
|
|
|
|
2023-07-12 14:06:05 +03:00
|
|
|
// Replace keyboard with the new one.
|
|
|
|
if s.KeyboardId != "" {
|
|
|
|
kbd, ok := c.B.Keyboards[s.KeyboardId]
|
|
|
|
if !ok {
|
|
|
|
return KeyboardNotExistErr
|
|
|
|
}
|
|
|
|
tkbd = kbd.ToTelegram()
|
2023-07-12 02:02:33 +03:00
|
|
|
}
|
|
|
|
|
2023-07-12 14:06:05 +03:00
|
|
|
msg.ReplyMarkup = tkbd
|
2023-07-12 00:33:51 +03:00
|
|
|
if _, err := c.B.Send(msg) ; err != nil {
|
2023-07-12 02:02:33 +03:00
|
|
|
return err
|
2023-07-12 00:33:51 +03:00
|
|
|
}
|
2023-07-12 14:06:05 +03:00
|
|
|
}
|
2023-07-12 02:02:33 +03:00
|
|
|
|
|
|
|
return nil
|
2023-07-12 00:33:51 +03:00
|
|
|
}
|
2023-07-09 01:28:59 +03:00
|
|
|
|