gateway

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package gateway provides external messaging platform integrations. It bridges bc channels to platforms like Telegram, Discord, and Slack.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Truncate

func Truncate(s string, n int) string

Truncate shortens a string to n characters, appending "..." if truncated.

Types

type Adapter

type Adapter interface {
	// Name returns the platform identifier ("telegram", "discord", "slack").
	Name() string

	// Start connects to the platform and begins receiving messages.
	// Calls onMessage for each inbound message. Blocks until ctx is canceled.
	Start(ctx context.Context, onMessage func(InboundMessage)) error

	// Stop gracefully disconnects from the platform.
	Stop(ctx context.Context) error

	// Send delivers a message to a platform channel.
	Send(ctx context.Context, channelID, sender, content string) error

	// Channels returns all channels/groups the bot is a member of.
	Channels(ctx context.Context) ([]ExternalChannel, error)

	// Health returns nil if the adapter is connected and operational.
	Health(ctx context.Context) error
}

Adapter handles the platform connection lifecycle and message routing.

type AdapterStatus

type AdapterStatus struct {
	LastMessageAt time.Time `json:"last_message_at,omitempty"`
	Error         string    `json:"error,omitempty"`
	BotName       string    `json:"bot_name,omitempty"`
	Connected     bool      `json:"connected"`
}

AdapterStatus reports connection state for the web UI.

type Attachment

type Attachment struct {
	URL      string `json:"url"`
	Name     string `json:"name"`
	MimeType string `json:"mime_type"`
	Source   string `json:"source"` // "slack", "telegram", "discord", "local"
	FileID   string `json:"file_id,omitempty"`
	Size     int64  `json:"size,omitempty"`
}

Attachment represents a file attached to a message.

type ChannelStore

type ChannelStore interface {
	SaveChannel(ctx context.Context, bcChannel, platform, platformID string) error
	LoadChannels(ctx context.Context) ([]PersistedChannel, error)
}

ChannelStore persists channel mappings so they survive server restarts. Implemented by notify.Store via a wrapper.

type ExternalChannel

type ExternalChannel struct {
	ID   string
	Name string
	Type string // "group", "channel", "dm"
}

ExternalChannel represents a channel/group on an external platform.

type FileSender

type FileSender interface {
	// SendFile uploads a file to a platform channel.
	SendFile(ctx context.Context, channelID, sender, filename string, data []byte, mimeType string) error
}

FileSender is optionally implemented by adapters that support file uploads.

type InboundMessage

type InboundMessage struct {
	Timestamp   time.Time
	ChannelID   string
	ChannelName string
	Sender      string
	SenderID    string
	Content     string
	MessageID   string
	Attachments []Attachment
}

InboundMessage is a normalized message from an external platform.

type Manager

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

Manager orchestrates all gateway adapters and routes messages.

func NewManager

func NewManager() *Manager

NewManager creates a new gateway manager.

func (*Manager) AdapterStatus

func (m *Manager) AdapterStatus(platform string) AdapterStatus

AdapterStatus returns the connection status for a specific adapter. If the adapter implements StatusReporter, uses that; otherwise infers from channels.

func (*Manager) ExternalChannels

func (m *Manager) ExternalChannels() []string

ExternalChannels returns all discovered external channels.

func (*Manager) IsGatewayChannel

func (m *Manager) IsGatewayChannel(name string) bool

IsGatewayChannel returns true if the channel name belongs to an external gateway.

func (*Manager) Register

func (m *Manager) Register(adapter Adapter)

Register adds an adapter to the manager.

func (*Manager) SeedChannel

func (m *Manager) SeedChannel(bcChannel string)

SeedChannel adds a known gateway channel to the channel map. Used on startup to restore mappings for channels that were dynamically discovered in previous sessions. The channelID is set to the channel name suffix (e.g., "all-bc" for "slack:all-bc") since the platform adapter will resolve it.

func (*Manager) Send

func (m *Manager) Send(ctx context.Context, bcChannel, sender, content string) (bool, error)

Send routes a message from a bc channel to the appropriate external platform. Returns true if the channel is an external gateway channel and was handled.

func (*Manager) SendFile

func (m *Manager) SendFile(ctx context.Context, bcChannel, sender, filename string, data []byte, mimeType string) (bool, error)

SendFile uploads a file to a gateway channel. Returns false if the channel is not a gateway channel or the adapter doesn't support file uploads.

func (*Manager) SetChannelStore

func (m *Manager) SetChannelStore(store ChannelStore)

SetChannelStore sets the persistence store for channel mappings.

func (*Manager) SetInboundHandler

func (m *Manager) SetInboundHandler(fn func(bcChannel, sender, content string))

SetInboundHandler sets the callback for inbound messages from external platforms.

func (*Manager) Start

func (m *Manager) Start(ctx context.Context) error

Start discovers channels from all adapters and begins receiving messages.

func (*Manager) Stop

func (m *Manager) Stop(ctx context.Context)

Stop gracefully shuts down all adapters.

type PersistedChannel

type PersistedChannel struct {
	BCChannel  string
	Platform   string
	PlatformID string
}

PersistedChannel is a saved bc_channel → platform_id mapping.

type StatusReporter

type StatusReporter interface {
	// Status returns the current connection state for UI display.
	Status() AdapterStatus
}

StatusReporter is optionally implemented by adapters that report connection state.

Directories

Path Synopsis
Package discord implements the gateway.Adapter for Discord.
Package discord implements the gateway.Adapter for Discord.
Package slackgw implements the gateway.Adapter for Slack.
Package slackgw implements the gateway.Adapter for Slack.
Package telegram implements the gateway.Adapter for Telegram Bot API.
Package telegram implements the gateway.Adapter for Telegram Bot API.

Jump to

Keyboard shortcuts

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