Skip to content

Commit

Permalink
No python
Browse files Browse the repository at this point in the history
  • Loading branch information
Feresey committed Sep 26, 2020
1 parent 644db05 commit 4ee5717
Showing 1 changed file with 10 additions and 19 deletions.
29 changes: 10 additions & 19 deletions tgapi/conversation.go
Expand Up @@ -13,22 +13,17 @@ var (
ErrNoSuchChoice = errors.New("no such choice")
)

type State int

const (
StateUndefined State = -1
StateFinished State = 0
)
type ConversationState int

type Choice struct {
// nil === true
Accept func(*Message) bool
Apply func(context.Context, *Message) (State, error)
Apply func(context.Context, *Message) (ConversationState, error)
}

type Conversation struct {
// read only
states map[State][]Choice
states map[ConversationState][]Choice

api *API
cache *ttlcache.Cache
Expand All @@ -38,7 +33,7 @@ func NewConversation(api *API, cache *ttlcache.Cache) *Conversation {
res := &Conversation{
api: api,
cache: cache,
states: make(map[State][]Choice),
states: make(map[ConversationState][]Choice),
}
return res
}
Expand All @@ -49,15 +44,15 @@ func (c *Conversation) Stop() {

// AddChoices to conversation list with given state.
// unsafe to use after starting a conversation.
func (c *Conversation) AddChoices(state State, choices ...Choice) {
func (c *Conversation) AddChoices(state ConversationState, choices ...Choice) {
c.states[state] = append(c.states[state], choices...)
}

func keyFromUserID(userID int64) string {
return strconv.FormatInt(userID, 10)
}

func (c *Conversation) AddUser(userID int64, state State) {
func (c *Conversation) AddUser(userID int64, state ConversationState) {
c.cache.Set(keyFromUserID(userID), state)
}

Expand All @@ -70,25 +65,21 @@ func (c *Conversation) RemoveUser(userID int64) {
c.cache.Remove(keyFromUserID(userID))
}

func (c *Conversation) Handle(ctx context.Context, msg *Message) (State, error) {
func (c *Conversation) Handle(ctx context.Context, msg *Message) (ConversationState, error) {
userID := msg.GetFrom().GetID()
key := keyFromUserID(userID)
stateI, ok := c.cache.Get(key)
if !ok {
return StateUndefined, ErrNoSuchConversation
return 0, ErrNoSuchConversation
}
state := stateI.(State)
state := stateI.(ConversationState)

for _, choice := range c.states[state] {
ok := choice.Accept == nil || choice.Accept(msg)
if ok {
next, err := choice.Apply(ctx, msg)
if err == nil {
if next == StateFinished {
c.cache.Remove(key)
} else {
c.cache.Set(key, next)
}
c.cache.Set(key, next)
return next, nil
}
return state, err
Expand Down

0 comments on commit 4ee5717

Please sign in to comment.