ruspet_portal_back/service/telegram_service/state/delegation.go

46 lines
1.2 KiB
Go

// telegram_service/state/handler_delegation.go - новый файл
package state
import (
"context"
"encoding/json"
"fmt"
"time"
)
type DelegationFlowState struct {
CategoryID uint64 `json:"category_id"`
TopicID uint64 `json:"topic_id,omitempty"`
DelegateID uint64 `json:"delegate_id"`
Stage string `json:"stage"` // "select_category", "select_topic", "select_user", "input_date"
}
func delegationKey(telegramID int64) string {
return fmt.Sprintf("delegation_flow:%d", telegramID)
}
func (s *StateManager) SetDelegation(ctx context.Context, telegramID int64, state *DelegationFlowState) error {
data, err := json.Marshal(state)
if err != nil {
return err
}
return s.redis.Set(ctx, delegationKey(telegramID), string(data), 30*time.Minute)
}
func (s *StateManager) GetDelegation(ctx context.Context, telegramID int64) (*DelegationFlowState, error) {
data, err := s.redis.Get(ctx, delegationKey(telegramID))
if err != nil {
return nil, nil
}
var state DelegationFlowState
if err := json.Unmarshal([]byte(data), &state); err != nil {
return nil, err
}
return &state, nil
}
func (s *StateManager) ClearDelegation(ctx context.Context, telegramID int64) error {
return s.redis.Del(ctx, delegationKey(telegramID))
}