tg/src/tx/session.go

59 lines
1.3 KiB
Go
Raw Normal View History

package tx
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-08-13 15:37:36 +03:00
// Convert the SessionId to Telegram API's type.
func (si SessionId) ToTelegram() int64 {
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-08-15 16:02:14 +03:00
// Custom value for each user.
V any
2023-07-09 01:28:59 +03:00
}
2023-08-13 15:37:36 +03:00
// Return new empty session with specified user ID.
func NewSession(id SessionId) *Session {
return &Session{
Id: id,
V: make(map[string]any),
}
}
2023-07-09 01:28:59 +03:00
// The type represents map of sessions using
// as key.
type SessionMap map[SessionId]*Session
2023-07-09 01:28:59 +03:00
2023-08-13 15:37:36 +03:00
// Add new empty session by it's ID.
func (sm SessionMap) Add(sid SessionId) {
sm[sid] = NewSession(sid)
}
2023-08-12 14:35:33 +03:00
// Session information for a group.
type GroupSession struct {
Id SessionId
// Information for each user in the group.
V map[SessionId]any
}
// Returns new empty group session with specified group and user IDs.
func NewGroupSession(id SessionId) *GroupSession {
return &GroupSession{
Id: id,
V: make(map[SessionId]any),
}
}
2023-08-13 15:37:36 +03:00
// Map for every group the bot is in.
type GroupSessionMap map[SessionId]*GroupSession
2023-08-13 15:37:36 +03:00
func (sm GroupSessionMap) Add(sid SessionId) {
sm[sid] = NewGroupSession(sid)
}