messaging

package
v1.7.2 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: Apache-2.0 Imports: 23 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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

func RetryAfter added in v1.7.2

func RetryAfter(err error) time.Duration

RetryAfter returns how long to wait before trying again, which the error, such as a provider's which is rate limited, says, with its RetryAfter method, or zero, when it doesn't say

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)
	Unavailable string
}

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 Connector added in v1.7.2

type Connector interface {
	// Connect connects to the provider, and returns who it is there, such
	// as the bot's username, which is logged
	Connect(ctx context.Context) (string, error)
}

Connector is a provider which is connected before it receives messages, such as to check its credentials, whose errors are those Receive's are, such as ErrForbidden

type Delivery added in v1.5.0

type Delivery struct {
	Channel  schema.Channel
	Messages []Sent
	Err      error
}

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

	// URL is a link to it, such as the server's https endpoint of a file
	// of a workspace, which is sent in the text, rather than its content,
	// which isn't opened, as it can be larger than a chat takes
	URL string
}

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 LinkFunc added in v1.7.0

type LinkFunc func(text string) string

LinkFunc returns the text which is sent to a chat, with its links which a chat can't open, such as to a workspace's files, as links it can, such as to the server's https endpoint, or their text, without one

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

func New(store Store, opts ...Opt) (*Messaging, error)

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

func (m *Messaging) DeleteSession(ctx context.Context, session uuid.UUID) (int, error)

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

func (*Messaging) Description() string

Description returns what messaging is

func (*Messaging) Disconnect

func (m *Messaging) Disconnect(ctx context.Context, id uuid.UUID) (_ schema.Channel, err error)

Disconnect disconnects the channel with the identifier, and returns it, or ErrNotFound when there's none

func (*Messaging) List

func (m *Messaging) List(ctx context.Context, session uuid.UUID) (_ []schema.Channel, err error)

List returns the channels of the session, or all of them, when it's the nil UUID, the first connected first

func (*Messaging) Name

func (*Messaging) Name() string

func (*Messaging) Providers

func (m *Messaging) Providers() []string

Providers returns the names of the providers, in order

func (*Messaging) Reply

func (m *Messaging) Reply(ctx context.Context, session uuid.UUID, out Outgoing) error

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

func (m *Messaging) Run(ctx context.Context, logger *slog.Logger) error

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

func (m *Messaging) SessionPrompt(ctx context.Context, session uuid.UUID) string

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

func (m *Messaging) SystemPrompt() string

SystemPrompt returns how channels work, and when to use the tools

func (*Messaging) Tools

func (m *Messaging) Tools() []llm.Tool

Tools returns the messaging tools

func (*Messaging) Type added in v1.6.0

func (*Messaging) Type() string

Type returns that the namespace's tools are built in

func (*Messaging) Watch added in v1.7.0

func (ns *Messaging) Watch(ctx context.Context, ch chan<- eventschema.Change) error

Watch sends the namespace's tools, which don't change

type OpenFunc added in v1.5.0

type OpenFunc func(ctx context.Context, session uuid.UUID, uri string) (File, io.ReadCloser, error)

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

func WithChat(fn ChatFunc) Opt

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

func WithFiles(fn OpenFunc) Opt

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 WithLinks(fn LinkFunc) Opt

WithLinks sets the function which changes the links of the text which is sent to a chat, such as a reply's, or the send tool's, so they open

func WithProvider

func WithProvider(provider Provider) Opt

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

func WithTracer(tracer trace.Tracer) Opt

WithTracer sets the tracer which traces what's sent, and received, or none, when it's nil

type Outgoing added in v1.5.0

type Outgoing struct {
	Text  string
	Files []string
}

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, or why it can't, such as that the
	// provider can't be reached
	Link(ctx context.Context, code string) (string, error)

	// 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. It's called
	// again, after a while, when it can't go on, unless its error is
	// ErrForbidden, such as for a token which isn't the provider's, and
	// after as long as its error says, at least, when it has a RetryAfter
	// method, such as when it's rate limited.
	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)
}

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

type SessionFunc func(context.Context, uuid.UUID) error

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.

func NewStore

func NewStore() Store

NewStore returns a store which keeps the channels in memory, so they aren't kept when the process ends, such as for tests

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.

Jump to

Keyboard shortcuts

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