-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve ConversationHandler logic, and expose custom KeyStrategy func…
…tions (#166) * Allow for defining custom key state functions for improved conversation handling * Allow for defining custom key state functions for improved conversation handling * add comment * nextHandler should not ignore emptyKey errors * add tests to cover the case of any updates which might break a conversation * Add a conversation-wide filter to control which updates get processed * Add a conversation handler filter
- Loading branch information
1 parent
a8e2c08
commit 43b5186
Showing
5 changed files
with
136 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,55 @@ | ||
package conversation | ||
|
||
type KeyStrategy int64 | ||
|
||
// Note: If you add a new keystrategy here, make sure to add it to the getStateKey method! | ||
const ( | ||
// KeyStrategySenderAndChat ensures that each sender get a unique conversation in each chats. | ||
KeyStrategySenderAndChat KeyStrategy = iota | ||
// KeyStrategySender gives a unique conversation to each sender, but that conversation is available in all chats. | ||
KeyStrategySender | ||
// KeyStrategyChat gives a unique conversation to each chat, which all senders can interact in together. | ||
KeyStrategyChat | ||
import ( | ||
"errors" | ||
"fmt" | ||
"strconv" | ||
|
||
"github.com/PaulSonOfLars/gotgbot/v2/ext" | ||
) | ||
|
||
var ErrEmptyKey = errors.New("empty conversation key") | ||
|
||
// KeyStrategy is the function used to obtain the current key in the ongoing conversation. | ||
// | ||
// Use one of the existing keys, or define your own if you need external data (eg a DB or other state). | ||
type KeyStrategy func(ctx *ext.Context) (string, error) | ||
|
||
var ( | ||
// Ensure key strategy methods match the function signatures. | ||
_ KeyStrategy = KeyStrategyChat | ||
_ KeyStrategy = KeyStrategySender | ||
_ KeyStrategy = KeyStrategySenderAndChat | ||
) | ||
|
||
// KeyStrategySenderAndChat ensures that each sender get a unique conversation, even in different chats. | ||
func KeyStrategySenderAndChat(ctx *ext.Context) (string, error) { | ||
if ctx.EffectiveSender == nil || ctx.EffectiveChat == nil { | ||
return "", fmt.Errorf("missing sender or chat fields: %w", ErrEmptyKey) | ||
} | ||
return fmt.Sprintf("%d/%d", ctx.EffectiveSender.Id(), ctx.EffectiveChat.Id), nil | ||
} | ||
|
||
// KeyStrategySender gives a unique conversation to each sender, and that single conversation is available in all chats. | ||
func KeyStrategySender(ctx *ext.Context) (string, error) { | ||
if ctx.EffectiveSender == nil { | ||
return "", fmt.Errorf("missing sender field: %w", ErrEmptyKey) | ||
} | ||
return strconv.FormatInt(ctx.EffectiveSender.Id(), 10), nil | ||
} | ||
|
||
// KeyStrategyChat gives a unique conversation to each chat, which all senders can interact in together. | ||
func KeyStrategyChat(ctx *ext.Context) (string, error) { | ||
if ctx.EffectiveChat == nil { | ||
return "", fmt.Errorf("missing chat field: %w", ErrEmptyKey) | ||
} | ||
return strconv.FormatInt(ctx.EffectiveChat.Id, 10), nil | ||
} | ||
|
||
// StateKey provides a sane default for handling incoming updates. | ||
func StateKey(ctx *ext.Context, strategy KeyStrategy) (string, error) { | ||
if strategy == nil { | ||
return KeyStrategySenderAndChat(ctx) | ||
} | ||
return strategy(ctx) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters