bot

package
v0.0.0-...-e9bb970 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 6, 2026 License: MIT Imports: 27 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadConfigFromBytes

func LoadConfigFromBytes(data []byte, conf *Config) error

LoadConfigFromBytes loads config from a raw []byte TOML file

func LoadConfigFromEnvVars

func LoadConfigFromEnvVars(ctx context.Context, prefix string, conf *Config) error

LoadConfigFromEnvVars reads config from env vars and maps that into the given Config struct.

func LoadConfigFromFile

func LoadConfigFromFile(filepath string, conf *Config) error

LoadConfigFromFile reads a TOML file and unmarshals that config into the given Config struct.

func LoadConfigFromFileAndEnvVars

func LoadConfigFromFileAndEnvVars(ctx context.Context, envVarsPrefix, filepath string, conf *Config) error

LoadConfigFromFileAndEnvVars reads config and maps that into the given Config in the following order: 1. Loads from Toml file. 2. Loads from env vars. Note: env var values will overwrite TOML file values.

func WakeUp

func WakeUp(_ context.Context, conf Config, bus EventBus.Bus) error

WakeUp wakes up the bot.

Types

type ChannelLimit

type ChannelLimit struct {
	RateLimitSeconds int
	MaxMessages      int
	ApplyToStaff     bool
}

ChannelLimit defines the rate limiting configuration for a specific channel.

type Config

type Config struct {
	Bot                 ConfigBot                 `env:",prefix=BOT_"`
	Staff               ConfigStaff               `env:",prefix=STAFF_"`
	Channels            ConfigChannels            `env:",prefix=CHANNELS_"`
	Links               ConfigLinks               `env:",prefix=LINKS_"`
	Twitter             ConfigTwitter             `env:",prefix=TWITTER_"`
	RateLimits          []RateLimitConfig         `toml:"rate_limits"`
	TrackingDetection   []TrackingDetectionConfig `toml:"tracking_detection"`
	TwitterContestToken string                    `env:"TWITTER_CONTEST_TOKEN"`
	TwitterContestURL   string                    `env:"TWITTER_CONTEST_URL"`
	NewRelicLicenseKey  string                    `env:"NEW_RELIC_LICENSE_KEY"`
	APIKey              string                    `env:"API_KEY"`
	Debug               bool                      `env:"DEBUG"`
	Version             string                    `env:"VERSION"`
}

type ConfigBot

type ConfigBot struct {
	ID         string          `env:"ID,required"`
	UserID     string          `env:"USER_ID,required"`
	Name       string          `env:"NAME,required"`
	UserToken  string          `env:"USER_TOKEN,required"`
	AdminToken string          `env:"ADMIN_TOKEN,required"`
	Server     ConfigBotServer `env:",prefix=SERVER_"`
}

type ConfigBotServer

type ConfigBotServer struct {
	Port          int    `env:"PORT,default=8080"`
	SigningSecret string `env:"SIGNING_SECRET,required"`
}

type ConfigChannels

type ConfigChannels struct {
	Reports    string `env:"REPORTS"`
	Playground string `env:"PLAYGROUND"`
	Jobs       string `env:"JOBS"`
	Staff      string `env:"STAFF"`
	General    string `env:"GENERAL"`
}
type ConfigLinks struct {
	COC        string `env:"COC"`
	Netiquette string `env:"NETIQUETTE"`
}

type ConfigStaff

type ConfigStaff struct {
	Members []string `env:"MEMBERS"`
}

type ConfigTwitter

type ConfigTwitter struct {
	twitter.Credentials
	APIKey       string `env:"API_KEY"`
	APIKeySecret string `env:"API_KEY_SECRET"`
}

type Context

type Context struct {
	Client              *slack.Client
	AdminClient         *slack.Client
	Config              Config
	SigningSecret       string
	Version             string
	TwitterContestToken string
	Harvester           *telemetry.Harvester
	RateLimiter         *RateLimiter
	ChannelResolver     *slackx.ChannelResolver
	TrackingDetector    *privacy.TrackingDetector

	Bus EventBus.Bus

	CLI bool // true if runs from CLI
	// contains filtered or unexported fields
}

func (*Context) IsStaff

func (c *Context) IsStaff(userID string) bool

func (*Context) VerifyRequest

func (c *Context) VerifyRequest(r *http.Request, body []byte) error

type EventHandler

type EventHandler func(Context, slackevents.EventsAPIInnerEvent) error

EventHandler handles a Slack event.

func CreateEventHandler

func CreateEventHandler(t slackevents.EventsAPIType, f EventHandler) EventHandler

CreateEventHandler creates a handler aware of errors (they are logged)

type RateLimitConfig

type RateLimitConfig struct {
	ChannelName      string `toml:"channel_name"`
	RateLimitSeconds int    `toml:"rate_limit_seconds"`
	MaxMessages      int    `toml:"max_messages"`
	ApplyToStaff     bool   `toml:"apply_to_staff"`
}

type RateLimiter

type RateLimiter struct {
	// contains filtered or unexported fields
}

RateLimiter enforces per-user, per-channel message rate limits using a sliding window algorithm. It is safe for concurrent use.

func NewRateLimiter

func NewRateLimiter(config []RateLimitConfig, getChannelID func(string) (string, error)) (*RateLimiter, error)

NewRateLimiter creates a new rate limiter with the given configuration. The getChannelID function resolves channel names to IDs. Returns an error if any channel name cannot be resolved.

func (*RateLimiter) CheckLimit

func (rl *RateLimiter) CheckLimit(channelID, userID string) (allowed bool, nextAllowedTime time.Time)

CheckLimit checks if a user is allowed to post a message in a channel. Returns (true, zero time) if allowed, or (false, nextAllowedTime) if rate limited.

func (*RateLimiter) ShouldCheckStaff

func (rl *RateLimiter) ShouldCheckStaff(channelID string) bool

ShouldCheckStaff returns true if rate limits should apply to staff members in the given channel.

type SlackContext

type SlackContext struct {
	User            string
	Channel         string
	Text            string
	Timestamp       string
	ThreadTimestamp string
}

type TrackingDetectionConfig

type TrackingDetectionConfig struct {
	ChannelName string `toml:"channel_name"`
}

type UserRateState

type UserRateState struct {
	Messages  []time.Time
	NextReset time.Time
}

UserRateState tracks a user's message history in a specific channel.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL