Simplified behaviour definition functions.
This commit is contained in:
parent
03a2f60436
commit
fbfa0ec388
15 changed files with 297 additions and 268 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -1,3 +1,4 @@
|
||||||
exe
|
exe
|
||||||
*.exe
|
*.exe
|
||||||
*.swp
|
*.swp
|
||||||
|
tmp
|
||||||
|
|
104
cmd/test/main.go
104
cmd/test/main.go
|
@ -4,101 +4,56 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
//tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
"github.com/mojosa-software/got/src/tx"
|
||||||
"boteval/src/behx"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var rootKbd = behx.NewKeyboard(
|
var navKeyboard = tx.NewKeyboard("nav").Row(
|
||||||
behx.NewButtonRow(
|
tx.NewButton().WithText("Inc/Dec").ScreenChange("inc/dec"),
|
||||||
behx.NewButton(
|
).Row(
|
||||||
"Increment",
|
tx.NewButton().WithText("Upper case").ScreenChange("upper-case"),
|
||||||
behx.NewCustomAction(func(c *behx.Context){
|
tx.NewButton().WithText("Lower case").ScreenChange("lower-case"),
|
||||||
|
)
|
||||||
|
|
||||||
|
var incKeyboard = tx.NewKeyboard("inc/dec").Row(
|
||||||
|
tx.NewButton().WithText("+").ActionFunc(func(c *tx.Context) {
|
||||||
counter := c.V["counter"].(*int)
|
counter := c.V["counter"].(*int)
|
||||||
*counter++
|
*counter++
|
||||||
c.Sendf("%d", *counter)
|
c.Sendf("%d", *counter)
|
||||||
}),
|
}),
|
||||||
),
|
tx.NewButton().WithText("-").ActionFunc(func(c *tx.Context) {
|
||||||
behx.NewButton(
|
|
||||||
"Decrement",
|
|
||||||
behx.NewCustomAction(func(c *behx.Context){
|
|
||||||
counter := c.V["counter"].(*int)
|
counter := c.V["counter"].(*int)
|
||||||
*counter--
|
*counter--
|
||||||
c.Sendf("%d", *counter)
|
c.Sendf("%d", *counter)
|
||||||
}),
|
}),
|
||||||
),
|
|
||||||
),
|
|
||||||
behx.NewButtonRow(
|
|
||||||
behx.NewButton("To second screen", behx.NewScreenChange("second")),
|
|
||||||
),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var secondKbd = behx.NewKeyboard(
|
var startScreen = tx.NewScreen("start").
|
||||||
behx.NewButtonRow(
|
WithText("The bot started!").
|
||||||
behx.NewButton(
|
Keyboard("nav")
|
||||||
"❤",
|
|
||||||
behx.NewScreenChange("start"),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
var inlineKbd = behx.NewKeyboard(
|
var incScreen = tx.NewScreen("inc/dec").
|
||||||
behx.NewButtonRow(
|
WithText("The screen shows how user separated data works").
|
||||||
behx.NewButton(
|
IKeyboard("inc/dec").
|
||||||
"INLINE PRESS ME",
|
Keyboard("nav")
|
||||||
behx.NewCustomAction(func(c *behx.Context){
|
|
||||||
log.Println("INLINE pressed the button!")
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
behx.NewButton("INLINE PRESS ME 2", behx.NewCustomAction(func(c *behx.Context){
|
|
||||||
log.Println("INLINE pressed another button!")
|
|
||||||
})),
|
|
||||||
),
|
|
||||||
behx.NewButtonRow(
|
|
||||||
behx.NewButton(
|
|
||||||
"INLINE PRESS ME 3",
|
|
||||||
behx.ScreenChange("second"),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
var beh = tx.NewBehaviour().
|
||||||
var startScreen = behx.NewScreen(
|
OnStartFunc(func(c *tx.Context) {
|
||||||
"Hello, World!",
|
// The function will be called every time
|
||||||
"inline",
|
// the bot is started.
|
||||||
"root",
|
|
||||||
)
|
|
||||||
|
|
||||||
var secondScreen = behx.NewScreen(
|
|
||||||
"Second screen!",
|
|
||||||
"",
|
|
||||||
"second",
|
|
||||||
)
|
|
||||||
|
|
||||||
var behaviour = behx.NewBehaviour(
|
|
||||||
behx.NewCustomAction(func(c *behx.Context){
|
|
||||||
// This way we provide counter for EACH user.
|
|
||||||
c.V["counter"] = new(int)
|
c.V["counter"] = new(int)
|
||||||
|
|
||||||
// Do NOT forget to change to some of the screens
|
|
||||||
// since they are the ones who provide behaviour
|
|
||||||
// definition.
|
|
||||||
c.ChangeScreen("start")
|
c.ChangeScreen("start")
|
||||||
}),
|
}).WithKeyboards(
|
||||||
behx.ScreenMap{
|
navKeyboard,
|
||||||
"start": startScreen,
|
incKeyboard,
|
||||||
"second": secondScreen,
|
).WithScreens(
|
||||||
},
|
startScreen,
|
||||||
behx.KeyboardMap{
|
incScreen,
|
||||||
"root": rootKbd,
|
|
||||||
"inline": inlineKbd,
|
|
||||||
"second": secondKbd,
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
token := os.Getenv("BOT_TOKEN")
|
token := os.Getenv("BOT_TOKEN")
|
||||||
|
|
||||||
bot, err := behx.NewBot(token, behaviour, nil)
|
bot, err := tx.NewBot(token, beh, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
}
|
}
|
||||||
|
@ -108,4 +63,3 @@ func main() {
|
||||||
log.Printf("Authorized on account %s", bot.Self.UserName)
|
log.Printf("Authorized on account %s", bot.Self.UserName)
|
||||||
bot.Run()
|
bot.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
2
go.mod
2
go.mod
|
@ -1,4 +1,4 @@
|
||||||
module boteval
|
module github.com/mojosa-software/got
|
||||||
|
|
||||||
go 1.20
|
go 1.20
|
||||||
|
|
||||||
|
|
|
@ -1,39 +0,0 @@
|
||||||
package behx
|
|
||||||
|
|
||||||
// The package implements
|
|
||||||
// behaviour for the Telegram bots.
|
|
||||||
|
|
||||||
|
|
||||||
// The type describes behaviour for the bot.
|
|
||||||
type Behaviour struct {
|
|
||||||
Start Action
|
|
||||||
Screens ScreenMap
|
|
||||||
Keyboards KeyboardMap
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewBehaviour(
|
|
||||||
start Action,
|
|
||||||
screens ScreenMap,
|
|
||||||
keyboards KeyboardMap,
|
|
||||||
) *Behaviour {
|
|
||||||
return &Behaviour{
|
|
||||||
start, screens, keyboards,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check whether the screen exists in the behaviour.
|
|
||||||
func (beh *Behaviour) ScreenExists(id ScreenId) bool {
|
|
||||||
_, ok := beh.Screens[id]
|
|
||||||
return ok
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns the screen by it's ID.
|
|
||||||
func (beh *Behaviour) GetScreen(id ScreenId) *Screen {
|
|
||||||
if !beh.ScreenExists(id) {
|
|
||||||
panic(ScreenNotExistErr)
|
|
||||||
}
|
|
||||||
|
|
||||||
screen := beh.Screens[id]
|
|
||||||
return screen
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,13 +0,0 @@
|
||||||
package behx
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
ScreenNotExistErr = errors.New("screen does not exist")
|
|
||||||
SessionNotExistErr = errors.New("session does not exist")
|
|
||||||
KeyboardNotExistErr = errors.New("keyboard does not exist")
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package behx
|
package tx
|
||||||
|
|
||||||
// Implementing the intereface lets you
|
// Implementing the intereface lets you
|
||||||
// provide behaviour for the buttons etc.
|
// provide behaviour for the buttons etc.
|
||||||
|
@ -7,29 +7,21 @@ type Action interface {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Customized action for the bot.
|
// Customized action for the bot.
|
||||||
type CustomAction func(*Context)
|
type ActionFunc func(*Context)
|
||||||
|
|
||||||
// The type implements changing screen to the underlying ScreenId
|
// The type implements changing screen to the underlying ScreenId
|
||||||
type ScreenChange ScreenId
|
type ScreenChange ScreenId
|
||||||
|
|
||||||
// Returns new ScreenChange.
|
|
||||||
func NewScreenChange(screen string) ScreenChange {
|
|
||||||
return ScreenChange(screen)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns new CustomAction.
|
|
||||||
func NewCustomAction(fn func(*Context)) CustomAction {
|
|
||||||
return CustomAction(fn)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (sc ScreenChange) Act(c *Context) {
|
func (sc ScreenChange) Act(c *Context) {
|
||||||
|
if !c.B.ScreenExist(ScreenId(sc)) {
|
||||||
|
panic(ScreenNotExistErr)
|
||||||
|
}
|
||||||
err := c.ChangeScreen(ScreenId(sc))
|
err := c.ChangeScreen(ScreenId(sc))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ca CustomAction) Act(c *Context) {
|
func (af ActionFunc) Act(c *Context) {
|
||||||
ca(c)
|
af(c)
|
||||||
}
|
}
|
||||||
|
|
86
src/tx/beh.go
Normal file
86
src/tx/beh.go
Normal file
|
@ -0,0 +1,86 @@
|
||||||
|
package tx
|
||||||
|
|
||||||
|
// The package implements
|
||||||
|
// behaviour for the Telegram bots.
|
||||||
|
|
||||||
|
// The type describes behaviour for the bot.
|
||||||
|
type Behaviour struct {
|
||||||
|
Start Action
|
||||||
|
Screens ScreenMap
|
||||||
|
Keyboards KeyboardMap
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns new empty behaviour.
|
||||||
|
func NewBehaviour() *Behaviour {
|
||||||
|
return &Behaviour{
|
||||||
|
Screens: make(ScreenMap),
|
||||||
|
Keyboards: make(KeyboardMap),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Behaviour) WithStart(a Action) *Behaviour {
|
||||||
|
b.Start = a
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Behaviour) OnStartFunc(
|
||||||
|
fn ActionFunc,
|
||||||
|
) *Behaviour {
|
||||||
|
return b.WithStart(fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Behaviour) OnStartChangeScreen(
|
||||||
|
id ScreenId,
|
||||||
|
) *Behaviour {
|
||||||
|
return b.WithStart(ScreenChange(id))
|
||||||
|
}
|
||||||
|
|
||||||
|
// The function sets keyboards.
|
||||||
|
func (b *Behaviour) WithKeyboards(
|
||||||
|
kbds ...*Keyboard,
|
||||||
|
) *Behaviour {
|
||||||
|
for _, kbd := range kbds {
|
||||||
|
if kbd.Id == "" {
|
||||||
|
panic("empty keyboard ID")
|
||||||
|
}
|
||||||
|
_, ok := b.Keyboards[kbd.Id]
|
||||||
|
if ok {
|
||||||
|
panic("duplicate keyboard IDs")
|
||||||
|
}
|
||||||
|
b.Keyboards[kbd.Id] = kbd
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// The function sets screens.
|
||||||
|
func (b *Behaviour) WithScreens(
|
||||||
|
screens ...*Screen,
|
||||||
|
) *Behaviour {
|
||||||
|
for _, screen := range screens {
|
||||||
|
if screen.Id == "" {
|
||||||
|
panic("empty screen ID")
|
||||||
|
}
|
||||||
|
_, ok := b.Screens[screen.Id]
|
||||||
|
if ok {
|
||||||
|
panic("duplicate keyboard IDs")
|
||||||
|
}
|
||||||
|
b.Screens[screen.Id] = screen
|
||||||
|
}
|
||||||
|
return b
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check whether the screen exists in the behaviour.
|
||||||
|
func (beh *Behaviour) ScreenExist(id ScreenId) bool {
|
||||||
|
_, ok := beh.Screens[id]
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns the screen by it's ID.
|
||||||
|
func (beh *Behaviour) GetScreen(id ScreenId) *Screen {
|
||||||
|
if !beh.ScreenExist(id) {
|
||||||
|
panic(ScreenNotExistErr)
|
||||||
|
}
|
||||||
|
|
||||||
|
screen := beh.Screens[id]
|
||||||
|
return screen
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package behx
|
package tx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
apix "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
apix "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||||
|
@ -28,7 +28,6 @@ func NewBot(token string, beh *Behaviour, sessions SessionMap) (*Bot, error) {
|
||||||
BotAPI: bot,
|
BotAPI: bot,
|
||||||
Behaviour: beh,
|
Behaviour: beh,
|
||||||
sessions: make(SessionMap),
|
sessions: make(SessionMap),
|
||||||
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,7 +51,6 @@ func (bot *Bot) Run() error {
|
||||||
bot.sessions.Add(sid)
|
bot.sessions.Add(sid)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// The "start" command resets the bot
|
// The "start" command resets the bot
|
||||||
// by executing the Start Action.
|
// by executing the Start Action.
|
||||||
if u.Message.IsCommand() {
|
if u.Message.IsCommand() {
|
||||||
|
@ -83,4 +81,3 @@ func (bot *Bot) Run() error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package behx
|
package tx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
apix "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
apix "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||||
|
@ -18,11 +18,31 @@ type ButtonMap map[string] *Button
|
||||||
type ButtonRow []*Button
|
type ButtonRow []*Button
|
||||||
|
|
||||||
// Returns new button with specified text and action.
|
// Returns new button with specified text and action.
|
||||||
func NewButton(text string, action Action) *Button {
|
func NewButton() *Button {
|
||||||
return &Button{
|
return &Button{}
|
||||||
Text: text,
|
|
||||||
Action: action,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (btn *Button) WithText(text string) *Button {
|
||||||
|
btn.Text = text
|
||||||
|
return btn
|
||||||
|
}
|
||||||
|
|
||||||
|
func (btn *Button) WithUrl(url string) *Button {
|
||||||
|
btn.Url = url
|
||||||
|
return btn
|
||||||
|
}
|
||||||
|
|
||||||
|
func (btn *Button) WithAction(a Action) *Button {
|
||||||
|
btn.Action = a
|
||||||
|
return btn
|
||||||
|
}
|
||||||
|
|
||||||
|
func (btn *Button) ActionFunc(fn ActionFunc) *Button {
|
||||||
|
return btn.WithAction(fn)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (btn *Button) ScreenChange(sc ScreenChange) *Button {
|
||||||
|
return btn.WithAction(sc)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewButtonData(text string, data string, action Action) *Button {
|
func NewButtonData(text string, data string, action Action) *Button {
|
||||||
|
@ -71,4 +91,3 @@ func (btn *Button) Key() string {
|
||||||
func NewButtonRow(btns ...*Button) ButtonRow {
|
func NewButtonRow(btns ...*Button) ButtonRow {
|
||||||
return btns
|
return btns
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,9 @@
|
||||||
package behx
|
package tx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
apix "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
apix "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
// The type represents way to interact with user in
|
// The type represents way to interact with user in
|
||||||
|
@ -56,7 +57,7 @@ func (c *Context) ChangeScreen(screenId ScreenId) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if !c.B.ScreenExists(screenId) {
|
if !c.B.ScreenExist(screenId) {
|
||||||
return ScreenNotExistErr
|
return ScreenNotExistErr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,4 +80,3 @@ func (c *Context) Send(text string) error {
|
||||||
func (c *Context) Sendf(format string, v ...any) error {
|
func (c *Context) Sendf(format string, v ...any) error {
|
||||||
return c.Send(fmt.Sprintf(format, v...))
|
return c.Send(fmt.Sprintf(format, v...))
|
||||||
}
|
}
|
||||||
|
|
11
src/tx/errors.go
Normal file
11
src/tx/errors.go
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
package tx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ScreenNotExistErr = errors.New("screen does not exist")
|
||||||
|
SessionNotExistErr = errors.New("session does not exist")
|
||||||
|
KeyboardNotExistErr = errors.New("keyboard does not exist")
|
||||||
|
)
|
|
@ -1,8 +1,9 @@
|
||||||
package behx
|
package tx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
apix "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
apix "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||||
)
|
)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
var otherKeyboard = tgbotapi.NewReplyKeyboard(
|
var otherKeyboard = tgbotapi.NewReplyKeyboard(
|
||||||
tgbotapi.NewKeyboardButtonRow(
|
tgbotapi.NewKeyboardButtonRow(
|
||||||
|
@ -22,20 +23,27 @@ type KeyboardId string
|
||||||
// The type represents reply keyboard which
|
// The type represents reply keyboard which
|
||||||
// is supposed to be showed on a Screen.
|
// is supposed to be showed on a Screen.
|
||||||
type Keyboard struct {
|
type Keyboard struct {
|
||||||
|
Id KeyboardId
|
||||||
Rows []ButtonRow
|
Rows []ButtonRow
|
||||||
}
|
}
|
||||||
|
|
||||||
type KeyboardMap map[KeyboardId]*Keyboard
|
type KeyboardMap map[KeyboardId]*Keyboard
|
||||||
|
|
||||||
// Return the new reply keyboard with rows as specified.
|
// Return the new reply keyboard with rows as specified.
|
||||||
func NewKeyboard(rows ...ButtonRow) *Keyboard {
|
func NewKeyboard(id KeyboardId) *Keyboard {
|
||||||
return &Keyboard{
|
return &Keyboard{
|
||||||
Rows: rows,
|
Id: id,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Adds a new button row to the current keyboard.
|
||||||
|
func (kbd *Keyboard) Row(btns ...*Button) *Keyboard {
|
||||||
|
kbd.Rows = append(kbd.Rows, btns)
|
||||||
|
return kbd
|
||||||
|
}
|
||||||
|
|
||||||
// Convert the Keyboard to the Telegram API type.
|
// Convert the Keyboard to the Telegram API type.
|
||||||
func (kbd *Keyboard) ToTelegram() apix.ReplyKeyboardMarkup {
|
func (kbd *Keyboard) toTelegram() apix.ReplyKeyboardMarkup {
|
||||||
rows := [][]apix.KeyboardButton{}
|
rows := [][]apix.KeyboardButton{}
|
||||||
for _, row := range kbd.Rows {
|
for _, row := range kbd.Rows {
|
||||||
buttons := []apix.KeyboardButton{}
|
buttons := []apix.KeyboardButton{}
|
||||||
|
@ -48,7 +56,7 @@ func (kbd *Keyboard) ToTelegram() apix.ReplyKeyboardMarkup {
|
||||||
return apix.NewReplyKeyboard(rows...)
|
return apix.NewReplyKeyboard(rows...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (kbd *Keyboard) ToTelegramInline() apix.InlineKeyboardMarkup {
|
func (kbd *Keyboard) toTelegramInline() apix.InlineKeyboardMarkup {
|
||||||
rows := [][]apix.InlineKeyboardButton{}
|
rows := [][]apix.InlineKeyboardButton{}
|
||||||
for _, row := range kbd.Rows {
|
for _, row := range kbd.Rows {
|
||||||
buttons := []apix.InlineKeyboardButton{}
|
buttons := []apix.InlineKeyboardButton{}
|
||||||
|
@ -72,4 +80,3 @@ func (kbd *Keyboard) buttonMap() ButtonMap {
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
package behx
|
package tx
|
||||||
|
|
||||||
// The package implements behaviourial
|
// The package implements behaviourial
|
||||||
// definition for the Telegram bots through the API.
|
// definition for the Telegram bots through the API.
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package behx
|
package tx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
apix "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
apix "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||||
|
@ -14,6 +14,8 @@ type ScreenText string
|
||||||
// Screen statement of the bot.
|
// Screen statement of the bot.
|
||||||
// Mostly what buttons to show.
|
// Mostly what buttons to show.
|
||||||
type Screen struct {
|
type Screen struct {
|
||||||
|
Id ScreenId
|
||||||
|
|
||||||
// Text to be sent to the user when changing to the screen.
|
// Text to be sent to the user when changing to the screen.
|
||||||
Text ScreenText
|
Text ScreenText
|
||||||
|
|
||||||
|
@ -28,14 +30,28 @@ type Screen struct {
|
||||||
type ScreenMap map[ScreenId]*Screen
|
type ScreenMap map[ScreenId]*Screen
|
||||||
|
|
||||||
// Returns the new screen with specified Text and Keyboard.
|
// Returns the new screen with specified Text and Keyboard.
|
||||||
func NewScreen(text ScreenText, ikbd KeyboardId, kbd KeyboardId) *Screen {
|
func NewScreen(id ScreenId) *Screen {
|
||||||
return &Screen{
|
return &Screen{
|
||||||
Text: text,
|
Id: id,
|
||||||
InlineKeyboardId: ikbd,
|
|
||||||
KeyboardId: kbd,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Returns the screen with specified text printing on appearing.
|
||||||
|
func (s *Screen) WithText(text ScreenText) *Screen {
|
||||||
|
s.Text = text
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Screen) IKeyboard(kbdId KeyboardId) *Screen {
|
||||||
|
s.InlineKeyboardId = kbdId
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Screen) Keyboard(kbdId KeyboardId) *Screen {
|
||||||
|
s.KeyboardId = kbdId
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
// Rendering the screen text to string to be sent or printed.
|
// Rendering the screen text to string to be sent or printed.
|
||||||
func (st ScreenText) String() string {
|
func (st ScreenText) String() string {
|
||||||
return string(st)
|
return string(st)
|
||||||
|
@ -52,7 +68,7 @@ func (s *Screen) Render(c *Context) error {
|
||||||
if !ok {
|
if !ok {
|
||||||
return KeyboardNotExistErr
|
return KeyboardNotExistErr
|
||||||
}
|
}
|
||||||
msg.ReplyMarkup = kbd.ToTelegramInline()
|
msg.ReplyMarkup = kbd.toTelegramInline()
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := c.B.Send(msg)
|
_, err := c.B.Send(msg)
|
||||||
|
@ -73,7 +89,7 @@ func (s *Screen) Render(c *Context) error {
|
||||||
if !ok {
|
if !ok {
|
||||||
return KeyboardNotExistErr
|
return KeyboardNotExistErr
|
||||||
}
|
}
|
||||||
tkbd = kbd.ToTelegram()
|
tkbd = kbd.toTelegram()
|
||||||
}
|
}
|
||||||
|
|
||||||
msg.ReplyMarkup = tkbd
|
msg.ReplyMarkup = tkbd
|
||||||
|
@ -84,4 +100,3 @@ func (s *Screen) Render(c *Context) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package behx
|
package tx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
apix "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
apix "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
Loading…
Reference in a new issue