conversation

package
v2.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2023 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var KeyNotFound = errors.New("conversation key not found")

Functions

func StateKey

func StateKey(ctx *ext.Context, strategy KeyStrategy) string

Types

type InMemoryStorage

type InMemoryStorage struct {
	// contains filtered or unexported fields
}

InMemoryStorage is a thread-safe in-memory implementation of the Storage interface.

func NewInMemoryStorage

func NewInMemoryStorage(strategy KeyStrategy) *InMemoryStorage

func (*InMemoryStorage) Delete

func (c *InMemoryStorage) Delete(ctx *ext.Context) error

func (*InMemoryStorage) Get

func (c *InMemoryStorage) Get(ctx *ext.Context) (*State, error)

func (*InMemoryStorage) Set

func (c *InMemoryStorage) Set(ctx *ext.Context, state State) error

type KeyStrategy

type KeyStrategy int64
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
)

Note: If you add a new keystrategy here, make sure to add it to the getStateKey method!

type State

type State struct {
	// Key represents the name of the current state, as defined in the States map of handlers.Conversation.
	Key string
}

State stores all the variables relevant to the current conversation state.

Note: More keys may be added in the future to support additional features. As such, any storage implementations should be flexible, and allow for storing the entire struct rather than individual fields.

type Storage

type Storage interface {
	// Get returns the state for the specified conversation key.
	// Note that this is checked at each incoming message, so may be a bottleneck for some implementations.
	//
	// If the key is not found (and as such, this conversation has not yet started), this method should return the
	// ConversationKeyNotFound error.
	// Get(key string) (*State, error)
	Get(ctx *ext.Context) (*State, error)

	// Set updates the conversation state.
	Set(ctx *ext.Context, state State) error

	// Delete ends the conversation, removing the key from the storage.
	Delete(ctx *ext.Context) error
}

Storage allows you to define custom backends for retaining conversation conversations. If you are looking to persist conversation data, you should implement this interface with you backend of choice. Note: Make sure to store the entire State struct; future changes may add new fields.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL