Documentation ¶
Overview ¶
Package eventrouter provides a way to dispatch events from Slack.
Index ¶
- type Handler
- type HandlerFunc
- type Option
- type Router
- func (r *Router) On(eventType string, h Handler)
- func (r *Router) OnAppMention(h appmention.Handler, preds ...appmention.Predicate)
- func (r *Router) OnMessage(h message.Handler, preds ...message.Predicate)
- func (r *Router) OnReactionAdded(h reaction.AddedHandler, preds ...reaction.Predicate)
- func (r *Router) OnReactionRemoved(h reaction.RemovedHandler, preds ...reaction.Predicate)
- func (router *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
- func (r *Router) SetAppRateLimitedHandler(h appratelimited.Handler)
- func (r *Router) SetFallback(h Handler)
- func (r *Router) SetURLVerificationHandler(h urlverification.Handler)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Handler ¶
type Handler interface {
HandleEventsAPIEvent(context.Context, *slackevents.EventsAPIEvent) error
}
Handler is a handler that processes events from Slack. Usually you don't need to use this directly. Instead, you might want to use event-specific handler types like `appmention.Handler`.
Handlers may return `routererrors.NotInterested` (or its equivalents in the sense of `errors.Is`). In such case the Router falls back to other handlers.
Handlers also may return `routererrors.HttpError` (or its equivalents in the sense of `errors.Is`). In such case the Router responds with corresponding HTTP status codes.
If any other errors are returned, the Router responds with Internal Server Error.
type HandlerFunc ¶
type HandlerFunc func(context.Context, *slackevents.EventsAPIEvent) error
func (HandlerFunc) HandleEventsAPIEvent ¶
func (f HandlerFunc) HandleEventsAPIEvent(ctx context.Context, e *slackevents.EventsAPIEvent) error
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option configures the Router.
func InsecureSkipVerification ¶
func InsecureSkipVerification() Option
InsecureSkipVerification skips verifying request signatures. This is useful to test your handlers, but do not use this in production environments.
func VerboseResponse ¶
func VerboseResponse() Option
If VerboseResponse is set, the Router shows error details when it fails to process requests.
func WithSigningSecret ¶
WithSigningSecret sets a signing token to verify requests from Slack.
For more details, see https://api.slack.com/authentication/verifying-requests-from-slack.
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router is an http.Handler that processes events from Slack via Events API.
For more details, see https://api.slack.com/apis/connections/events-api.
Example ¶
package main import ( "context" "net/http" "os" "regexp" eventrouter "github.com/genkami/go-slack-event-router" "github.com/genkami/go-slack-event-router/message" "github.com/genkami/go-slack-event-router/reaction" "github.com/slack-go/slack/slackevents" ) func main() { signingSecret := os.Getenv("SLACK_SIGNING_SECRET") r, _ := eventrouter.New(eventrouter.WithSigningSecret(signingSecret)) // omitted error handling // Call handleDeploy whenever the router receives `message` events and the text of the message matches to /deploy/. r.OnMessage(message.HandlerFunc(handleDeploy), message.TextRegexp(regexp.MustCompile(`deploy`))) // Call handleIssue whenever the router receives `reaction_added` events with reaction `:issue:` and the event happens in the channel ABCXYZ. r.OnReactionAdded(reaction.AddedHandlerFunc(handleIssue), reaction.Name("issue"), reaction.Channel("ABCXYZ")) http.Handle("/slack/events", r) // ... } func handleDeploy(ctx context.Context, e *slackevents.MessageEvent) error { // Do whatever you want... return nil } func handleIssue(ctx context.Context, e *slackevents.ReactionAddedEvent) error { // Do whatever you want... return nil }
Output:
func New ¶
New creates a new Router.
At least one of WithSigningSecret() or InsecureSkipVerification() must be specified.
func (*Router) On ¶
On registers a handler for a specific event type.
If more than one handlers are registered, the first ones take precedence.
Handlers may return `routererrors.NotInterested` (or its equivalents in the sense of `errors.Is`). In such case the Router falls back to other handlers.
Handlers also may return `routererrors.HttpError` (or its equivalents in the sense of `errors.Is`). In such case the Router responds with corresponding HTTP status codes.
If any other errors are returned, the Router responds with Internal Server Error.
This can be useful if you have a general-purpose event handlers that can process arbitrary types of events, but, in the most cases it would be better option to use event-specfic `OnEVENT_NAME` methods instead.
func (*Router) OnAppMention ¶
func (r *Router) OnAppMention(h appmention.Handler, preds ...appmention.Predicate)
OnAppMention registers a handler that processes `app_mention` events.
If more than one handlers are registered, the first ones take precedence.
Predicates are used to distinguish whether a coming event should be processed by the given handler or not. The handler `h` will be called only when all of given Predicates are true.
func (*Router) OnMessage ¶
OnMessage registers a handler that processes `message` events.
If more than one handlers are registered, the first ones take precedence.
Predicates are used to distinguish whether a coming event should be processed by the given handler or not. The handler `h` will be called only when all of given Predicates are true.
func (*Router) OnReactionAdded ¶
func (r *Router) OnReactionAdded(h reaction.AddedHandler, preds ...reaction.Predicate)
OnReactionAdded registers a handler that processes `reaction_added` events.
If more than one handlers are registered, the first ones take precedence.
Predicates are used to distinguish whether a coming event should be processed by the given handler or not. The handler `h` will be called only when all of given Predicates are true.
func (*Router) OnReactionRemoved ¶
func (r *Router) OnReactionRemoved(h reaction.RemovedHandler, preds ...reaction.Predicate)
OnReactionRemoved registers a handler that processes `reaction_removed` events.
If more than one handlers are registered, the first ones take precedence.
Predicates are used to distinguish whether a coming event should be processed by the given handler or not. The handler `h` will be called only when all of given Predicates are true.
func (*Router) ServeHTTP ¶
func (router *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
func (*Router) SetAppRateLimitedHandler ¶
func (r *Router) SetAppRateLimitedHandler(h appratelimited.Handler)
SetAppRateLimitedHandler sets a handler to process `app_rate_limited` events.
If more than one handlers are registered, the last one will be used.
If no handler is set explicitly, the Rotuer uses the default handler that simply ignores events of this type.
For more details see https://api.slack.com/docs/rate-limits#rate-limits__events-api.
func (*Router) SetFallback ¶
SetFallback sets a fallback handler that is called when none of the registered handlers matches to a coming event.
If more than one handlers are registered, the last one will be used.
func (*Router) SetURLVerificationHandler ¶
func (r *Router) SetURLVerificationHandler(h urlverification.Handler)
SetURLVerificationHandler sets a handler to process `url_verification` events.
If more than one handlers are registered, the last one will be used.
If no handler is set explicitly, the Rotuer uses the default handler.
For more details see https://api.slack.com/events/url_verification.
Directories ¶
Path | Synopsis |
---|---|
Package appmention provides handlers to process `app_mention` events.
|
Package appmention provides handlers to process `app_mention` events. |
package appratelimited provides handler to process `app_rate_limited` events.
|
package appratelimited provides handler to process `app_rate_limited` events. |
Package errors provides error values and types that are intended to be used to implement handlers.
|
Package errors provides error values and types that are intended to be used to implement handlers. |
Package interactionrouter provides a way to dispatch interactive callbacks sent from Slack.
|
Package interactionrouter provides a way to dispatch interactive callbacks sent from Slack. |
internal
|
|
Package message provides handlers to process `message` events.
|
Package message provides handlers to process `message` events. |
Package reaction provides handlers to process `reaction_*` events.
|
Package reaction provides handlers to process `reaction_*` events. |
Package signature provides helpers to validate request signature.
|
Package signature provides helpers to validate request signature. |
Package urlverification provides handlers to process `url_verification` events.
|
Package urlverification provides handlers to process `url_verification` events. |