Files
rmser/internal/transport/telegram/fsm.go
SERTY 5f35d7a75f добавлен биллинг и тарифы
добавлена интеграция с юкасса
2025-12-24 09:06:19 +03:00

83 lines
1.7 KiB
Go

package telegram
import "sync"
// Состояния пользователя
type State int
const (
StateNone State = iota
StateAddServerURL
StateAddServerLogin
StateAddServerPassword
StateAddServerConfirmName
StateAddServerInputName
StateBillingGiftURL
)
// UserContext хранит временные данные в процессе диалога
type UserContext struct {
State State
TempURL string
TempLogin string
TempPassword string
TempServerName string
BillingTargetURL string
}
// StateManager управляет состояниями
type StateManager struct {
mu sync.RWMutex
states map[int64]*UserContext
}
func NewStateManager() *StateManager {
return &StateManager{
states: make(map[int64]*UserContext),
}
}
func (sm *StateManager) GetState(userID int64) State {
sm.mu.RLock()
defer sm.mu.RUnlock()
if ctx, ok := sm.states[userID]; ok {
return ctx.State
}
return StateNone
}
func (sm *StateManager) SetState(userID int64, state State) {
sm.mu.Lock()
defer sm.mu.Unlock()
if _, ok := sm.states[userID]; !ok {
sm.states[userID] = &UserContext{}
}
sm.states[userID].State = state
}
func (sm *StateManager) GetContext(userID int64) *UserContext {
sm.mu.RLock()
defer sm.mu.RUnlock()
if ctx, ok := sm.states[userID]; ok {
return ctx
}
return &UserContext{} // Return empty safe struct
}
func (sm *StateManager) UpdateContext(userID int64, updater func(*UserContext)) {
sm.mu.Lock()
defer sm.mu.Unlock()
if _, ok := sm.states[userID]; !ok {
sm.states[userID] = &UserContext{}
}
updater(sm.states[userID])
}
func (sm *StateManager) Reset(userID int64) {
sm.mu.Lock()
defer sm.mu.Unlock()
delete(sm.states, userID)
}