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.
|
|
|
|
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,
|
|
|
|
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.
|
2023-10-10 12:42:05 +03:00
|
|
|
type SessionId int64
|
2023-07-09 01:28:59 +03:00
|
|
|
|
2023-08-13 15:37:36 +03:00
|
|
|
// Convert the SessionId to Telegram API's type.
|
2023-08-19 12:47:33 +03:00
|
|
|
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 {
|
2023-08-15 16:02:14 +03:00
|
|
|
// Id of the chat of the user.
|
2023-07-09 01:28:59 +03:00
|
|
|
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
|
|
|
|
pathHistory []Path
|
|
|
|
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 {
|
|
|
|
ret := &Session{}
|
|
|
|
ret.Id = id
|
|
|
|
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
|
|
|
|