Documentation
¶
Overview ¶
Package matrix provides a reusable Matrix bot client built on top of mautrix. It supports encrypted rooms, message handling, and formatted (HTML) responses.
Environment variables:
- MATRIX_API_URL: Matrix homeserver URL
- MATRIX_API_USER: Matrix username (localpart)
- MATRIX_API_PASS: Matrix password
Index ¶
- func MarkdownToHTML(md string) string
- type Bot
- func (b *Bot) Client() *mautrix.Client
- func (b *Bot) OnMessage(handler MessageHandler)
- func (b *Bot) Run(ctx context.Context) error
- func (b *Bot) SendHTML(ctx context.Context, roomID id.RoomID, text string, html string) error
- func (b *Bot) SendReply(ctx context.Context, roomID id.RoomID, text string, html string, ...) error
- func (b *Bot) SendText(ctx context.Context, roomID id.RoomID, text string) error
- func (b *Bot) Stop() error
- type Config
- type MessageHandler
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MarkdownToHTML ¶
MarkdownToHTML converts markdown text to HTML. Useful for formatting bot responses with rich text.
Types ¶
type Bot ¶
type Bot struct {
// contains filtered or unexported fields
}
Bot is a Matrix bot that can join rooms, receive messages, and send responses.
func NewBot ¶
NewBot creates a new Matrix bot with the given configuration. Call Run() to start the bot.
func (*Bot) OnMessage ¶
func (b *Bot) OnMessage(handler MessageHandler)
OnMessage registers a handler for incoming messages. Multiple handlers can be registered and all will be called.
func (*Bot) Run ¶
Run starts the bot: connects to the homeserver, sets up encryption, and begins syncing. This blocks until Stop() is called or an error occurs.
func (*Bot) SendReply ¶
func (b *Bot) SendReply(ctx context.Context, roomID id.RoomID, text string, html string, mentionUserIDs ...id.UserID) error
SendReply sends a formatted message that mentions specific users.
type Config ¶
type Config struct {
Homeserver string // Matrix homeserver URL (e.g. https://matrix.org)
Username string // Username localpart (e.g. "mybot")
Password string // Password for authentication
Database string // SQLite database path for crypto state (default: "matrix-bot.db")
Debug bool // Enable debug logging
}
Config holds the configuration for the Matrix bot. All fields can be populated from environment variables using GetEnvironmentConfig().
func GetEnvironmentConfig ¶
func GetEnvironmentConfig() Config
GetEnvironmentConfig creates a Config from environment variables.
type MessageHandler ¶
type MessageHandler func(ctx context.Context, roomID id.RoomID, sender id.UserID, message *event.MessageEventContent)
MessageHandler is called when the bot receives a message. The handler receives the context, the room ID, the sender, and the message event.
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
ai-assistant
command
AI Assistant bot that uses Ollama to generate responses in Matrix rooms.
|
AI Assistant bot that uses Ollama to generate responses in Matrix rooms. |
|
commandbot
command
Command bot that demonstrates a multi-command handler pattern.
|
Command bot that demonstrates a multi-command handler pattern. |
|
echobot
command
Package main demonstrates a simple Matrix echo bot using the matrix library.
|
Package main demonstrates a simple Matrix echo bot using the matrix library. |