butterrobot/docs/creating-a-plugin.md
Felipe M. 7c684af8c3
All checks were successful
ci/woodpecker/tag/release Pipeline was successful
refactor: python -> go
2025-04-20 14:13:44 +02:00

1.2 KiB

Creating a Plugin

Example

This simple "Marco Polo" plugin will answer Polo to the user that says Marco:

package myplugin

import (
	"strings"

	"git.nakama.town/fmartingr/butterrobot/internal/model"
	"git.nakama.town/fmartingr/butterrobot/internal/plugin"
)

// MarcoPlugin is a simple Marco/Polo plugin
type MarcoPlugin struct {
	plugin.BasePlugin
}

// New creates a new MarcoPlugin instance
func New() *MarcoPlugin {
	return &MarcoPlugin{
		BasePlugin: plugin.BasePlugin{
			ID:   "test.marco",
			Name: "Marco/Polo",
			Help: "Responds to 'Marco' with 'Polo'",
		},
	}
}

// OnMessage handles incoming messages
func (p *MarcoPlugin) OnMessage(msg *model.Message, config map[string]interface{}) []*model.Message {
	if !strings.EqualFold(strings.TrimSpace(msg.Text), "Marco") {
		return nil
	}

	response := &model.Message{
		Text:     "Polo",
		Chat:     msg.Chat,
		ReplyTo:  msg.ID,
		Channel:  msg.Channel,
	}

	return []*model.Message{response}
}

To use the plugin, register it in your application:

// In app.go or similar initialization file
func (a *App) Run() error {
    // ...

    // Register plugins
    plugin.Register(myplugin.New())

    // ...
}