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 Attachment
- type ChatFunc
- type Delivery
- type Editor
- type File
- type FileSender
- 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 (m *Messaging) Mirror(ctx context.Context, session uuid.UUID, out Outgoing) 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, out Outgoing) error
- func (m *Messaging) Run(ctx context.Context, logger *slog.Logger) error
- func (m *Messaging) Send(ctx context.Context, session uuid.UUID, out Outgoing, ids ...uuid.UUID) (_ []Delivery, err error)
- func (m *Messaging) SessionPrompt(ctx context.Context, session uuid.UUID) string
- func (m *Messaging) SystemPrompt() string
- func (m *Messaging) Tools() []llm.Tool
- func (*Messaging) Type() string
- type OpenFunc
- type Opt
- type Outgoing
- type Provider
- type Sent
- 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 Attachment ¶ added in v1.5.0
type Attachment struct {
Name string
Type string
Size int64
Description string
Open func(context.Context) (io.ReadCloser, error)
}
Attachment is a file sent with a message, such as a photo, a document, or a voice message: its name, its type, when it's known, or empty, so it's detected from its content, its size, when it's known, what it is, such as a photo which was compressed, and the function which opens it, which is downloaded as it's read, which the caller closes, or why it can't be, such as that it's too large, when it's unavailable
type ChatFunc ¶
type ChatFunc func(ctx context.Context, channel schema.Channel, message Message, progress func(line string)) error
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. It calls the progress function with each line which says what's happening, as it replies, such as what the tools it calls are doing, which is shown in the chat.
type Delivery ¶ added in v1.5.0
Delivery is what was sent to one of a session's channels: the messages, in the order they were sent, and the error of anything which wasn't sent, or nil, when everything was.
type Editor ¶ added in v1.5.0
type Editor interface {
// Edit changes the text of the message with the identifier, which was
// sent to the chat with the address
Edit(ctx context.Context, address, id, text string) error
// Delete deletes the message with the identifier, which was sent to the
// chat with the address
Delete(ctx context.Context, address, id string) error
}
Editor is a provider which can change, and delete, the messages it sent, such as to show how a reply is getting on, in a message which is changed as it gets on, and deleted once it's replied
type File ¶ added in v1.5.0
type File struct {
// URI is the file's, such as resource:///<hash>
URI string
// Name is the file's name, such as speech.ogg
Name string
// Type is its MIME type, such as image/png
Type string
// Size is its size, in bytes
Size int64
}
File is a session's file, which is sent to its channels
type FileSender ¶ added in v1.5.0
type FileSender interface {
// SendFile sends the file, whose content the function opens, and is read
// as it's sent, which the provider closes, and can open again, such as to
// send it again, to the chat with the address, with the caption, which is
// Markdown, when it has one, under it, or before it, when it's too long
// to be its caption
//
// It returns the messages it was sent as, in order, such as the caption,
// when it's a message of its own, and the file, including those which
// were sent before an error
SendFile(ctx context.Context, address string, file File, open func() (io.ReadCloser, error), caption string) ([]Sent, error)
}
FileSender is a provider which can send files, such as images, and speech
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, or the caption of its attachments
Text string
// Caption is whether the text is the caption of its attachments, which
// was sent with them, rather than a message of its own
Caption bool
// Attachments are the files sent with it, such as photos, documents,
// and voice messages, which are opened when they're kept
Attachments []Attachment
// 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) Mirror ¶ added in v1.6.3
Mirror sends what the person sent the session from elsewhere, such as the web UI, to its channels which are the person's own chats, rather than groups, which others read, so a private chat has both sides of the conversation, and does nothing when it has none
func (*Messaging) Notify ¶
func (*Messaging) Notify(func(eventschema.EventAction, string))
func (*Messaging) Reply ¶
Reply sends the session's reply, its text, and the files it made, such as an image, or speech, to each of its channels, and does nothing when it has none, or the reply has neither. 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, out Outgoing, ids ...uuid.UUID) (_ []Delivery, err error)
Send sends the text, and the session's files, by their URIs, to the channels of the session with the identifiers, or all of them, when there are none, and returns what was sent to each, in order, or why it wasn't, so a channel which couldn't be sent to, or a file which couldn't be, doesn't stop the others. A provider which can't send files, or without WithFiles, is sent the text only. Returns ErrBadParameter when there's no text, nor files, and ErrNotFound when the session has no channels, or one of the identifiers isn't its.
func (*Messaging) SessionPrompt ¶ added in v1.5.1
SessionPrompt returns what's added to the system prompt, for the session, when chats are connected to it: that the person talks to it there, such as from their phone, so speech is a voice note, and their voice messages are transcribed, or nothing, when none are
func (*Messaging) SystemPrompt ¶
SystemPrompt returns how channels work, and when to use the tools
type OpenFunc ¶ added in v1.5.0
OpenFunc opens the session's file with the URI, and returns what it is, and its content, which the caller closes. Returns ErrNotFound when the session has no file with it.
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 WithFiles ¶ added in v1.5.0
WithFiles sets the function which opens a session's files, so they're sent to its channels, with its replies, by the providers which can send files, without which only text is sent
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 Outgoing ¶ added in v1.5.0
Outgoing is what's sent to a session's channels: text, which is Markdown, and the session's files, by their URIs, such as an image, or speech
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, and returns the messages it
// was sent as, in order, including those which were sent before an
// error
Send(ctx context.Context, address, text string) ([]Sent, error)
// Private returns true when the chat with the address is the person's
// own, rather than a group's, or a channel's, which others read
Private(address string) bool
}
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 Sent ¶ added in v1.5.0
type Sent struct {
ID string `json:"id"`
Text bool `json:"text,omitempty"`
File string `json:"file,omitempty"`
Caption bool `json:"caption,omitempty"`
Note string `json:"note,omitempty"`
}
Sent is a message which was sent to a chat: its identifier, which the provider gave it, and what it was. That's text, a file, with the text as its caption, or without it, or a note in place of a file which couldn't be sent, such as one which is too large.
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. |