func Example() { r := slash.NewMux() r.Command("/weather", "secrettoken", slash.HandlerFunc(Weather)) s := slash.NewServer(r) http.ListenAndServe(":8080", s) }
// newSlackServer returns an http handler for handling Slack slash commands at <url>/slack. func newSlackServer(q conveyor.BuildQueue, c *cli.Context) http.Handler { ts := oauth2.StaticTokenSource( &oauth2.Token{AccessToken: c.String("github.token")}, ) tc := oauth2.NewClient(oauth2.NoContext, ts) cy := github.NewClient(tc) r := slash.NewMux() r.Match(slash.MatchSubcommand(`help`), slack.Help) r.MatchText( regexp.MustCompile(`enable (?P<owner>\S+?)/(?P<repo>\S+)`), slack.NewEnable( cy, slack.NewHook(c.String("url"), c.String("github.secret")), ), ) r.MatchText( regexp.MustCompile(`build (?P<owner>\S+?)/(?P<repo>\S+)@(?P<branch>\S+)`), slack.NewBuild( cy, q, fmt.Sprintf(logsURLTemplate, c.String("url")), ), ) return slash.NewServer(slash.ValidateToken(r, c.String("slack.token"))) }
// newSlackServer returns an http handler for handling Slack slash commands at <url>/slack. func newSlackServer(c *cli.Context) http.Handler { ts := oauth2.StaticTokenSource( &oauth2.Token{AccessToken: c.String("github.token")}, ) tc := oauth2.NewClient(oauth2.NoContext, ts) client := github.NewClient(tc) r := slash.NewMux() r.MatchText( regexp.MustCompile(`setup (?P<owner>\S+?)/(?P<repo>\S+)`), slack.NewWebhookHandler( client, slack.NewHook(c.String("url"), c.String("github.secret")), ), ) return slash.NewServer(slash.ValidateToken(r, c.String("slack.token"))) }
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") } }
// newSlackServer returns an http handler for handling Slack slash commands at <url>/slack. func newSlackServer(cy *conveyor.Conveyor, c *cli.Context) http.Handler { s := slack.New(cy) s.URLTemplate = template.Must(template.New("url").Parse(fmt.Sprintf(logsURLTemplate, c.String("url")))) return slash.NewServer(slash.ValidateToken(s, c.String("slack.token"))) }
func main() { h := slash.HandlerFunc(Handle) s := slash.NewServer(h) http.ListenAndServe(":8080", s) }