session

package
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2025 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type History

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

History model. It contains all the steps of the user in the chat with the bot. The history is used to keep track of the user's actions and to be able to go back to the previous steps.

func (*History) Add

func (h *History) Add(c telebot.Context, uniq string)

Add add a new step to the history and clears the history if the message is sent without a callback. The history is cleared when the callback is nil, indicating the start of a new sequence or action.

func (*History) Back

func (h *History) Back()

Back removes the last step from the history. It is used to go back to the previous step in the sequence.

func (*History) Clear

func (h *History) Clear()

Clear history.

func (*History) ExtractValues

func (h *History) ExtractValues(keys map[string]any) bool

ExtractValues extracts values from the current step and writes them to the specified variables. It returns false if at least one parameter was not found or could not be converted.

func (*History) Get

func (h *History) Get(key string) (any, bool)

Get get data from current history step. Return `nil` and `false` if not exist

func (*History) GetPrevStep

func (h *History) GetPrevStep() *HistoryStep

GetPrevStep get previous step. If not exists return nil.

func (*History) GetStep

func (h *History) GetStep() *HistoryStep

Get current step. If not exists return nil.

func (*History) Set

func (h *History) Set(key string, val any)

Set set data to current history step.

type HistoryStep

type HistoryStep struct {
	// Unique is the unique identifier of the step.
	// It is used to identify the step in the history and to
	// be able to go back to the previous steps.
	Unique string
	// contains filtered or unexported fields
}

HistoryStep model. It contains the unique identifier of the step and the data of the step.

type Scene

type Scene struct {

	// Wizard data for scenes of type wizard.
	Wizard Wizard
	// contains filtered or unexported fields
}

Scene represents a scene model.

func (*Scene) AddMessage

func (s *Scene) AddMessage(msg *telebot.Message)

AddMessage adds a message to the scene's message store It appends the provided message to the Messages slice.

func (*Scene) Delete

func (s *Scene) Delete(key string)

Delete delete value by `key` if `key` not exists it will work without error

func (*Scene) DeleteMessages

func (s *Scene) DeleteMessages(c telebot.Context, skipLast ...bool)

DeleteMessages deletes all stored messages from both the chat and the scene It clears the Messages slice after deleting messages.

func (*Scene) ExtractValues

func (s *Scene) ExtractValues(keys map[string]any) bool

ExtractValues extracts values from the scene's data and writes them to the specified variables. It returns false if at least one parameter was not found or could not be converted.

func (*Scene) Get

func (s *Scene) Get(key string) (any, bool)

Get get value by `key` if `key` not exists, it will return false

func (*Scene) GetMessages

func (s *Scene) GetMessages() []telebot.Message

GetMessages retrieves all the messages stored in the scene It returns the slice of messages.

func (*Scene) Name

func (s *Scene) Name() string

Current returns the name of the current scene.

func (*Scene) Set

func (s *Scene) Set(key string, value any)

Set set value in storage by `key` and `value` if `key` not exists, it will create new one

type SceneType

type SceneType string

SceneType represents the type of a scene.

const (
	SceneTypeSimple SceneType = "simple"
	SceneTypeWizard SceneType = "wizard"
)

Available scene types.

type Session

type Session struct {
	// History is a list of steps that the user has taken.
	// It is used to keep track of the user's progress through a scene.
	History *History

	// Scene is a pointer to the user's current scene.
	// It is used to store the user's current scene, which is used to determine the next step in the scene.
	Scene *Scene

	// Language is a string that represents the user's language.
	Language string
	// contains filtered or unexported fields
}

Session model. It contains data of user session. Data is stored in the cache with the user's ID as the key. The session is updated every time the user interacts with the bot.

func FromContext

func FromContext(c telebot.Context) *Session

Extract session data from context. If session not exist it create scene.

func (*Session) Delete

func (s *Session) Delete(key string)

Delete delete value by `key`. if `key` not exists it will work without error.

func (*Session) EnterScene

func (s *Session) EnterScene(name string) *Scene

If scene already exists it will return exist scene instance. It can accept onExpire func to handle scene expiration event. Scene expiration event will be triggered when user leave from scene.

func (*Session) ExtractValues

func (s *Session) ExtractValues(keys map[string]any) bool

ExtractValues extracts values from the session storage and writes them to the specified variables. It returns false if at least one parameter was not found or could not be converted.

func (*Session) Get

func (s *Session) Get(key string) (any, bool)

Get get value by `key`. if `key` not exists, it will return false.

func (*Session) GetScene

func (s *Session) GetScene() *Scene

GetScene retrieves the current scene from the session. If the session does not contain a scene, or the scene is empty (i.e. the current scene is an empty string), it returns nil. Otherwise, it returns the current scene.

func (*Session) GetTranslator

func (s *Session) GetTranslator() i18n.Translator

GetTranslator returns translator function based on the user's language preference. If the user's language preference is not set, it uses the bot's default language. If the user's language preference is set, it overrides the bot's default language.

func (*Session) LeaveScene

func (s *Session) LeaveScene()

LeaveScene clears the current scene from the session. It sets the scene to nil, effectively leaving the scene.

func (*Session) Set

func (s *Session) Set(key string, val any)

Set set value in session storage by `key` and `value`. if `key` not exists, it will create new one.

type Wizard

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

Wizard represents a wizard model for step management in scenes.

func (*Wizard) Back

func (w *Wizard) Back()

Back moves the cursor to the previous step in the wizard. If the current step is the first step, it does nothing.

func (*Wizard) GetStep

func (w *Wizard) GetStep() int

GetStep returns the current step in the wizard.

func (*Wizard) Next

func (w *Wizard) Next()

Next moves the cursor to the next step in the wizard.

Jump to

Keyboard shortcuts

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