Added way to store custom information for each user.
This commit is contained in:
parent
521709f87d
commit
6bbea7c6d9
3 changed files with 33 additions and 8 deletions
|
@ -2,7 +2,7 @@ package behx
|
||||||
|
|
||||||
import (
|
import (
|
||||||
apix "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
apix "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||||
//"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
// The type represents way to interact with user in
|
// The type represents way to interact with user in
|
||||||
|
@ -71,6 +71,12 @@ func (c *Context) ChangeScreen(screenId ScreenId) error {
|
||||||
|
|
||||||
// Sends to the user specified text.
|
// Sends to the user specified text.
|
||||||
func (c *Context) Send(text string) error {
|
func (c *Context) Send(text string) error {
|
||||||
return nil
|
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...))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -21,6 +21,9 @@ type Session struct {
|
||||||
PreviousScreenId ScreenId
|
PreviousScreenId ScreenId
|
||||||
// The currently showed on display keyboard.
|
// The currently showed on display keyboard.
|
||||||
KeyboardId KeyboardId
|
KeyboardId KeyboardId
|
||||||
|
|
||||||
|
// Custom data for each user.
|
||||||
|
V map[string] any
|
||||||
}
|
}
|
||||||
|
|
||||||
// The type represents map of sessions using
|
// The type represents map of sessions using
|
||||||
|
@ -31,6 +34,7 @@ type SessionMap map[SessionId] *Session
|
||||||
func NewSession(id SessionId) *Session {
|
func NewSession(id SessionId) *Session {
|
||||||
return &Session{
|
return &Session{
|
||||||
Id: id,
|
Id: id,
|
||||||
|
V: make(map[string] any),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,14 +11,21 @@ import (
|
||||||
var rootKbd = behx.NewKeyboard(
|
var rootKbd = behx.NewKeyboard(
|
||||||
behx.NewButtonRow(
|
behx.NewButtonRow(
|
||||||
behx.NewButton(
|
behx.NewButton(
|
||||||
"1",
|
"Increment",
|
||||||
behx.NewCustomAction(func(c *behx.Context){
|
behx.NewCustomAction(func(c *behx.Context){
|
||||||
log.Println("pressed the button!")
|
counter := c.V["counter"].(*int)
|
||||||
|
*counter++
|
||||||
|
c.Sendf("%d", *counter)
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
behx.NewButton(
|
||||||
|
"Decrement",
|
||||||
|
behx.NewCustomAction(func(c *behx.Context){
|
||||||
|
counter := c.V["counter"].(*int)
|
||||||
|
*counter--
|
||||||
|
c.Sendf("%d", *counter)
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
behx.NewButton("PRESS ME 2", behx.NewCustomAction(func(c *behx.Context){
|
|
||||||
log.Println("pressed another button!")
|
|
||||||
})),
|
|
||||||
),
|
),
|
||||||
behx.NewButtonRow(
|
behx.NewButtonRow(
|
||||||
behx.NewButton("To second screen", behx.NewScreenChange("second")),
|
behx.NewButton("To second screen", behx.NewScreenChange("second")),
|
||||||
|
@ -68,7 +75,15 @@ var secondScreen = behx.NewScreen(
|
||||||
)
|
)
|
||||||
|
|
||||||
var behaviour = behx.NewBehaviour(
|
var behaviour = behx.NewBehaviour(
|
||||||
behx.NewScreenChange("start"),
|
behx.NewCustomAction(func(c *behx.Context){
|
||||||
|
// This way we provide counter for EACH user.
|
||||||
|
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")
|
||||||
|
}),
|
||||||
behx.ScreenMap{
|
behx.ScreenMap{
|
||||||
"start": startScreen,
|
"start": startScreen,
|
||||||
"second": secondScreen,
|
"second": secondScreen,
|
||||||
|
|
Loading…
Reference in a new issue