Documentation
¶
Overview ¶
Package messaging connects chats on messaging services, such as Telegram, to sessions, as channels, so the person can talk to their assistant there. A message sent in a channel's chat is sent to its session, and the session's replies are sent to each of its channels, wherever the message they reply to came from. A chat is connected with a link, which has a code, that the person opens, so only they can connect it, and a chat is a channel of one session, which a session can have more than one of.
Index ¶
- Constants
- type ChatFunc
- type Message
- type Messaging
- func (m *Messaging) Connect(ctx context.Context, session uuid.UUID, provider string) (_ string, _ time.Time, err error)
- func (m *Messaging) DeleteSession(ctx context.Context, session uuid.UUID) (int, error)
- func (*Messaging) Description() string
- func (m *Messaging) Disconnect(ctx context.Context, id uuid.UUID) (_ schema.Channel, err error)
- func (m *Messaging) List(ctx context.Context, session uuid.UUID) (_ []schema.Channel, err error)
- func (*Messaging) Name() string
- func (*Messaging) Notify(func(eventschema.EventAction, string))
- func (m *Messaging) Providers() []string
- func (m *Messaging) Reply(ctx context.Context, session uuid.UUID, text string) error
- func (m *Messaging) Run(ctx context.Context, logger *slog.Logger) error
- func (m *Messaging) Send(ctx context.Context, session uuid.UUID, text string, ids ...uuid.UUID) (_ []schema.Channel, err error)
- func (m *Messaging) SystemPrompt() string
- func (m *Messaging) Tools() []llm.Tool
- type Opt
- type Provider
- type SessionFunc
- type Store
- type Typer
Constants ¶
const ( // Name is the namespace of the tools Name = "messaging" // CodeExpiry is how long a link which connects a chat can be used for CodeExpiry = 15 * time.Minute )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ChatFunc ¶
ChatFunc sends the message, which was sent in the channel's chat, to the channel's session, and returns once its model has replied, such as by chatting in it. The reply is sent to the session's channels by Reply, which the session calls with it.
type Message ¶
type Message struct {
// Address is the chat it was sent in
Address string
// Title is the chat's name, such as the person's, or a group's
Title string
// From is who sent it, such as their first name
From string
// Code is the code it has, when it connects the chat, such as from a
// link which Link returned, and none otherwise
Code string
// Text is what it says
Text string
// Time is when it was sent
Time time.Time
}
Message is a message sent to a provider, from a chat
type Messaging ¶
type Messaging struct {
// contains filtered or unexported fields
}
Messaging is the messaging providers, such as Telegram, and the channels, which are their chats, connected to sessions. It's safe to use concurrently, by one server, as a provider's messages are received by one.
func New ¶
New returns the messaging, whose channels are kept in the store, with the options, such as the providers, and the function which sends their messages to a session. Returns ErrBadParameter when there's no store, or an option isn't valid.
func (*Messaging) Connect ¶
func (m *Messaging) Connect(ctx context.Context, session uuid.UUID, provider string) (_ string, _ time.Time, err error)
Connect returns the link which connects a chat on the provider, or the only one, when it's empty, to the session, and when it expires. Returns ErrBadParameter when there's no session, or no provider is given, and there's more than one, and ErrNotFound when there's no provider with the name.
func (*Messaging) DeleteSession ¶
DeleteSession disconnects the channels of the session, which was deleted, and forgets the links which would connect a chat to it, and returns how many channels there were
func (*Messaging) Description ¶
Description returns what messaging is
func (*Messaging) Disconnect ¶
Disconnect disconnects the channel with the identifier, and returns it, or ErrNotFound when there's none
func (*Messaging) List ¶
List returns the channels of the session, or all of them, when it's the nil UUID, the first connected first
func (*Messaging) Notify ¶
func (*Messaging) Notify(func(eventschema.EventAction, string))
Notify does nothing, as the tools don't change
func (*Messaging) Reply ¶
Reply sends the session's reply to each of its channels, and does nothing when it has none, or there's no text, such as when the reply is only a picture. It's called with each reply which is added to a session, wherever the message it replies to came from.
func (*Messaging) Run ¶
Run receives the messages sent to each provider, which are sent to their channel's session, until the context is cancelled, when it returns nil. A message which connects a chat connects it, and one from a chat which isn't connected is ignored. A provider which can't receive tries again, waiting longer each time. With a broadcaster, the channels of each session which is deleted are disconnected. What fails is logged to the logger, when there is one.
func (*Messaging) Send ¶
func (m *Messaging) Send(ctx context.Context, session uuid.UUID, text string, ids ...uuid.UUID) (_ []schema.Channel, err error)
Send sends the text to the channels of the session with the identifiers, or all of them, when there are none, and returns those it was sent to. Returns ErrBadParameter when there's no text, ErrNotFound when the session has no channels, or one of the identifiers isn't its, and the errors of the channels it couldn't be sent to, after it's sent to the others.
func (*Messaging) SystemPrompt ¶
SystemPrompt returns how channels work, and when to use the tools
type Opt ¶
type Opt func(*opt) error
Opt is an option of messaging
func WithBroadcaster ¶
func WithBroadcaster(b *broadcaster.Broadcaster) Opt
WithBroadcaster sets the broadcaster which Run watches for the sessions which are deleted, whose channels are disconnected
func WithChat ¶
WithChat sets the function which sends a message from a channel to its session, without which the messages are ignored
func WithProvider ¶
WithProvider adds the messaging provider, such as Telegram, whose chats can be connected. Returns ErrBadParameter when it's nil, and ErrConflict when one with its name was added.
func WithSessions ¶
func WithSessions(fn SessionFunc) Opt
WithSessions sets the function which says whether a session exists, so a chat isn't connected to a session which was deleted, and a channel whose session was deleted, such as when the event that it was deleted was missed, is disconnected, when a message from it can't be answered
func WithTracer ¶
WithTracer sets the tracer which traces what's sent, and received, or none, when it's nil
type Provider ¶
type Provider interface {
// Name returns the provider's name, such as telegram, which its
// channels have
Name() string
// Link returns the link which the person opens to connect a chat, with
// the code, which the provider's message from that chat then has, such
// as https://t.me/bot?start=code
Link(code string) string
// Receive calls the function with each message sent to the provider,
// one at a time, in the order they're sent, until the context is
// cancelled, when it returns nil, or it can't go on. A message the
// function returns an error for isn't received again.
Receive(ctx context.Context, fn func(context.Context, Message) error) error
// Send sends the text to the chat with the address, as more than one
// message, when it's longer than one can be
Send(ctx context.Context, address, text string) error
}
Provider is a messaging service, such as Telegram, whose chats are channels: it receives the messages sent to it, and sends messages to a chat
type SessionFunc ¶
SessionFunc returns nil when the session with the identifier exists, or ErrNotFound when it doesn't
type Store ¶
type Store interface {
// Load returns the channels, or none, when none were saved
Load(context.Context) ([]schema.Channel, error)
// Save replaces the channels with these
Save(context.Context, []schema.Channel) error
}
Store keeps the channels, such as in a file, so they're kept when the server restarts. There are few of them, so they're loaded, and saved, as a whole.
type Typer ¶
type Typer interface {
// Typing shows that a reply is being written in the chat with the
// address, for a few seconds, or until a message is sent
Typing(ctx context.Context, address string) error
}
Typer is a provider which can show that a reply is being written, such as Telegram's "typing…"
Directories
¶
| Path | Synopsis |
|---|---|
|
Package schema has the channels of messaging: chats, such as on Telegram, which are attached to sessions, so the person can talk to their assistant there
|
Package schema has the channels of messaging: chats, such as on Telegram, which are attached to sessions, so the person can talk to their assistant there |
|
Package telegram is the Telegram messaging provider, a bot, whose chats are channels.
|
Package telegram is the Telegram messaging provider, a bot, whose chats are channels. |