managarten/services/mana-matrix-bot/internal/plugin/plugin.go
Till JS 819568c3df feat(infra): consolidate 21 Matrix bots into Go binary + add Go API gateway
Replace 21 separate NestJS Matrix bot processes (~2.1 GB RAM, ~4.2 GB Docker images)
with a single Go binary using plugin architecture (8.6 MB binary, ~30 MB RAM).

New services:
- services/mana-matrix-bot/ — Go Matrix bot with 21 plugins (mautrix-go, Redis sessions)
- services/mana-api-gateway-go/ — Go API gateway (rate limiting, API keys, credit billing)

Deleted:
- 21 services/matrix-*-bot/ directories
- packages/bot-services/ and packages/matrix-bot-common/
- Legacy deploy scripts and CI build jobs

Updated:
- docker-compose.macmini.yml: new Go services, legacy bots removed
- CI/CD: change detection + build jobs for Go services
- Root package.json: new dev:matrix, build:matrix, test:matrix scripts

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-27 21:03:00 +01:00

99 lines
3.1 KiB
Go

package plugin
import (
"context"
"time"
)
// MessageContext carries all information about an incoming message.
type MessageContext struct {
RoomID string
Sender string
EventID string
Body string // text body (for text messages)
IsVoice bool // true if transcribed from audio
Client MatrixClient
Session *SessionAccess
}
// MatrixClient is the interface plugins use to interact with Matrix.
type MatrixClient interface {
SendMessage(ctx context.Context, roomID string, text string) (string, error)
SendReply(ctx context.Context, roomID string, eventID string, text string) (string, error)
SendNotice(ctx context.Context, roomID string, text string) (string, error)
EditMessage(ctx context.Context, roomID string, eventID string, text string) (string, error)
SetTyping(ctx context.Context, roomID string, typing bool) error
DownloadMedia(ctx context.Context, mxcURL string) ([]byte, error)
UploadMedia(ctx context.Context, data []byte, contentType string, filename string) (string, error)
SendAudio(ctx context.Context, roomID string, mxcURL string, filename string, size int) (string, error)
GetUserID() string
}
// SessionAccess provides per-user session operations for a plugin.
type SessionAccess struct {
UserID string
Manager SessionManager
}
// SessionManager is the interface for session storage.
type SessionManager interface {
Get(userID, key string) (any, bool)
Set(userID, key string, value any)
Delete(userID, key string)
GetToken(userID string) (string, bool)
SetToken(userID, token string, expiresAt time.Time)
IsLoggedIn(userID string) bool
}
// Plugin is the interface every bot plugin must implement.
type Plugin interface {
// Name returns the unique plugin identifier (e.g., "todo", "calendar").
Name() string
// Init is called once during startup with the plugin's config.
Init(ctx context.Context, cfg PluginConfig) error
// HandleTextMessage is called for m.text events.
HandleTextMessage(ctx context.Context, mc *MessageContext) error
// Commands returns the list of commands this plugin handles.
Commands() []CommandDef
}
// AudioHandler is optionally implemented by plugins that handle voice messages.
type AudioHandler interface {
HandleAudioMessage(ctx context.Context, mc *MessageContext, audioData []byte) error
}
// ImageHandler is optionally implemented by plugins that handle image messages.
type ImageHandler interface {
HandleImageMessage(ctx context.Context, mc *MessageContext) error
}
// Scheduler is optionally implemented by plugins that need periodic tasks.
type Scheduler interface {
ScheduledTasks() []ScheduledTask
}
// ScheduledTask defines a periodic task.
type ScheduledTask struct {
Name string
Interval time.Duration
Run func(ctx context.Context) error
}
// CommandDef describes a command for help text and routing.
type CommandDef struct {
Patterns []string // e.g., ["!todo", "!add", "!neu"]
Description string
Category string // e.g., "Aufgaben", "Kalender"
}
// PluginConfig holds per-plugin configuration.
type PluginConfig struct {
Enabled bool
AccessToken string
AllowedRooms []string
BackendURL string
Extra map[string]string
}