Documentation
¶
Overview ¶
Package botbooter is a small framework for building chat bots that behave the same way across Slack, Discord, Telegram, WhatsApp and a local CLI. It is a thin facade over the internal packages, so consumers keep a single import path.
Example ¶
package main
import (
"context"
"os"
"strings"
"github.com/lao/botbooter"
)
func main() {
bot := botbooter.InitAsCLIBot(strings.NewReader("echo hello\n"), os.Stdout)
_ = bot.HandleFunc("^echo ", func(ctx context.Context, b *botbooter.Bot, m *botbooter.Message) {
_ = b.SendMessageContext(ctx, m.ChannelID, strings.TrimPrefix(m.Content, "echo "))
})
// Run returns when the input reaches EOF.
_ = bot.Run(context.Background())
}
Output: hello
Index ¶
- Constants
- Variables
- func DiscordRawEvent(m *Message) (*discordgo.MessageCreate, bool)
- func DiscordSession(b *Bot) *discordgo.Session
- func SlackClient(b *Bot) *slackapi.Client
- func SlackRawEvent(m *Message) (*slackevents.MessageEvent, bool)
- func SlackSocketClient(b *Bot) *socketmode.Client
- func TelegramClient(b *Bot) *bot.Bot
- func TelegramRawEvent(m *Message) (*models.Update, bool)
- type Attachment
- type Bot
- type BotType
- type CLIMessage
- type Command
- type CommandHandler
- type Message
- type Middleware
- type WhatsAppConfig
- type WhatsAppMedia
- type WhatsAppMessage
Examples ¶
Constants ¶
const ( SlackBotType = core.SlackBotType DiscordBotType = core.DiscordBotType CLIBotType = core.CLIBotType TelegramBotType = core.TelegramBotType WhatsAppBotType = core.WhatsAppBotType )
Supported bot types.
const TelegramEnvSuppressURLWarning = telegram.EnvSuppressURLWarning
TelegramEnvSuppressURLWarning names the environment variable that silences the plaintext-token warning logged on every successful Telegram resolve via [Bot.ResolveAttachmentURL]. Set it to any non-empty value to opt out.
Variables ¶
var ( ErrUnknownBotType = core.ErrUnknownBotType ErrAlreadyConnected = core.ErrAlreadyConnected // ErrMissingWhatsAppConfig is returned by InitAsWhatsAppBot when a required // WhatsAppConfig field is empty. ErrMissingWhatsAppConfig = whatsapp.ErrMissingConfig )
Errors returned by Bot methods and platform helpers.
Functions ¶
func DiscordRawEvent ¶ added in v0.2.0
func DiscordRawEvent(m *Message) (*discordgo.MessageCreate, bool)
DiscordRawEvent returns the raw Discord event carried on m, reporting whether m originated from Discord.
func DiscordSession ¶ added in v0.2.0
DiscordSession returns the discordgo session backing b, or nil if b is not a Discord bot.
func SlackClient ¶ added in v0.2.0
SlackClient returns the Slack Web API client backing b, or nil if b is not a Slack bot.
func SlackRawEvent ¶ added in v0.2.0
func SlackRawEvent(m *Message) (*slackevents.MessageEvent, bool)
SlackRawEvent returns the raw Slack event carried on m, reporting whether m originated from Slack.
func SlackSocketClient ¶ added in v0.2.0
func SlackSocketClient(b *Bot) *socketmode.Client
SlackSocketClient returns the Socket Mode client backing b, or nil if b is not a Slack bot.
func TelegramClient ¶ added in v0.2.0
TelegramClient returns the go-telegram bot client backing b, or nil if b is not a Telegram bot.
Types ¶
type Attachment ¶
type Attachment = core.Attachment
Attachment is a platform-agnostic file attachment. See core.Attachment.
type Bot ¶
Bot is the platform-agnostic chat bot. See core.Bot.
func InitAsCLIBot ¶
InitAsCLIBot creates a local CLI bot.
func InitAsDiscordBot ¶
InitAsDiscordBot creates a Discord bot that connects via the Gateway.
func InitAsSlackBot ¶
InitAsSlackBot creates a Slack bot that connects via Socket Mode.
func InitAsTelegramBot ¶ added in v0.2.0
InitAsTelegramBot creates a Telegram bot that connects via the Bot API.
func InitAsWhatsAppBot ¶ added in v0.2.0
func InitAsWhatsAppBot(cfg WhatsAppConfig) (*Bot, error)
InitAsWhatsAppBot creates a WhatsApp bot backed by the Meta Cloud API. It runs an inbound webhook HTTP server at cfg.Addr, so put a TLS-terminating proxy in front and register the public HTTPS URL in Meta's webhook settings. Inbound media arrives as an id in Attachment.ExtraData (not a URL); resolve the bytes with GET /{media-id} using your access token. It returns an error if a required config field is missing.
type CLIMessage ¶
type CLIMessage = core.CLIMessage
CLIMessage is the raw payload of a CLI message. See core.CLIMessage.
func CLIRawEvent ¶ added in v0.2.0
func CLIRawEvent(m *Message) (*CLIMessage, bool)
CLIRawEvent returns the parsed CLI line carried on m, reporting whether m originated from the CLI adapter.
type Command ¶
Command pairs a regexp pattern with a handler. See core.Command.
type CommandHandler ¶
type CommandHandler = core.CommandHandler
CommandHandler handles a matched message. See core.CommandHandler.
type Message ¶
Message is an incoming message handed to handlers. See core.Message.
type Middleware ¶
type Middleware = core.Middleware
Middleware wraps message dispatch. See core.Middleware.
type WhatsAppConfig ¶ added in v0.2.0
WhatsAppConfig configures a WhatsApp Cloud API bot. See whatsapp.Config.
type WhatsAppMedia ¶ added in v0.2.0
WhatsAppMedia identifies media attached to a WhatsApp message. See whatsapp.Media.
type WhatsAppMessage ¶ added in v0.2.0
WhatsAppMessage is the parsed payload of a WhatsApp message. See whatsapp.Message.
func WhatsAppRawEvent ¶ added in v0.2.0
func WhatsAppRawEvent(m *Message) (*WhatsAppMessage, bool)
WhatsAppRawEvent returns the parsed WhatsApp message carried on m, reporting whether m originated from WhatsApp. AuthorName and Timestamp on the returned value are enriched, not present in its Raw JSON.
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
v1
command
Command v1 is a small demo of botbooter.
|
Command v1 is a small demo of botbooter. |
|
internal
|
|
|
asserts
Package asserts holds tiny test assertion helpers shared across botbooter.
|
Package asserts holds tiny test assertion helpers shared across botbooter. |
|
cli
Package cli is the local CLI adapter for botbooter.
|
Package cli is the local CLI adapter for botbooter. |
|
core
Package core holds botbooter's platform-agnostic engine: the Bot type, its command/middleware dispatch, and the connection lifecycle.
|
Package core holds botbooter's platform-agnostic engine: the Bot type, its command/middleware dispatch, and the connection lifecycle. |
|
discord
Package discord is the Discord adapter for botbooter.
|
Package discord is the Discord adapter for botbooter. |
|
slack
Package slack is the Slack adapter for botbooter, connecting via Socket Mode.
|
Package slack is the Slack adapter for botbooter, connecting via Socket Mode. |
|
telegram
Package telegram is the Telegram adapter for botbooter: it connects via the Bot API getUpdates long-poll loop and implements core.Adapter.
|
Package telegram is the Telegram adapter for botbooter: it connects via the Bot API getUpdates long-poll loop and implements core.Adapter. |
|
whatsapp
Package whatsapp is the WhatsApp adapter for botbooter.
|
Package whatsapp is the WhatsApp adapter for botbooter. |