relax

command module
v0.0.0-...-650939c Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2017 License: MPL-2.0 Imports: 5 Imported by: 0

README

Relax

Relax is a Message Broker for Slack Bots. What does that mean? If you are running a "bot-as-a-service" for Slack, you have to maintain hundreds (if not thousands) of websocket connections and handle the deluge of events from all these connections.

Relax does all that heavy lifting for you and provides you with a single stream of events that your web app can then take action on. The protocol is JSON based and so any web app can communicate with Relax.

Travis Badge for Relax Gitter

If you are a Rails app however, there is a nifty Ruby client for you to use.

You can also download pre-built binaries here.

If you are a Heroku user, you can deploy Relax right away with one click.

Deploy

Known Issue: When deploying using the "Deploy with Heroku" button, Heroku's Free Redis server takes a while to boot up, so your Relax deployment will take a while to come up as well. You can see Relax trying to connect to Heroku's Redis server by tailing logs heroku logs --tail -a <your-heroku-app-name>. After a while, it should connect.

Coming Soon

  • Pluggable Messaging Backends

Installation

Although Relax is written in Go, it does not require any knowledge of Go. You can download the latest stable version of relax here.

Binaries are available for both OS X and Linux. Untar and gunzip the downloaded file and move the relax binary to your $PATH to start using.

Analytics for your Slackbot

Relax now has optional Botmetrics analytics built in. If you want analytics for your Slack bot, set the BOTMETRICS_ENABLED environment variable to 'true', get an API key and a bot ID from botmetrics and set the BOTMETRICS_API_KEY and BOTMETRICS_BOT_ID environment variables set up and restart your Relax instances.

In Production Use

Relax is used in production to power Nestor.

Running Relax

To run it, basically run relax (assuming it is in your $PATH).

RELAX_BOTS_KEY=relax_bots_key RELAX_BOTS_PUBSUB=relax_bots_pubsub RELAX_EVENTS_QUEUE=relax_events_queue REDIS_HOST=localhost:6379 relax

Setup

The Relax message broker requires a few environment variables to be set up (these same environment variables are also used to set up the Relax Ruby Client). These environment variables are basically Redis keys that can be configured based on your specific needs.

RELAX_BOTS_KEY: This can be any string value and is used to store state about all Slack clients currently controlled by Relax in Redis.

RELAX_BOTS_PUBSUB: This can be any string value and is used by Relax clients to notify Relax brokers that a new Slack bot has been started.

RELAX_EVENTS_QUEUE: This can be any string value and is used by Relax brokers to send events to the client.

RELAX_MUTEX_KEY: This can be any string value and is used by Relax brokers to decide whether to send events back to clients.

Protocol

You interact with Relax by sending messages to Relax via Redis, there are two primary ways of interacting with Relax:

Starting Bots

To start a bot, you need to HSET on RELAX_BOTS_KEY with a JSON blob containing "team_id" and "token" keys which represent the Team UID and Token for the bot you want to start. Along with this, you should also PUBLISH on RELAX_BOTS_PUBSUB with a JSON blob containing the keys "type" and "team_id" containing the values "team_added" and the Team UID of the bot you want to start.

For e.g., for a bot who's Slack Team UID is "TDEADBEEF" and who's token is "xoxo_slackbotoken", you can issue the following commands using redis-cli to start a bot (assuming that $RELAX_BOTS_KEY is relax_bots_key):

$ redis-cli
127.0.0.1:6379> MULTI
OK
127.0.0.1:6379> HSET relax_bots_key TDEADBEEF '{"team_id":"TDEADBEEF","token":"xoxo_slackbotoken"}'
(integer) 1
127.0.0.1:6379> HGETALL relax_bots_key
1) "TDEADBEEF"
2) "{\"team_id\":\"TDEADBEEF\",\"token\":\"xoxo_slackbotoken\"}"
127.0.0.1:6379> PUBLISH relax_bots_pubsub '{"type":"team_added","team_id":"TDEADBEEF"}'
QUEUED
127.0.0.1:6379> EXEC

Listening for Events

Relax also generates events (details of events are described in the "Events" section of the README)

Events are queued in the $RELAX_EVENTS_QUEUE key in Redis and so to consume events, you need to LPOP or BLPOP the $RELAX_EVENTS_QUEUE to deal with events.

Events

Slack Events are gathered from all teams that Slack is listening to and are multiplexed onto a single Redis queue. The event data structure consists of the following fields:

type

This is a string value contains the type of event can hold the following values:

Type What it does
disable_bot This event is sent when authentication with a team fails (either due to a wrong token or an expired token). message_new
message_edited This event is sent when a message has been edited.
message_deleted This event is sent when a message has been deleted.
reaction_added This event is sent when a reaction has been added to a message.
reaction_removed This event is sent when a reaction has been removed from a message.
team_joined This event is sent when a new member has been added to the team. The best practice upon receiving this event is to refresh the team database and make sure that information on all members of the team is up to date.
im_created This event is sent when a new direct message has been opened with the bot. This can be ignored in most cases as it is used by Relax to keep internal metadata in sync.

user_uid

This is a string value and is the UID (generated by Slack) of the user associated with the event. So in the case of message_new it's the UID of the user who created the message, for reaction_added, it's the UID of the user who added a reaction to a message.

channel_uid

This is the UID (generated by Slack) of the channel associated with the event.

im

This is a boolean value to indicate whether the channel (with the UID channel_uid) is an IM or not.

text

This is a string value and contains the text associated with the event. This can mean different things in different contexts:

Type What "text" field means
message_new The message text
message_edited The new text of the message
message_deleted The original message text
reaction_added Reaction added to a message. This contains the text representation of a reaction, for e.g. :simple_smiley:
reaction_removed Reaction removed from a message. This contain the text representation of a reaction, for e.g. :simple_smiley:
team_joined Since there is no text metadata associated with this event, it is always blank.
im_created Since there is no text metadata associated with this event, it is always blank.

relax_bot_uid

This is a string value and represent the UID of the bot that Relax controls. This is useful if you want to strip out the @-mention word in a message @-mention'ed to the bot.

timestamp

This is a string value and represents the timestamp at which a particular event occurred but can have different meanings in different contexts:

Type What "timestamp" field means
disable_bot Empty string
message_new Timestamp at which the message was created. In this case, timestamp and event_timestamp will be the same
message_edited Timestamp of the message that has been changed. Upon receiving a "message_changed" event, you can use "channel_uid" and "timestamp" to identify the message who's text has been changed and modify its text accordingly
message_deleted Timestamp of the message that has been deleted. Upon receiving a "message_changed" event, you can use "channel_uid" and "timestamp" to identify the message that has been deleted and delete that message accordingly
reaction_added Timestamp of the message for which a reaction has been added. Upon receiving a "reaction_added" event, you can use "channel_uid" and "timestamp" to identify the message for which a reaction has been added and change the metadata for that message accordingly
reaction_removed Timestamp of the message for which a reaction has been removed. Upon receiving a "reaction_removed" event, you can use "channel_uid" and "timestamp" to identify the message for which a reaction has been removed and change the metadata for that message accordingly
team_joined Empty string
im_created Empty string

provider

This is a string value and until Relax supports multiple providers, this is always "slack".

event_timestamp

This is a string value and represents the time at which an event occurs. In the case of disable_bot, team_joined and im_created events, it is an empty string.

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
Godeps
_workspace/src/github.com/Sirupsen/logrus
Package logrus is a structured logger for Go, completely API compatible with the standard library logger.
Package logrus is a structured logger for Go, completely API compatible with the standard library logger.
_workspace/src/github.com/cenkalti/backoff
Package backoff implements backoff algorithms for retrying operations.
Package backoff implements backoff algorithms for retrying operations.
_workspace/src/github.com/facebookgo/grace/gracehttp
Package gracehttp provides easy to use graceful restart functionality for HTTP server.
Package gracehttp provides easy to use graceful restart functionality for HTTP server.
_workspace/src/github.com/facebookgo/grace/gracenet
Package gracenet provides a family of Listen functions that either open a fresh connection or provide an inherited connection from when the process was started.
Package gracenet provides a family of Listen functions that either open a fresh connection or provide an inherited connection from when the process was started.
_workspace/src/github.com/facebookgo/httpdown
Package httpdown provides http.ConnState enabled graceful termination of http.Server.
Package httpdown provides http.ConnState enabled graceful termination of http.Server.
_workspace/src/github.com/facebookgo/stats
Package stats defines a lightweight interface for collecting statistics.
Package stats defines a lightweight interface for collecting statistics.
_workspace/src/github.com/gorilla/websocket
Package websocket implements the WebSocket protocol defined in RFC 6455.
Package websocket implements the WebSocket protocol defined in RFC 6455.
_workspace/src/github.com/gorilla/websocket/examples/autobahn
Command server is a test server for the Autobahn WebSockets Test Suite.
Command server is a test server for the Autobahn WebSockets Test Suite.
_workspace/src/github.com/onsi/ginkgo
Ginkgo is a BDD-style testing framework for Golang The godoc documentation describes Ginkgo's API.
Ginkgo is a BDD-style testing framework for Golang The godoc documentation describes Ginkgo's API.
_workspace/src/github.com/onsi/ginkgo/config
Ginkgo accepts a number of configuration options.
Ginkgo accepts a number of configuration options.
_workspace/src/github.com/onsi/ginkgo/ginkgo
The Ginkgo CLI The Ginkgo CLI is fully documented [here](http://onsi.github.io/ginkgo/#the_ginkgo_cli) You can also learn more by running: ginkgo help Here are some of the more commonly used commands: To install: go install github.com/onsi/ginkgo/ginkgo To run tests: ginkgo To run tests in all subdirectories: ginkgo -r To run tests in particular packages: ginkgo <flags> /path/to/package /path/to/another/package To pass arguments/flags to your tests: ginkgo <flags> <packages> -- <pass-throughs> To run tests in parallel ginkgo -p this will automatically detect the optimal number of nodes to use.
The Ginkgo CLI The Ginkgo CLI is fully documented [here](http://onsi.github.io/ginkgo/#the_ginkgo_cli) You can also learn more by running: ginkgo help Here are some of the more commonly used commands: To install: go install github.com/onsi/ginkgo/ginkgo To run tests: ginkgo To run tests in all subdirectories: ginkgo -r To run tests in particular packages: ginkgo <flags> /path/to/package /path/to/another/package To pass arguments/flags to your tests: ginkgo <flags> <packages> -- <pass-throughs> To run tests in parallel ginkgo -p this will automatically detect the optimal number of nodes to use.
_workspace/src/github.com/onsi/ginkgo/internal/remote
Aggregator is a reporter used by the Ginkgo CLI to aggregate and present parallel test output coherently as tests complete.
Aggregator is a reporter used by the Ginkgo CLI to aggregate and present parallel test output coherently as tests complete.
_workspace/src/github.com/onsi/ginkgo/reporters
Ginkgo's Default Reporter A number of command line flags are available to tweak Ginkgo's default output.
Ginkgo's Default Reporter A number of command line flags are available to tweak Ginkgo's default output.
_workspace/src/github.com/onsi/gomega
Gomega is the Ginkgo BDD-style testing framework's preferred matcher library.
Gomega is the Ginkgo BDD-style testing framework's preferred matcher library.
_workspace/src/github.com/onsi/gomega/format
Gomega's format package pretty-prints objects.
Gomega's format package pretty-prints objects.
_workspace/src/github.com/onsi/gomega/gbytes
Package gbytes provides a buffer that supports incrementally detecting input.
Package gbytes provides a buffer that supports incrementally detecting input.
_workspace/src/github.com/onsi/gomega/gexec
Package gexec provides support for testing external processes.
Package gexec provides support for testing external processes.
_workspace/src/github.com/onsi/gomega/ghttp
Package ghttp supports testing HTTP clients by providing a test server (simply a thin wrapper around httptest's server) that supports registering multiple handlers.
Package ghttp supports testing HTTP clients by providing a test server (simply a thin wrapper around httptest's server) that supports registering multiple handlers.
_workspace/src/github.com/onsi/gomega/matchers
Gomega matchers This package implements the Gomega matchers and does not typically need to be imported.
Gomega matchers This package implements the Gomega matchers and does not typically need to be imported.
_workspace/src/gopkg.in/bsm/ratelimit.v1
Simple, thread-safe Go rate-limiter.
Simple, thread-safe Go rate-limiter.
_workspace/src/gopkg.in/bufio.v1
Package bufio implements buffered I/O. It wraps an io.Reader or io.Writer object, creating another object (Reader or Writer) that also implements the interface but provides buffering and some help for textual I/O.
Package bufio implements buffered I/O. It wraps an io.Reader or io.Writer object, creating another object (Reader or Writer) that also implements the interface but provides buffering and some help for textual I/O.
_workspace/src/gopkg.in/redis.v3
Package redis implements a Redis client.
Package redis implements a Redis client.

Jump to

Keyboard shortcuts

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