Giter VIP home page Giter VIP logo

go-slack-event-router's Introduction

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.

go-slack-event-router's People

Contributors

genkami avatar renovate-bot avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.