2023-08-19 09:12:26 +03:00
|
|
|
package tg
|
2023-07-09 01:28:59 +03:00
|
|
|
|
2024-03-28 10:41:09 +03:00
|
|
|
// The type represents map of sessions using
|
|
|
|
// as key.
|
2024-07-21 16:02:47 +03:00
|
|
|
type SessionMap map[SessionID]*Session
|
2024-03-28 10:41:09 +03:00
|
|
|
|
|
|
|
// Add new empty session by it's ID.
|
2024-03-29 14:30:48 +03:00
|
|
|
func (sm SessionMap) Add(
|
|
|
|
bot *Bot,
|
2024-07-21 16:02:47 +03:00
|
|
|
sid SessionID,
|
2024-03-29 14:30:48 +03:00
|
|
|
scope SessionScope,
|
|
|
|
) *Session {
|
|
|
|
ret := NewSession(bot, sid, scope)
|
2024-03-28 10:41:09 +03:00
|
|
|
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 (
|
2023-09-27 14:09:49 +03:00
|
|
|
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.
|
2024-07-21 16:02:47 +03:00
|
|
|
type SessionID int64
|
2023-07-09 01:28:59 +03:00
|
|
|
|
2024-07-21 16:02:47 +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 {
|
2024-07-21 16:02:47 +03:00
|
|
|
// 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.
|
2023-09-11 10:23:04 +03:00
|
|
|
Data any
|
2024-03-28 10:41:09 +03:00
|
|
|
|
|
|
|
bot *Bot
|
2024-07-21 16:02:47 +03:00
|
|
|
pathHistory []Widget
|
2024-03-28 10:41:09 +03:00
|
|
|
skippedUpdates *UpdateChan
|
|
|
|
updates *UpdateChan
|
2023-07-09 01:28:59 +03:00
|
|
|
}
|
|
|
|
|
2024-03-29 14:30:48 +03:00
|
|
|
// Return new empty session.
|
2024-07-21 16:02:47 +03:00
|
|
|
func NewSession(bot *Bot, id SessionID, scope SessionScope) *Session {
|
2024-03-29 14:30:48 +03:00
|
|
|
ret := &Session{}
|
2024-07-21 16:02:47 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-03-28 10:41:09 +03:00
|
|
|
|