tg/session.go

64 lines
1.2 KiB
Go
Raw Permalink Normal View History

2023-08-19 09:12:26 +03:00
package tg
2023-07-09 01:28:59 +03:00
// The type represents map of sessions using
// as key.
type SessionMap map[SessionID]*Session
// Add new empty session by it's ID.
2024-03-29 14:30:48 +03:00
func (sm SessionMap) Add(
bot *Bot,
sid SessionID,
2024-03-29 14:30:48 +03:00
scope SessionScope,
) *Session {
ret := NewSession(bot, sid, scope)
sm[sid] = ret
return ret
}
2023-09-26 17:13:31 +03:00
// The way to determine where the context is
// related to.
type SessionScope uint8
const (
NoSessionScope SessionScope = iota
2023-09-26 17:13:31 +03:00
PrivateSessionScope
GroupSessionScope
ChannelSessionScope
)
2023-07-09 01:28:59 +03:00
// Represents unique value to identify chats.
// In fact is simply ID of the chat.
type SessionID int64
2023-07-09 01:28:59 +03:00
// Convert the SessionID to Telegram API's type.
func (si SessionID) ToAPI() int64 {
2023-08-13 15:37:36 +03:00
return int64(si)
}
2023-07-09 01:28:59 +03:00
// The type represents current state of
// user interaction per each of them.
type Session struct {
// ID of the chat of the user.
ID SessionID
2023-09-26 17:13:31 +03:00
Scope SessionScope
2023-08-15 16:02:14 +03:00
// Custom value for each user.
Data any
bot *Bot
pathHistory []Widget
skippedUpdates *UpdateChan
updates *UpdateChan
2023-07-09 01:28:59 +03:00
}
2024-03-29 14:30:48 +03:00
// Return new empty session.
func NewSession(bot *Bot, id SessionID, scope SessionScope) *Session {
2024-03-29 14:30:48 +03:00
ret := &Session{}
ret.ID = id
2024-03-29 14:30:48 +03:00
ret.Scope = scope
ret.bot = bot
ret.updates = NewUpdateChan()
ret.skippedUpdates = NewUpdateChan()
return ret
2023-08-13 15:37:36 +03:00
}