63 lines
1.2 KiB
Go
63 lines
1.2 KiB
Go
package tg
|
|
|
|
// The type represents map of sessions using
|
|
// as key.
|
|
type SessionMap map[SessionID]*Session
|
|
|
|
// Add new empty session by it's ID.
|
|
func (sm SessionMap) Add(
|
|
bot *Bot,
|
|
sid SessionID,
|
|
scope SessionScope,
|
|
) *Session {
|
|
ret := NewSession(bot, sid, scope)
|
|
sm[sid] = ret
|
|
return ret
|
|
}
|
|
|
|
// The way to determine where the context is
|
|
// related to.
|
|
type SessionScope uint8
|
|
const (
|
|
NoSessionScope SessionScope = iota
|
|
PrivateSessionScope
|
|
GroupSessionScope
|
|
ChannelSessionScope
|
|
)
|
|
|
|
// Represents unique value to identify chats.
|
|
// In fact is simply ID of the chat.
|
|
type SessionID int64
|
|
|
|
// Convert the SessionID to Telegram API's type.
|
|
func (si SessionID) ToAPI() int64 {
|
|
return int64(si)
|
|
}
|
|
|
|
// The type represents current state of
|
|
// user interaction per each of them.
|
|
type Session struct {
|
|
// ID of the chat of the user.
|
|
ID SessionID
|
|
Scope SessionScope
|
|
// Custom value for each user.
|
|
Data any
|
|
|
|
bot *Bot
|
|
pathHistory []Widget
|
|
skippedUpdates *UpdateChan
|
|
updates *UpdateChan
|
|
}
|
|
|
|
// Return new empty session.
|
|
func NewSession(bot *Bot, id SessionID, scope SessionScope) *Session {
|
|
ret := &Session{}
|
|
ret.ID = id
|
|
ret.Scope = scope
|
|
ret.bot = bot
|
|
ret.updates = NewUpdateChan()
|
|
ret.skippedUpdates = NewUpdateChan()
|
|
return ret
|
|
}
|
|
|
|
|