eventrouter

package module
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 14, 2021 License: Apache-2.0 Imports: 15 Imported by: 0

README

go-slack-event-router

ci status Go Reference

This is not a chatbot framework, but rather a simple event dispatcher that enhances the functionality of slack-go/slack.

Example

To handle Events API:

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 ExampleRouter() {
	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
}

To handle user interaction:

import (
	"context"
	"net/http"
	"os"

	"github.com/genkami/go-slack-event-router/interactionrouter"
	"github.com/slack-go/slack"
)

func ExampleRouter() {
	signingSecret := os.Getenv("SLACK_SIGNING_SECRET")
	r, _ := interactionrouter.New(interactionrouter.WithSigningSecret(signingSecret)) // omitted error handling

	// Call handlePostNiceGif whenever the router receives `block_actions` event with a block `post_nice_gif` with an action `gif_keyword`.
	r.On(slack.InteractionTypeBlockActions, interactionrouter.HandlerFunc(handlePostNiceGif),
		interactionrouter.BlockAction("post_nice_gif", "gif_keyword"))

	http.Handle("/slack/actions", r)

	// ...
}

func handlePostNiceGif(ctx context.Context, callback *slack.InteractionCallback) error {
	// Do whatever you want...
	return nil
}

License

Distributed under the Apache License Version 2.0. See LICENSE for more information.

Documentation

Overview

Package eventrouter provides a way to dispatch events from Slack.

Index

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

func WithSigningSecret(token string) Option

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

func New(options ...Option) (*Router, error)

New creates a new Router.

At least one of WithSigningSecret() or InsecureSkipVerification() must be specified.

func (*Router) On

func (r *Router) On(eventType string, h Handler)

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

func (r *Router) OnMessage(h message.Handler, preds ...message.Predicate)

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

func (r *Router) SetFallback(h Handler)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL