From 4ee5717373ac7228c2b97e0232d014b5d896df40 Mon Sep 17 00:00:00 2001 From: Feresey Date: Sat, 26 Sep 2020 23:16:48 +0300 Subject: [PATCH] No python --- tgapi/conversation.go | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/tgapi/conversation.go b/tgapi/conversation.go index bb431e3..0e88985 100644 --- a/tgapi/conversation.go +++ b/tgapi/conversation.go @@ -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 @@ -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 } @@ -49,7 +44,7 @@ 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...) } @@ -57,7 +52,7 @@ 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) } @@ -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