gateway

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: Apache-2.0 Imports: 22 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package gateway connects Antares to messaging platforms. Every platform shares the same session store, memory, and tool surface.

Index

Constants

View Source
const DefaultIntents = (1 << 9) | (1 << 15) | (1 << 12)

DefaultIntents covers guild messages, message content, and direct messages.

Variables

This section is empty.

Functions

func BindingAdmits

func BindingAdmits(b *config.Binding, isDirect bool, userID string, roles []string) bool

BindingAdmits reports whether a binding admits a sender, combining its per-user and per-role allow lists:

  • both lists empty → admit (the binding sets no per-sender limit).
  • user in AllowedUsers → admit (an explicit user always wins).
  • a guild message whose sender holds a role in AllowedRoles → admit.
  • otherwise → deny.

Roles only apply to guild (non-direct) messages; a DM has no server roles, so in a DM only the user list is consulted. This means a DM restricted purely by role (roles set, user list empty) admits the paired owner rather than locking them out.

func ClearDiscordCommands

func ClearDiscordCommands(ctx context.Context, token, appID string) error

ClearDiscordCommands removes every registered global slash command.

func ClearTelegramCommands

func ClearTelegramCommands(ctx context.Context, token string) error

ClearTelegramCommands empties the command menu via deleteMyCommands.

func GatewaySessionKey added in v0.4.0

func GatewaySessionKey(cfg *config.Config, msg InboundMessage) string

GatewaySessionKey maps an inbound message to the KV key that holds its persistent session id. In a group channel with group_sessions_per_user set (the default), the sender's user id is folded in so each person gets their own conversation rather than sharing one channel-wide thread; a DM is already 1:1 so its key stays per-channel. Every place that reads, writes, or clears a gateway session must use this so a per-user session and its /new both target the same key.

func HasBindings

func HasBindings(bindings []config.Binding, platform string) bool

HasBindings reports whether a platform has any binding configured at all (enabled or not). When true and ResolveBinding returns nil for a GROUP channel, the channel is unregistered and the bot should stay silent. Direct messages are never gated this way — the caller lets DMs through regardless.

func RegisterDiscordCommands

func RegisterDiscordCommands(ctx context.Context, token, appID string) error

RegisterDiscordCommands publishes the gateway command set as global slash commands. Global commands can take up to an hour to propagate on Discord's side the first time; re-registering the same set is cheap and idempotent.

func ResolveBinding

func ResolveBinding(bindings []config.Binding, platform, guildID, channelID string) *config.Binding

ResolveBinding picks the most specific enabled binding for a message on a platform, given its guild (server) and channel ids. Specificity order:

guild + channel  >  channel only  >  guild only  >  platform only

It returns nil when no binding matches. A nil result from a platform that has NO bindings at all means "use the default agent"; a nil result from a platform that DOES have bindings means "ignore this channel" — the caller distinguishes the two with HasBindings.

func SetTelegramCommands

func SetTelegramCommands(ctx context.Context, token string) error

SetTelegramCommands publishes the command menu via setMyCommands.

Types

type Adapter

type Adapter interface {
	Name() string
	// Start blocks until ctx is cancelled or the connection fails fatally.
	Start(ctx context.Context) error
	// Send delivers a message; it returns the platform message id.
	Send(ctx context.Context, r Reply) (string, error)
	// Connected reports the live connection state for the dashboard.
	Connected() bool
}

Adapter is one platform connection.

type Discord

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

Discord connects to the bot gateway over WebSocket and replies over REST.

func NewDiscord

func NewDiscord(cfg config.Discord, mgr *Manager) *Discord

NewDiscord builds the Discord adapter.

func (*Discord) Connected

func (d *Discord) Connected() bool

func (*Discord) Name

func (d *Discord) Name() string

func (*Discord) Send

func (d *Discord) Send(ctx context.Context, r Reply) (string, error)

Send posts a message and returns its id.

func (*Discord) Start

func (d *Discord) Start(ctx context.Context) error

Start connects to the gateway and processes events until ctx is cancelled.

type DiscordChannel

type DiscordChannel struct {
	ID       string `json:"id"`
	Name     string `json:"name"`
	Type     int    `json:"type"`
	ParentID string `json:"parent_id,omitempty"`
}

DiscordChannel is a channel within a guild. Type 0 is a text channel, 5 an announcement channel — the ones a bot can post to and worth binding.

func ListDiscordChannels

func ListDiscordChannels(ctx context.Context, token, guildID string) ([]DiscordChannel, error)

ListDiscordChannels returns the text and announcement channels of a guild.

type DiscordGuild

type DiscordGuild struct {
	ID   string `json:"id"`
	Name string `json:"name"`
	Icon string `json:"icon,omitempty"`
}

DiscordGuild is a server the bot belongs to.

func ListDiscordGuilds

func ListDiscordGuilds(ctx context.Context, token string) ([]DiscordGuild, error)

ListDiscordGuilds returns the servers the bot is a member of.

type DiscordRole

type DiscordRole struct {
	ID   string `json:"id"`
	Name string `json:"name"`
}

DiscordRole is a server role the bot can gate access by.

func ListDiscordRoles

func ListDiscordRoles(ctx context.Context, token, guildID string) ([]DiscordRole, error)

ListDiscordRoles returns a guild's roles (excluding @everyone), so the UI can offer them as an access allow-list.

type Feishu

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

Feishu (Lark) receives events by webhook and sends with a tenant token. The adapter runs its own listener and refreshes the tenant token as needed. It handles plaintext (unencrypted) event delivery.

func NewFeishu

func NewFeishu(cfg config.Feishu, mgr *Manager) *Feishu

NewFeishu builds the Feishu adapter.

func (*Feishu) Connected

func (f *Feishu) Connected() bool

func (*Feishu) Name

func (f *Feishu) Name() string

func (*Feishu) Send

func (f *Feishu) Send(ctx context.Context, r Reply) (string, error)

Send posts a text message to a chat.

func (*Feishu) Start

func (f *Feishu) Start(ctx context.Context) error

Start serves the event webhook until the context is cancelled.

type Handler

type Handler func(ctx context.Context, msg InboundMessage, partial func(string)) (string, error)

Handler processes an inbound message and streams partial replies. The returned string is the final text.

type Identity

type Identity struct {
	Name   string `json:"name"`
	Handle string `json:"handle"`
}

Identity is what a platform reports about the bot behind a token.

func VerifyToken

func VerifyToken(ctx context.Context, platform, token string) (*Identity, error)

VerifyToken asks the platform whether a bot token is real before Antares stores it. Saving an unverified token means the failure surfaces later, in a background reconnect loop, where nobody is watching.

type InboundMessage

type InboundMessage struct {
	Platform  string
	ChannelID string
	// GuildID is the server a channel belongs to (Discord). Empty for platforms
	// without a server concept (Telegram) and for direct messages.
	GuildID  string
	UserID   string
	UserName string
	// DisplayName is the friendliest name for the sender — a Discord server
	// nickname, else the account display name, else the username. Empty falls
	// back to UserName. Shown to the agent so it can address the person.
	DisplayName string
	// Roles are the sender's server-role ids (Discord guild members). Used to
	// gate a server binding by role. Empty in DMs and on platforms without roles.
	Roles []string
	Text  string
	// IsDirect marks a 1:1 chat, where the agent replies without being addressed.
	IsDirect bool
	// MessageID lets adapters edit their own reply while streaming.
	MessageID string
}

InboundMessage is one message received from a platform.

type Manager

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

Manager supervises the enabled adapters.

func NewManager

func NewManager(cfg *config.Config, db store.Store, handler Handler) *Manager

NewManager builds a gateway manager.

func (*Manager) Deliver

func (m *Manager) Deliver(ctx context.Context, target, text string) error

Deliver sends text to "platform:channel", used by the cron scheduler.

func (*Manager) Reconcile added in v0.4.0

func (m *Manager) Reconcile(cfg *config.Config) error

Reconcile publishes the new configuration and restarts only the adapters whose per-platform settings (or the master Gateway.Enabled switch) actually changed since the last successful apply. Untouched settings like bindings just take effect on the next message because the inbound handler re-reads the live config.

The diff is against applied-state, not the previously published config, so a retry after a stubborn stop still sees the platform as dirty rather than silently agreeing with the freshly-published pointer. If some platforms fail to reconcile within the bounded stop wait, Reconcile returns their combined error and leaves them tracked for the next attempt; the platforms that did reconcile stay reconciled.

A nil baseCtx means Start has not been called yet: publish the config and let a later Start pick it up. Returning an error here would leave the dashboard reporting a failed save for a manager that simply is not up yet.

func (*Manager) SetConfig

func (m *Manager) SetConfig(cfg *config.Config)

SetConfig swaps in a reloaded configuration. Reload replaces the whole config pointer, so without this the manager would keep reading the values it was built with and Sync would reconcile against a stale file. It is kept as a plain setter for callers that only need the pointer refreshed; Reconcile is the entry point that also brings running adapters into line.

func (*Manager) Start

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

Start launches every enabled platform and keeps it running until ctx ends.

func (*Manager) Status

func (m *Manager) Status() map[string]bool

Status reports each platform's connection state.

func (*Manager) Stop

func (m *Manager) Stop(name string)

Stop shuts down one platform. Held under opMu so a manual Stop cannot race a concurrent Reconcile or Sync onto the same slot. After a successful Stop the platform is treated as unapplied, so the next Reconcile against a config that wants it live will restart it — a manual Stop is a temporary override, not a permanent disable.

func (*Manager) StopAll

func (m *Manager) StopAll()

StopAll shuts down every platform. Fully resets the applied bookkeeping, so a subsequent Start or Reconcile treats every platform as freshly unconfigured.

func (*Manager) Sync

func (m *Manager) Sync(platform string) error

Sync brings one platform in line with the current configuration: it stops a running adapter and starts a fresh one when the platform should be live. Without this, saving a token or flipping a switch in the dashboard only took effect after restarting the process, which is a poor thing to ask of someone who just pasted a token. Sync serializes with Reconcile and StopAll through opMu so two lifecycle ops cannot both try to install a replacement into the same slot.

type Matrix

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

Matrix connects to a homeserver with an access token and long-polls /sync. Replies are sent with PUT .../send. It needs no public URL.

func NewMatrix

func NewMatrix(cfg config.Matrix, mgr *Manager) *Matrix

NewMatrix builds the Matrix adapter.

func (*Matrix) Connected

func (m *Matrix) Connected() bool

func (*Matrix) Name

func (m *Matrix) Name() string

func (*Matrix) Send

func (m *Matrix) Send(ctx context.Context, r Reply) (string, error)

Send posts a message to a room. Matrix has no cheap edit, so EditID is ignored and a new message is sent.

func (*Matrix) Start

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

Start syncs from "now" (skipping history) and processes new messages until the context is cancelled.

type Reply

type Reply struct {
	ChannelID string
	Text      string
	// ReplyTo threads the response onto the triggering message when supported.
	ReplyTo string
	// EditID updates a previously sent message instead of posting a new one.
	EditID string
}

Reply is what an adapter sends back.

type Signal

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

Signal talks to a signal-cli REST API daemon that the user runs. It polls the receive endpoint and posts to send, so it needs no public URL of its own.

func NewSignal

func NewSignal(cfg config.Signal, mgr *Manager) *Signal

NewSignal builds the Signal adapter.

func (*Signal) Connected

func (s *Signal) Connected() bool

func (*Signal) Name

func (s *Signal) Name() string

func (*Signal) Send

func (s *Signal) Send(ctx context.Context, r Reply) (string, error)

Send posts a message to a recipient or group.

func (*Signal) Start

func (s *Signal) Start(ctx context.Context) error

Start polls the receive endpoint until the context is cancelled.

type Slack

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

Slack connects over Socket Mode — an outbound WebSocket opened with an app-level token — so it needs no public URL. Replies go over the Web API with the bot token.

func NewSlack

func NewSlack(cfg config.Slack, mgr *Manager) *Slack

NewSlack builds the Slack adapter.

func (*Slack) Connected

func (s *Slack) Connected() bool

func (*Slack) Name

func (s *Slack) Name() string

func (*Slack) Send

func (s *Slack) Send(ctx context.Context, r Reply) (string, error)

Send posts a new message, or edits one when EditID is set. It returns the message ts.

func (*Slack) Start

func (s *Slack) Start(ctx context.Context) error

Start opens the socket and reconnects until the context is cancelled.

type Telegram

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

Telegram talks to the Bot API over long polling, so no public domain or webhook is required — it works behind a home NAT.

func NewTelegram

func NewTelegram(cfg config.Telegram, mgr *Manager) *Telegram

NewTelegram builds the Telegram adapter.

func (*Telegram) Connected

func (t *Telegram) Connected() bool

func (*Telegram) Name

func (t *Telegram) Name() string

func (*Telegram) Send

func (t *Telegram) Send(ctx context.Context, r Reply) (string, error)

Send posts or edits a message and returns its id.

func (*Telegram) Start

func (t *Telegram) Start(ctx context.Context) error

Start long-polls for updates until ctx is cancelled.

type WhatsApp

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

WhatsApp uses the Meta Cloud API. Inbound messages arrive by webhook, so the adapter runs its own HTTP listener; outbound goes to the Graph API. The user points Meta's webhook at this listener (directly or through a reverse proxy).

func NewWhatsApp

func NewWhatsApp(cfg config.WhatsApp, mgr *Manager) *WhatsApp

NewWhatsApp builds the WhatsApp adapter.

func (*WhatsApp) Connected

func (w *WhatsApp) Connected() bool

func (*WhatsApp) Name

func (w *WhatsApp) Name() string

func (*WhatsApp) Send

func (w *WhatsApp) Send(ctx context.Context, r Reply) (string, error)

Send posts a text message through the Graph API.

func (*WhatsApp) Start

func (w *WhatsApp) Start(ctx context.Context) error

Start serves the webhook until the context is cancelled.

Jump to

Keyboard shortcuts

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