gateway

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2026 License: MIT Imports: 8 Imported by: 0

README

pkg/gateway

External platform adapters for the bc notification system.

Overview

This package defines the NotificationAdapter interface and Manager that orchestrate connections to 34 external platforms. Each adapter connects to a platform, receives inbound events, and forwards them to pkg/notify for dispatch to subscribed agents.

Key Types

  • NotificationAdapter — interface each platform implements (Name, Type, Start, Stop, HTTPHandler, Channels, Status)
  • Notification — normalized inbound event (Channel, Platform, Sender, Content, Mentions, Timestamp, Raw json.RawMessage)
  • Manager — registers adapters, discovers channels, routes inbound events
  • ChannelInfo — a channel/group discovered on a platform (ID, Name, Platform)
  • AdapterStatus — connection state (Connected, Error, BotName, LastMessageAt, MessageCount)
  • AdapterType — socket, webhook, or poll

Adapters

Platform Directory Transport
Slack slack/ Socket Mode (WebSocket)
Telegram telegram/ Bot API long-polling
Discord discord/ Gateway WebSocket
GitHub github/ Webhook
GitLab gitlab/ Webhook
Sentry sentry/ Webhook
PagerDuty pagerduty/ Webhook
Datadog datadog/ Webhook
Grafana grafana/ Webhook
RSS rss/ Poll
Notion notion/ Poll
WhatsApp whatsapp/ Socket
Matrix matrix/ Poll
+ 21 more see subdirectories various

Architecture

See docs/architecture/notifications.md for the full notification architecture, including message flow diagrams, credential injection, and how to add new adapters.

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 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"`
	MessageCount  int64     `json:"message_count"`
}

AdapterStatus reports connection state for the web UI.

type AdapterType added in v0.2.1

type AdapterType string

AdapterType identifies the connection pattern for a NotificationAdapter.

const (
	// AdapterSocket is a long-lived connection (WebSocket, polling loop).
	AdapterSocket AdapterType = "socket"
	// AdapterWebhook is an HTTP endpoint where the platform POSTs events to bc.
	AdapterWebhook AdapterType = "webhook"
	// AdapterPoll is timer-based polling where bc fetches new events.
	AdapterPoll AdapterType = "poll"
)

type ChannelInfo added in v0.2.1

type ChannelInfo struct {
	ID       string `json:"id"`
	Name     string `json:"name"`
	Platform string `json:"platform"`
}

ChannelInfo represents a discovered channel on a platform.

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 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) AdapterNames added in v0.2.1

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

AdapterNames returns the names of all registered adapters.

func (*Manager) AdapterStatus

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

AdapterStatus returns the connection status for a specific adapter.

func (*Manager) DiscoveredSources added in v0.2.1

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

DiscoveredSources returns all discovered external channels.

func (*Manager) GetAdapter added in v0.2.1

func (m *Manager) GetAdapter(name string) NotificationAdapter

GetAdapter returns a registered adapter by name, or nil if not found.

func (*Manager) HandleNotification added in v0.2.1

func (m *Manager) HandleNotification(platform string, n Notification)

HandleNotification processes an inbound notification from a specific platform.

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 NotificationAdapter)

Register adds a NotificationAdapter to the manager.

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, raw json.RawMessage))

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(_ context.Context)

Stop gracefully shuts down all adapters.

func (*Manager) WebhookHandlers added in v0.2.1

func (m *Manager) WebhookHandlers() map[string]http.Handler

WebhookHandlers returns HTTP handlers for all webhook-type adapters, keyed by adapter name. The server mounts these at /hooks/{name}.

type Notification added in v0.2.1

type Notification struct {
	Timestamp time.Time       `json:"timestamp"`
	Raw       json.RawMessage `json:"raw"`
	Channel   string          `json:"channel"`
	Platform  string          `json:"platform"`
	Sender    string          `json:"sender"`
	Content   string          `json:"content"` // human-readable text for display/storage
	Mentions  []string        `json:"mentions"`
}

Notification is a normalized inbound event from an external platform. The Raw field contains the complete platform payload as JSON.

type NotificationAdapter added in v0.2.1

type NotificationAdapter interface {
	// Name returns the adapter identifier ("slack", "github", "telegram").
	Name() string

	// Type returns the connection pattern (socket, webhook, or poll).
	Type() AdapterType

	// Start connects to the platform and begins receiving notifications.
	// Calls handler for each inbound event with raw JSON payload.
	// Blocks until ctx is canceled. For webhook adapters, this is a no-op.
	Start(ctx context.Context, handler func(Notification)) error

	// Stop gracefully disconnects from the platform.
	Stop() error

	// HTTPHandler returns an http.Handler for webhook-based adapters.
	// Socket and poll adapters return nil.
	HTTPHandler() http.Handler

	// Channels returns discovered channels/groups the bot has access to.
	Channels() []ChannelInfo

	// Status returns the adapter's connection state for the web UI.
	Status() AdapterStatus
}

NotificationAdapter handles the platform connection lifecycle. This is the new interface that all adapters should implement.

type PersistedChannel

type PersistedChannel struct {
	BCChannel  string
	Platform   string
	PlatformID string
}

PersistedChannel is a saved bc_channel → platform_id mapping.

Directories

Path Synopsis
Package discord implements the gateway.NotificationAdapter for Discord.
Package discord implements the gateway.NotificationAdapter for Discord.
Package github implements the gateway.NotificationAdapter for GitHub webhooks.
Package github implements the gateway.NotificationAdapter for GitHub webhooks.
Package imessage implements a gateway.NotificationAdapter that polls the BlueBubbles API for new iMessage messages.
Package imessage implements a gateway.NotificationAdapter that polls the BlueBubbles API for new iMessage messages.
Package irc implements a gateway.NotificationAdapter for IRC channels using the ergochat/irc-go library.
Package irc implements a gateway.NotificationAdapter for IRC channels using the ergochat/irc-go library.
Package matrix implements a gateway.NotificationAdapter that polls the Matrix client-server API /sync endpoint for new events.
Package matrix implements a gateway.NotificationAdapter that polls the Matrix client-server API /sync endpoint for new events.
Package mattermost implements a gateway.NotificationAdapter using the Mattermost WebSocket API for real-time message events.
Package mattermost implements a gateway.NotificationAdapter using the Mattermost WebSocket API for real-time message events.
Package mqtt implements a gateway.NotificationAdapter for MQTT topics using the Eclipse Paho MQTT client.
Package mqtt implements a gateway.NotificationAdapter for MQTT topics using the Eclipse Paho MQTT client.
Package notion implements a gateway.NotificationAdapter that polls the Notion API for recently updated pages and databases.
Package notion implements a gateway.NotificationAdapter that polls the Notion API for recently updated pages and databases.
Package reddit implements a gateway.NotificationAdapter that polls the Reddit API for new posts and comments in a subreddit.
Package reddit implements a gateway.NotificationAdapter that polls the Reddit API for new posts and comments in a subreddit.
Package rss implements a gateway.NotificationAdapter that polls RSS 2.0 and Atom feeds on a configurable interval.
Package rss implements a gateway.NotificationAdapter that polls RSS 2.0 and Atom feeds on a configurable interval.
Package signal implements a gateway.NotificationAdapter that polls a signal-cli REST API for new messages.
Package signal implements a gateway.NotificationAdapter that polls a signal-cli REST API for new messages.
Package slackgw implements the gateway.NotificationAdapter for Slack.
Package slackgw implements the gateway.NotificationAdapter for Slack.
Package telegram implements the gateway.NotificationAdapter for Telegram Bot API.
Package telegram implements the gateway.NotificationAdapter for Telegram Bot API.
Package twitter implements a gateway.NotificationAdapter that polls the Twitter API v2 mentions endpoint for new mentions.
Package twitter implements a gateway.NotificationAdapter that polls the Twitter API v2 mentions endpoint for new mentions.
Package webhook implements a generic gateway.NotificationAdapter that receives arbitrary JSON payloads via HTTP POST.
Package webhook implements a generic gateway.NotificationAdapter that receives arbitrary JSON payloads via HTTP POST.
Package whatsapp implements a gateway.NotificationAdapter using whatsmeow (WhatsApp Web multi-device protocol).
Package whatsapp implements a gateway.NotificationAdapter using whatsmeow (WhatsApp Web multi-device protocol).

Jump to

Keyboard shortcuts

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