provider

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2026 License: MIT Imports: 5 Imported by: 3

Documentation

Overview

Package provider defines the core interface for messaging providers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AgentProcessor

type AgentProcessor interface {
	Process(ctx context.Context, sessionID, content string) (string, error)
}

AgentProcessor processes messages through an AI agent.

type ChatType

type ChatType string

ChatType represents the type of chat.

const (
	ChatTypeDM      ChatType = "dm"
	ChatTypeGroup   ChatType = "group"
	ChatTypeChannel ChatType = "channel"
	ChatTypeThread  ChatType = "thread"
)

type Config

type Config struct {
	Enabled bool
	Token   string
}

Config is the base configuration for providers.

type Event

type Event struct {
	// Type is the event type.
	Type EventType

	// ProviderName is the provider this event came from.
	ProviderName string

	// ChatID is the chat/conversation identifier.
	ChatID string

	// Data contains event-specific data.
	Data map[string]any

	// Timestamp is when the event occurred.
	Timestamp time.Time
}

Event represents a provider event.

type EventHandler

type EventHandler func(ctx context.Context, event Event) error

EventHandler handles provider events.

type EventType

type EventType string

EventType represents the type of provider event.

const (
	EventTypeMessageEdited  EventType = "message_edited"
	EventTypeMessageDeleted EventType = "message_deleted"
	EventTypeReaction       EventType = "reaction"
	EventTypeTyping         EventType = "typing"
	EventTypePresence       EventType = "presence"
	EventTypeMemberJoined   EventType = "member_joined"
	EventTypeMemberLeft     EventType = "member_left"
	EventTypeChannelCreated EventType = "channel_created"
	EventTypeChannelDeleted EventType = "channel_deleted"
)

type IncomingMessage

type IncomingMessage struct {
	// ID is the unique message identifier.
	ID string

	// ProviderName is the provider this message came from (e.g., "telegram").
	ProviderName string

	// ChatID is the chat/conversation identifier.
	ChatID string

	// ChatType is the type of chat (dm, group, channel, thread).
	ChatType ChatType

	// SenderID is the sender's identifier.
	SenderID string

	// SenderName is the sender's display name.
	SenderName string

	// Content is the message text content.
	Content string

	// Media contains any attached media.
	Media []Media

	// ReplyTo is the ID of the message being replied to, if any.
	ReplyTo string

	// Timestamp is when the message was sent.
	Timestamp time.Time

	// Metadata contains provider-specific metadata.
	Metadata map[string]any
}

IncomingMessage represents a message received from a provider.

type Media

type Media struct {
	// Type is the media type (image, video, audio, document).
	Type MediaType

	// URL is the media URL (for remote media).
	URL string

	// Data is the raw media data (for local media).
	Data []byte

	// MimeType is the MIME type.
	MimeType string

	// Filename is the file name.
	Filename string

	// Caption is an optional caption.
	Caption string
}

Media represents attached media.

type MediaType

type MediaType string

MediaType represents the type of media.

const (
	MediaTypeImage    MediaType = "image"
	MediaTypeVideo    MediaType = "video"
	MediaTypeAudio    MediaType = "audio"
	MediaTypeDocument MediaType = "document"
	MediaTypeSticker  MediaType = "sticker"
	MediaTypeVoice    MediaType = "voice"
)

type MessageFormat

type MessageFormat string

MessageFormat represents the message format.

const (
	MessageFormatPlain    MessageFormat = "plain"
	MessageFormatMarkdown MessageFormat = "markdown"
	MessageFormatHTML     MessageFormat = "html"
)

type MessageHandler

type MessageHandler func(ctx context.Context, msg IncomingMessage) error

MessageHandler handles incoming messages.

type OutgoingMessage

type OutgoingMessage struct {
	// Content is the message text content.
	Content string

	// Media contains media to attach.
	Media []Media

	// ReplyTo is the ID of the message to reply to, if any.
	ReplyTo string

	// Format specifies the message format.
	Format MessageFormat

	// Metadata contains provider-specific options.
	Metadata map[string]any
}

OutgoingMessage represents a message to send through a provider.

type Provider

type Provider interface {
	// Name returns the provider name (e.g., "discord", "telegram", "whatsapp").
	Name() string

	// Connect establishes connection to the messaging platform.
	Connect(ctx context.Context) error

	// Disconnect closes the connection.
	Disconnect(ctx context.Context) error

	// Send sends a message to a specific chat/conversation.
	Send(ctx context.Context, chatID string, msg OutgoingMessage) error

	// OnMessage registers a handler for incoming messages.
	OnMessage(handler MessageHandler)

	// OnEvent registers a handler for platform events.
	OnEvent(handler EventHandler)
}

Provider is the interface that all messaging providers implement.

type RouteHandler

type RouteHandler struct {
	Pattern RoutePattern
	Handler MessageHandler
}

RouteHandler processes routed messages.

type RoutePattern

type RoutePattern struct {
	// Providers limits to specific providers (empty = all).
	Providers []string

	// ChatTypes limits to specific chat types (empty = all).
	ChatTypes []ChatType

	// Prefix matches messages starting with a prefix.
	Prefix string
}

RoutePattern defines which messages to match.

func All

func All() RoutePattern

All returns a pattern that matches all messages.

func DMOnly

func DMOnly() RoutePattern

DMOnly returns a pattern that matches only DM messages.

func FromProviders

func FromProviders(providers ...string) RoutePattern

FromProviders returns a pattern that matches messages from specific providers.

func GroupOnly

func GroupOnly() RoutePattern

GroupOnly returns a pattern that matches only group messages.

type Router

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

Router routes messages between providers and handlers.

func NewRouter

func NewRouter(logger *slog.Logger) *Router

NewRouter creates a new message router.

func (*Router) Broadcast

func (r *Router) Broadcast(ctx context.Context, chatIDs map[string]string, msg OutgoingMessage) error

Broadcast sends a message to all registered providers.

func (*Router) ConnectAll

func (r *Router) ConnectAll(ctx context.Context) error

ConnectAll connects all registered providers.

func (*Router) DisconnectAll

func (r *Router) DisconnectAll(ctx context.Context) error

DisconnectAll disconnects all registered providers.

func (*Router) GetProvider

func (r *Router) GetProvider(name string) (Provider, bool)

GetProvider returns a provider by name.

func (*Router) ListProviders

func (r *Router) ListProviders() []string

ListProviders returns all registered provider names.

func (*Router) OnMessage

func (r *Router) OnMessage(pattern RoutePattern, handler MessageHandler)

OnMessage adds a message handler with a pattern.

func (*Router) ProcessWithAgent

func (r *Router) ProcessWithAgent() MessageHandler

ProcessWithAgent creates a message handler that processes through the agent and sends responses.

func (*Router) ProcessWithVoice

func (r *Router) ProcessWithVoice(voice VoiceProcessor) MessageHandler

ProcessWithVoice creates a message handler with voice transcription and synthesis. It transcribes incoming voice notes, processes through the agent, and optionally responds with synthesized voice based on the voice processor's response mode.

func (*Router) Register

func (r *Router) Register(provider Provider)

Register adds a provider to the router.

func (*Router) Send

func (r *Router) Send(ctx context.Context, providerName, chatID string, msg OutgoingMessage) error

Send sends a message to a specific provider and chat.

func (*Router) SetAgent

func (r *Router) SetAgent(agent AgentProcessor)

SetAgent sets the agent processor for the router.

func (*Router) Unregister

func (r *Router) Unregister(name string)

Unregister removes a provider from the router.

type StreamingProvider

type StreamingProvider interface {
	Provider

	// SendTyping sends a typing indicator to a chat.
	SendTyping(ctx context.Context, chatID string) error

	// SendStream sends a message as a stream of chunks.
	SendStream(ctx context.Context, chatID string, chunks <-chan string) error
}

StreamingProvider extends Provider with typing indicators and streaming.

type VoiceProcessor

type VoiceProcessor interface {
	// TranscribeAudio converts audio to text.
	TranscribeAudio(ctx context.Context, audio []byte, mimeType string) (string, error)
	// SynthesizeSpeech converts text to audio. Returns audio bytes and MIME type.
	SynthesizeSpeech(ctx context.Context, text string) ([]byte, string, error)
	// ResponseMode returns the voice response mode ("auto", "always", "never").
	ResponseMode() string
}

VoiceProcessor handles voice transcription and synthesis.

Directories

Path Synopsis
Package providertest provides conformance test helpers for provider implementations.
Package providertest provides conformance test helpers for provider implementations.

Jump to

Keyboard shortcuts

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