Install
Create a Go module and add Osmose:
go mod init example.com/my-bot
go get github.com/ofabiodev/osmose
Set the bot token in the environment:
export OSMIUM_TOKEN="your-token"
On PowerShell:
$env:OSMIUM_TOKEN = "your-token"
A ping bot
package main
import (
"context"
"log"
"os"
"os/signal"
"github.com/ofabiodev/osmose"
)
func main() {
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt)
defer stop()
client, err := osmose.New(osmose.Config{
Token: os.Getenv("OSMIUM_TOKEN"),
ClientID: 123456,
})
if err != nil {
log.Fatal(err)
}
client.OnReady(func(_ context.Context, event *osmose.ReadyEvent) error {
log.Printf("connected as %s", event.User.Username)
return nil
})
client.OnMessageCreate(func(ctx context.Context, event *osmose.MessageCreateEvent) error {
if event.Message.Content != "!ping" {
return nil
}
return event.Reply(ctx, "Pong! 🏓")
})
if err := client.Run(ctx); err != nil {
log.Fatal(err)
}
}
Run handles connect, initialize, authorize, event dispatch, keepalive,
reconnect, and shutdown.
Next steps
- Read Events to handle messages and interactions.
- Use Services to send messages and query Osmium.
- Use Protocol and raw API when a service does not cover an endpoint yet.