tg/src/behx/screen.go

88 lines
1.8 KiB
Go
Raw Normal View History

2023-07-09 01:28:59 +03:00
package behx
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
// The keyboard to be sent in the message part.
InlineKeyboardId KeyboardId
// Keyboard to be displayed on the screen.
KeyboardId KeyboardId
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(text ScreenText, ikbd KeyboardId, kbd KeyboardId) *Screen {
2023-07-09 01:28:59 +03:00
return &Screen {
Text: text,
InlineKeyboardId: ikbd,
KeyboardId: kbd,
2023-07-09 01:28:59 +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.
func (s *Screen) Render(c *Context) error {
2023-07-12 14:06:05 +03:00
id := c.Id.ToTelegram()
msg := apix.NewMessage(id, s.Text.String())
if s.InlineKeyboardId != "" {
kbd, ok := c.B.Keyboards[s.InlineKeyboardId]
if !ok {
return KeyboardNotExistErr
}
msg.ReplyMarkup = kbd.ToTelegramInline()
}
_, 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 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 14:06:05 +03:00
msg.ReplyMarkup = tkbd
if _, err := c.B.Send(msg) ; err != nil {
return err
}
2023-07-12 14:06:05 +03:00
}
return nil
}
2023-07-09 01:28:59 +03:00