62 lines
1.2 KiB
Markdown
62 lines
1.2 KiB
Markdown
# Creating a Plugin
|
|
|
|
## Example
|
|
|
|
This simple "Marco Polo" plugin will answer _Polo_ to the user that says _Marco_:
|
|
|
|
```go
|
|
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:
|
|
|
|
```go
|
|
// In app.go or similar initialization file
|
|
func (a *App) Run() error {
|
|
// ...
|
|
|
|
// Register plugins
|
|
plugin.Register(myplugin.New())
|
|
|
|
// ...
|
|
}
|
|
```
|