channels

package
v0.101.0 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package channels provides the ChatChannel interface for all chat platform integrations.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoChannelForPlatform = &ChannelError{Code: "NO_CHANNEL", Message: "no channel registered for platform"}
	ErrInvalidSignature     = &ChannelError{Code: "INVALID_SIGNATURE", Message: "webhook signature validation failed"}
	ErrInvalidPayload       = &ChannelError{Code: "INVALID_PAYLOAD", Message: "could not parse webhook payload"}
	ErrUnauthorized         = &ChannelError{Code: "UNAUTHORIZED", Message: "user not authorized for this platform"}
	ErrMediaDownloadFailed  = &ChannelError{Code: "MEDIA_FAILED", Message: "failed to download media"}
)

Errors

Functions

This section is empty.

Types

type ChannelError

type ChannelError struct {
	Code    string
	Message string
	Err     error
}

ChannelError represents an error in channel operations.

func (*ChannelError) Error

func (e *ChannelError) Error() string

func (*ChannelError) IsRetryable

func (e *ChannelError) IsRetryable() bool

IsRetryable returns true if the error is transient and the operation can be retried.

func (*ChannelError) Unwrap

func (e *ChannelError) Unwrap() error

type ChannelRouter

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

ChannelRouter routes incoming messages to the appropriate handler. It verifies user credentials and forwards messages to the AI agent system. Concurrent-safe for Register and GetChannel operations.

func NewChannelRouter

func NewChannelRouter(media MediaHandler) *ChannelRouter

NewChannelRouter creates a new channel router.

func (*ChannelRouter) Close

func (r *ChannelRouter) Close() error

Close closes all registered channels. Concurrent-safe: uses write lock.

func (*ChannelRouter) GetChannel

func (r *ChannelRouter) GetChannel(platform chat_apps.Platform) ChatChannel

GetChannel returns the channel for a platform, or nil if not registered. Concurrent-safe: uses read lock.

func (*ChannelRouter) HandleWebhook

func (r *ChannelRouter) HandleWebhook(ctx context.Context, platform chat_apps.Platform, headers map[string]string, body []byte) (*chat_apps.IncomingMessage, error)

HandleWebhook handles an incoming webhook request.

func (*ChannelRouter) Register

func (r *ChannelRouter) Register(channel ChatChannel)

Register registers a chat channel for a platform. Concurrent-safe: uses write lock.

func (*ChannelRouter) SendChunkedResponse

func (r *ChannelRouter) SendChunkedResponse(ctx context.Context, platform chat_apps.Platform, chatID string, chunks <-chan string) error

SendChunkedResponse sends a streaming AI response to a chat platform.

func (*ChannelRouter) SendResponse

func (r *ChannelRouter) SendResponse(ctx context.Context, platform chat_apps.Platform, msg *chat_apps.OutgoingMessage) error

SendResponse sends a single response message to a chat platform.

type ChatChannel

type ChatChannel interface {
	// Name returns the platform name (e.g., "telegram", "whatsapp", "dingtalk").
	Name() chat_apps.Platform

	// ValidateWebhook verifies the incoming webhook request.
	// Returns an error if the request signature is invalid or the request is malformed.
	ValidateWebhook(ctx context.Context, headers map[string]string, body []byte) error

	// ParseMessage parses the incoming webhook payload into an IncomingMessage.
	// The payload format is platform-specific (JSON for most platforms).
	ParseMessage(ctx context.Context, payload []byte) (*chat_apps.IncomingMessage, error)

	// SendMessage sends a single message to the chat platform.
	SendMessage(ctx context.Context, msg *chat_apps.OutgoingMessage) error

	// SendChunkedMessage sends streaming content chunks.
	// The channels function closes when all chunks are sent or an error occurs.
	// This is used for streaming AI responses.
	SendChunkedMessage(ctx context.Context, chatID string, chunks <-chan string) error

	// DownloadMedia downloads media from the platform's CDN.
	// Returns the media data, MIME type, and an error if any.
	DownloadMedia(ctx context.Context, url string) ([]byte, string, error)

	// Close closes any open connections and releases resources.
	Close() error
}

ChatChannel defines the interface for all chat platform integrations. Each platform (Telegram, WhatsApp, DingTalk) implements this interface.

type MediaHandler

type MediaHandler interface {
	// ProcessAudio converts audio data to text using Whisper.
	ProcessAudio(ctx context.Context, data []byte, mimeType string) (string, error)

	// ProcessImage extracts text from images using OCR.
	ProcessImage(ctx context.Context, data []byte) (string, error)
}

MediaHandler processes multimedia messages (audio, images).

Directories

Path Synopsis
Package dingtalk provides cryptographic utilities for DingTalk.
Package dingtalk provides cryptographic utilities for DingTalk.
Package telegram implements the Telegram Bot channel.
Package telegram implements the Telegram Bot channel.
Package whatsapp implements WhatsApp integration via Baileys Node.js bridge.
Package whatsapp implements WhatsApp integration via Baileys Node.js bridge.

Jump to

Keyboard shortcuts

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