2023-07-09 01:28:59 +03:00
|
|
|
package behx
|
|
|
|
|
2023-07-12 14:20:52 +03:00
|
|
|
import (
|
|
|
|
apix "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
2023-07-12 14:59:07 +03:00
|
|
|
"fmt"
|
2023-07-12 14:20:52 +03:00
|
|
|
)
|
|
|
|
|
2023-07-09 01:28:59 +03:00
|
|
|
// The type represents way to interact with user in
|
|
|
|
// handling functions. Is provided to Act() function always.
|
|
|
|
type Context struct {
|
2023-07-12 14:06:05 +03:00
|
|
|
*Session
|
2023-07-09 01:28:59 +03:00
|
|
|
B *Bot
|
2023-07-12 14:06:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Goroutie function to handle each user.
|
|
|
|
func (ctx *Context) handleUpdateChan(updates chan *Update) {
|
|
|
|
bot := ctx.B
|
|
|
|
session := ctx.Session
|
|
|
|
bot.Start.Act(ctx)
|
|
|
|
for u := range updates {
|
2023-07-12 14:20:52 +03:00
|
|
|
screen := bot.Screens[session.CurrentScreenId]
|
|
|
|
|
2023-07-12 14:06:05 +03:00
|
|
|
if u.Message != nil {
|
2023-07-12 14:30:05 +03:00
|
|
|
|
|
|
|
kbd := bot.Keyboards[screen.KeyboardId]
|
|
|
|
btns := kbd.buttonMap()
|
2023-07-12 14:06:05 +03:00
|
|
|
text := u.Message.Text
|
|
|
|
btn, ok := btns[text]
|
|
|
|
|
|
|
|
// Skipping wrong text messages.
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
btn.Action.Act(ctx)
|
|
|
|
} else if u.CallbackQuery != nil {
|
|
|
|
cb := apix.NewCallback(u.CallbackQuery.ID, u.CallbackQuery.Data)
|
2023-07-12 14:30:05 +03:00
|
|
|
data := u.CallbackQuery.Data
|
2023-07-12 14:06:05 +03:00
|
|
|
|
|
|
|
_, err := bot.Request(cb)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-07-12 14:30:05 +03:00
|
|
|
kbd := bot.Keyboards[screen.InlineKeyboardId]
|
|
|
|
btns := kbd.buttonMap()
|
|
|
|
btn := btns[data]
|
|
|
|
btn.Action.Act(ctx)
|
2023-07-12 14:06:05 +03:00
|
|
|
}
|
|
|
|
}
|
2023-07-09 01:28:59 +03:00
|
|
|
}
|
|
|
|
|
2023-07-12 00:33:51 +03:00
|
|
|
// Changes screen of user to the Id one.
|
|
|
|
func (c *Context) ChangeScreen(screenId ScreenId) error {
|
2023-07-12 14:06:05 +03:00
|
|
|
// Return if it will not change anything.
|
|
|
|
if c.CurrentScreenId == screenId {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-12 00:33:51 +03:00
|
|
|
if !c.B.ScreenExists(screenId) {
|
2023-07-09 01:28:59 +03:00
|
|
|
return ScreenNotExistErr
|
|
|
|
}
|
|
|
|
|
2023-07-12 14:06:05 +03:00
|
|
|
screen := c.B.Screens[screenId]
|
|
|
|
screen.Render(c)
|
|
|
|
|
|
|
|
c.Session.ChangeScreen(screenId)
|
|
|
|
c.KeyboardId = screen.KeyboardId
|
2023-07-12 00:33:51 +03:00
|
|
|
|
2023-07-09 01:28:59 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-12 00:33:51 +03:00
|
|
|
// Sends to the user specified text.
|
2023-07-09 01:28:59 +03:00
|
|
|
func (c *Context) Send(text string) error {
|
2023-07-12 14:59:07 +03:00
|
|
|
msg := apix.NewMessage(c.Id.ToTelegram(), text)
|
|
|
|
_, err := c.B.Send(msg)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) Sendf(format string, v ...any) error {
|
|
|
|
return c.Send(fmt.Sprintf(format, v...))
|
2023-07-09 01:28:59 +03:00
|
|
|
}
|
|
|
|
|