func newSlack(c client) *Slack { r := slash.NewMux() s := &Slack{client: c, mux: r} r.Match(slash.MatchSubcommand(`help`), Help) r.MatchText( regexp.MustCompile(`enable (?P<owner>\S+?)/(?P<repo>\S+)`), slash.HandlerFunc(s.Enable), ) r.MatchText( regexp.MustCompile(`build (?P<owner>\S+?)/(?P<repo>\S+)@(?P<branch>\S+)`), slash.HandlerFunc(s.Build), ) return s }
func Example() { 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, command slash.Command) (string, error) { h := slash.NewMux() var zipcodeRegex = regexp.MustCompile(`(?P<zip>[0-9])`) h.MatchText(zipcodeRegex, slash.HandlerFunc(Zipcode)) return h.ServeCommand(ctx, command) }
func printErrors(h slash.Handler) slash.Handler { return slash.HandlerFunc(func(ctx context.Context, r slash.Responder, command slash.Command) error { if err := h.ServeCommand(ctx, r, command); err != nil { fmt.Printf("error: %v\n", err) } return nil }) }
func ExampleServer() { // A slash.Handler that will handle our slash commands. h := slash.NewServer(slash.HandlerFunc(func(ctx context.Context, r slash.Responder, c slash.Command) error { return r.Respond(slash.Reply("Hey")) })) // Responses from the above handler will be posted here. responses := slashtest.NewServer() defer responses.Close() req, _ := slashtest.NewRequest("POST", "/", responses.NewCommand()) resp := httptest.NewRecorder() h.ServeHTTP(resp, req) select { case resp := <-responses.Responses: fmt.Println(resp.Text) // Output: Hey case <-time.After(time.Second): panic("timeout") } }
// replyHandler returns a slash.Handler that just replies to the user with the // text. func replyHandler(text string) slash.Handler { return slash.HandlerFunc(func(ctx context.Context, r slash.Responder, c slash.Command) (slash.Response, error) { return slash.Reply(text), nil }) }
// replyHandler returns a slash.Handler that just replies to the user with the // text. func replyHandler(text string) slash.Handler { return slash.HandlerFunc(func(ctx context.Context, r slash.Responder, c slash.Command) error { return r.Respond(slash.Reply(text)) }) }
func main() { h := slash.HandlerFunc(Handle) s := slash.NewServer(h) http.ListenAndServe(":8080", s) }