tg/src/behx/context.go

67 lines
1.3 KiB
Go
Raw Normal View History

2023-07-09 01:28:59 +03:00
package behx
// 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 {
if u.Message != nil {
screen := bot.Screens[session.CurrentScreenId]
kbd := bot.Keyboards[screen.KeyboardId]
btns := kbd.buttonMap()
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)
_, err := bot.Request(cb)
if err != nil {
panic(err)
}
}
}
2023-07-09 01:28:59 +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
}
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-09 01:28:59 +03:00
return nil
}
// Sends to the user specified text.
2023-07-09 01:28:59 +03:00
func (c *Context) Send(text string) error {
return nil
}