Documentation
¶
Overview ¶
Package bot is togo's chat-bot subsystem: a Bot driver contract plus a command/message handler registry so any plugin can add bot commands that work across platforms. Real platforms (Telegram, Discord, Slack) ship as driver plugins that call bot.RegisterDriver and depend on this package. Select one with BOT_DRIVER.
Install: `togo install togo-framework/bot` (blank-import registers it), then a driver, e.g. `togo install togo-framework/bot-telegram`.
import _ "github.com/togo-framework/bot"
import _ "github.com/togo-framework/bot-telegram"
bot.OnCommand("ping", func(ctx context.Context, b *bot.Service, m bot.Message) (string, error) {
return "pong 🏓", nil
})
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func OnCommand ¶
OnCommand registers a handler for "/name". A leading slash is optional. The handler runs when a message starts with "/name". Later registrations for the same name win.
func OnMessage ¶
func OnMessage(h Handler)
OnMessage registers a handler invoked for every non-command message.
func ParseCommand ¶
ParseCommand splits "/cmd a b c" into ("cmd", ["a","b","c"], true). It returns ok=false when text is not a command.
func RegisterDriver ¶
func RegisterDriver(name string, f DriverFactory)
RegisterDriver registers a bot platform driver by name (call from a plugin's init()).
Types ¶
type Bot ¶
type Bot interface {
// Start begins receiving messages and blocks until ctx is canceled or Stop
// is called. Drivers invoke the dispatch func they were given for each message.
Start(ctx context.Context) error
// Stop shuts the bot down and releases resources.
Stop() error
// Send posts a message to a channel/chat.
Send(ctx context.Context, channel, msg string) error
}
Bot is implemented by platform driver plugins (telegram/discord/slack).
type DriverFactory ¶
DriverFactory builds a Bot from the kernel (env-configured). dispatch must be called by the driver for every inbound message; it routes to the registered command/message handlers and sends any reply.
type Handler ¶
Handler handles a command or message. Return a non-empty string to reply, or an error (the error is logged and no reply is sent).
type Message ¶
type Message struct {
Channel string // platform channel/chat ID — reply here
User string // sender's platform user ID
Username string // sender's display name / handle (best-effort)
Text string // raw message text
IsCommand bool // true when Text began with "/"
Command string // command name without the leading "/" (e.g. "ping")
Args []string // whitespace-split arguments after the command
Platform string // driver name: telegram | discord | slack
Raw map[string]any // platform-specific payload for advanced handlers
}
Message is an inbound chat message normalized across platforms.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service is the bot runtime stored on the kernel (k.Get("bot")).
func FromKernel ¶
FromKernel returns the bot Service if a driver was registered.
func (*Service) Dispatch ¶
Dispatch routes m to the matching command handler (if any) or to the message handlers, and sends back any non-empty reply. Drivers call this for each inbound message.