Documentation ¶
Overview ¶
Package slash implements a simple router for Slack's slash commands.
Example ¶
package main import ( "context" "fmt" "math/rand" "net/http" "os" "strconv" "htdvisser.dev/slash" ) func main() { // This is an extremely simple response to a slash command. // You'll want to add "blocks" for more advanced responses. // See also https://api.slack.com/messaging/composing/layouts. type response struct { ResponseType string `json:"response_type"` Text string `json:"text"` } // We set up the slash command router with the Signing secret we got from Slack. r := slash.NewRouter(os.Getenv("SLACK_SIGNING_SECRET")) // We register the /pseudorandom command that generates a pseudorandom number for the user. r.RegisterCommand("/pseudorandom", func(ctx context.Context, req slash.Request) interface{} { n := 10 if req.Text() != "" { var err error n, err = strconv.Atoi(req.Text()) if err != nil { // We send a response to the user to inform them about errors. return response{ ResponseType: "ephemeral", Text: "Please give me a number", } } } if n <= 1 { return response{ ResponseType: "ephemeral", Text: "Please give me a number _larger than one_", } } return response{ ResponseType: "in_channel", Text: fmt.Sprintf("%s got %d", req.UserName(), rand.Intn(n)), } }) http.Handle("/slash", r) http.ListenAndServe(":8080", nil) }
Output:
Index ¶
- type HandlerFunc
- type Logger
- type MiddlewareFunc
- type Request
- func (r Request) ChannelID() string
- func (r Request) ChannelName() string
- func (r Request) Command() string
- func (r Request) EnterpriseID() string
- func (r Request) EnterpriseName() string
- func (r Request) ResponseURL() string
- func (r Request) TeamDomain() string
- func (r Request) TeamID() string
- func (r Request) Text() string
- func (r Request) Token() string
- func (r Request) TriggerID() string
- func (r Request) UserID() string
- func (r Request) UserName() string
- type Router
- type RouterOption
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type HandlerFunc ¶
HandlerFunc is a function that handles a slash command.
type Logger ¶
type Logger interface { Print(v ...interface{}) Printf(format string, v ...interface{}) }
Logger is the logger interface used by the slash command Router.
type MiddlewareFunc ¶
type MiddlewareFunc func(next HandlerFunc) HandlerFunc
MiddlewareFunc is a function that can be used as middleware in slash command handling.
type Request ¶
Request is the request message for a slash command.
func (Request) ChannelName ¶
ChannelName returns the `channel_name` field from the slash request.
func (Request) EnterpriseID ¶
EnterpriseID returns the `enterprise_id` field from the slash request.
func (Request) EnterpriseName ¶
EnterpriseName returns the `enterprise_name` field from the slash request.
func (Request) ResponseURL ¶
ResponseURL returns the `response_url` field from the slash request.
func (Request) TeamDomain ¶
TeamDomain returns the `team_domain` field from the slash request.
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router routes slash commands to their handlers.
func NewRouter ¶
func NewRouter(signingSecret string, opts ...RouterOption) *Router
NewRouter creates a new router for slash commands.
func (*Router) HandleCommand ¶
func (sr *Router) HandleCommand(ctx context.Context, header http.Header, body []byte) (s int, h http.Header, b []byte, err error)
HandleCommand reads the command from the request header+body and returns a response.
func (*Router) RegisterCommand ¶
func (sr *Router) RegisterCommand(command string, handler HandlerFunc)
RegisterCommand registers a slash command and its handler to the Router.
type RouterOption ¶
type RouterOption interface {
// contains filtered or unexported methods
}
RouterOption sets options on the Router.
func WithCommandFailedHandler ¶
func WithCommandFailedHandler(f HandlerFunc) RouterOption
WithCommandFailedHandler returns a RouterOption that sets the handler for failed commands.
func WithCommandUnknownHandler ¶
func WithCommandUnknownHandler(f HandlerFunc) RouterOption
WithCommandUnknownHandler returns a RouterOption that sets the handler for unknown commands.
func WithLogger ¶
func WithLogger(logger Logger) RouterOption
WithLogger returns a RouterOption that sets the logger.
func WithMiddleware ¶
func WithMiddleware(f MiddlewareFunc) RouterOption
WithMiddleware returns a RouterOption that sets the middleware that will be called with the matched command handler.
func WithoutLogger ¶
func WithoutLogger() RouterOption
WithoutLogger returns a RouterOption that disables logging.