Documentation
¶
Overview ¶
Package provider defines the core interface for messaging providers.
Index ¶
- type AgentProcessor
- type ChatType
- type Config
- type Event
- type EventHandler
- type EventType
- type IncomingMessage
- type Media
- type MediaType
- type MessageFormat
- type MessageHandler
- type OutgoingMessage
- type Provider
- type RouteHandler
- type RoutePattern
- type Router
- func (r *Router) Broadcast(ctx context.Context, chatIDs map[string]string, msg OutgoingMessage) error
- func (r *Router) ConnectAll(ctx context.Context) error
- func (r *Router) DisconnectAll(ctx context.Context) error
- func (r *Router) GetProvider(name string) (Provider, bool)
- func (r *Router) ListProviders() []string
- func (r *Router) OnMessage(pattern RoutePattern, handler MessageHandler)
- func (r *Router) ProcessWithAgent() MessageHandler
- func (r *Router) ProcessWithVoice(voice VoiceProcessor) MessageHandler
- func (r *Router) Register(provider Provider)
- func (r *Router) Send(ctx context.Context, providerName, chatID string, msg OutgoingMessage) error
- func (r *Router) SetAgent(agent AgentProcessor)
- func (r *Router) Unregister(name string)
- type StreamingProvider
- type VoiceProcessor
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 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 ¶
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 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 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 (*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 ¶
ConnectAll connects all registered providers.
func (*Router) DisconnectAll ¶
DisconnectAll disconnects all registered providers.
func (*Router) GetProvider ¶
GetProvider returns a provider by name.
func (*Router) ListProviders ¶
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) SetAgent ¶
func (r *Router) SetAgent(agent AgentProcessor)
SetAgent sets the agent processor for the router.
func (*Router) Unregister ¶
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. |