Ejemplo n.º 1
0
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
}
Ejemplo n.º 2
0
func Example() {
	r := slash.NewMux()
	r.Command("/weather", "secrettoken", slash.HandlerFunc(Weather))

	s := slash.NewServer(r)
	http.ListenAndServe(":8080", s)
}
Ejemplo n.º 3
0
// 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)
}
Ejemplo n.º 4
0
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
	})
}
Ejemplo n.º 5
0
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")
	}
}
Ejemplo n.º 6
0
// 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
	})
}
Ejemplo n.º 7
0
// 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))
	})
}
Ejemplo n.º 8
0
func main() {
	h := slash.HandlerFunc(Handle)
	s := slash.NewServer(h)
	http.ListenAndServe(":8080", s)
}