tg/src/tx/context.go

144 lines
2.8 KiB
Go
Raw Normal View History

package tx
2023-07-09 01:28:59 +03:00
2023-07-12 14:20:52 +03:00
import (
"fmt"
apix "github.com/go-telegram-bot-api/telegram-bot-api/v5"
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
B *Bot
updates chan *Update
available bool
2023-07-12 14:06:05 +03:00
}
// Goroutie function to handle each user.
func (c *Context) handleUpdateChan(updates chan *Update) {
bot := c.B
session := c.Session
bot.Start.Act(c)
2023-07-12 14:06:05 +03:00
for u := range updates {
2023-07-12 14:20:52 +03:00
screen := bot.Screens[session.CurrentScreenId]
// The part is added to implement custom update handling.
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]
/*if ok {
c.available = false
btn.Action.Act(c)
c.available = true
continue
}*/
// Sending wrong messages to
// the currently reading goroutine.
if !ok && c.ReadingUpdate {
c.updates <- u
2023-07-12 14:06:05 +03:00
continue
}
2023-08-11 11:04:28 +03:00
if ok && btn.Action != nil {
c.run(btn.Action)
}
2023-07-12 14:06:05 +03:00
} 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(c)
2023-07-12 14:06:05 +03:00
}
}
2023-07-09 01:28:59 +03:00
}
func (c *Context) run(a Action) {
c.available = true
go a.Act(c)
}
func (c *Context) Available() bool {
return c.available
}
// 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
}
if !c.B.ScreenExist(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)
2023-07-12 14:06:05 +03:00
c.Session.ChangeScreen(screenId)
c.KeyboardId = screen.KeyboardId
c.available = false
if screen.Action != nil {
c.run(screen.Action)
}
2023-07-09 01:28:59 +03:00
return nil
}
// Returns the next update ignoring current screen.
func (c *Context) ReadUpdate() (*Update, error) {
var (
u *Update
)
c.ReadingUpdate = true
for {
select {
case u = <-c.updates:
c.ReadingUpdate = false
return u, nil
default:
if !c.available {
return nil, NotAvailableErr
}
}
}
}
// Returns the next text message that the user sends.
func (c *Context) ReadTextMessage() (string, error) {
u, err := c.ReadUpdate()
if err != nil {
return "", err
}
if u.Message == nil {
return "", WrongUpdateType{}
}
return u.Message.Text, nil
}
// Sends to the user specified text.
func (c *Context) Send(v ...any) error {
msg := apix.NewMessage(c.Id.ToTelegram(), fmt.Sprint(v...))
_, err := c.B.Send(msg)
return err
}
// Sends the formatted with fmt.Sprintf message to the user.
func (c *Context) Sendf(format string, v ...any) error {
return c.Send(fmt.Sprintf(format, v...))
2023-07-09 01:28:59 +03:00
}