Documentation ¶
Overview ¶
Example ¶
package main import ( "net/http" "regexp" "github.com/ejholmes/slash" "golang.org/x/net/context" ) func main() { r := slash.NewMux() r.Command("/weather", "secrettoken", slash.HandlerFunc(Weather)) s := slash.NewServer(r) http.ListenAndServe(":8080", s) } // Weather is the primary slash handler for the /weather command. func Weather(ctx context.Context, r slash.Responder, command slash.Command) (slash.Response, error) { h := slash.NewMux() var zipcodeRegex = regexp.MustCompile(`(?P<zip>[0-9])`) h.MatchText(zipcodeRegex, slash.HandlerFunc(Zipcode)) return h.ServeCommand(ctx, r, command) } // Zipcode is a slash handler that returns the weather for a zip code. func Zipcode(ctx context.Context, r slash.Responder, command slash.Command) (slash.Response, error) { params := slash.Params(ctx) zip := params["zip"] return slash.Reply(zip), nil }
Output:
Index ¶
- Variables
- func Params(ctx context.Context) map[string]string
- func WithParams(ctx context.Context, params map[string]string) context.Context
- type Command
- type Handler
- type HandlerFunc
- type Matcher
- type MatcherFunc
- type Mux
- func (m *Mux) Command(command, token string, handler Handler) *Route
- func (m *Mux) Handler(command Command) (Handler, map[string]string)
- func (m *Mux) Match(matcher Matcher, handler Handler) *Route
- func (m *Mux) MatchText(re *regexp.Regexp, handler Handler) *Route
- func (m *Mux) ServeCommand(ctx context.Context, r Responder, command Command) (Response, error)
- type Responder
- type Response
- type Route
- type Server
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoHandler is returned by Mux ServeCommand if a Handler isn't found // for the route. ErrNoHandler = errors.New("slash: no handler") // ErrInvalidToken is returned when the provided token in the request // does not match the expected secret. ErrInvalidToken = errors.New("slash: invalid token") )
var NoResponse = Response{}
An empty response.
Functions ¶
Types ¶
type Command ¶
type Command struct { Token string TeamID string TeamDomain string ChannelID string ChannelName string UserID string UserName string Command string Text string ResponseURL *url.URL }
Command represents an incoming Slash Command request.
func CommandFromValues ¶
CommandFromValues returns a Command object from a url.Values object.
type Handler ¶
type Handler interface { // ServeCommand runs the command. The handler should return a Response // that will be used as the initial reply to send back to the user, or // an error. If an error is returned, then the string value is what // will be sent to the user. // // That provided Responder can be used to send asynchronous responses // after the initial response, up to 30 minutes after the command was // invoked. ServeCommand(context.Context, Responder, Command) (Response, error) }
Handler represents something that handles a slash command.
func ValidateToken ¶
ValidateToken returns a new Handler that verifies that the token in the request matches the given token.
type HandlerFunc ¶
HandlerFunc is a function that implements the Handler interface.
func (HandlerFunc) ServeCommand ¶
type Matcher ¶
Matcher is something that can check if a Command matches a Route.
func MatchCommand ¶
MatchCommand returns a Matcher that checks that the command strings match.
func MatchSubcommand ¶
MatchSubcommand returns a Matcher that checks for the first string of the text portion of a command, assuming it's a subcommand.
func MatchTextRegexp ¶
MatchTextRegexp returns a Matcher that checks that the command text matches a regular expression.
type MatcherFunc ¶
MatcherFunc is a function that implements Matcher.
type Mux ¶
type Mux struct {
// contains filtered or unexported fields
}
Mux is a Handler implementation that routes commands to Handlers.
func (*Mux) Command ¶
Handle adds a Handler to handle the given command.
Example
m.Handle("/deploy", "token", DeployHandler)
func (*Mux) Handler ¶
Handler returns the Handler that can handle the given slash command. If no handler matches, nil is returned.
type Response ¶
Response represents the response to send back to the user.