## Relax-js

Relax-js is a Node.js client for interacting with [Relax](https://github.com/zerobotlabs/relax) – a multitenant message broker for Slack.

There are two primary functions that this client library lets you perform:

1. Spin up new Slack bots on Relax
2. Listen for events generated by Relax

## Getting Started

If you're just getting started with Relax and don't already have a Relax Go instance up and running, go ahead and get that set up first by following the instructions [here](https://github.com/zerobotlabs/relax) (Don't worry you don't need any prior knowledge of Go). If you use Heroku, there's a "Deploy to Heroku" button to help you get up and running super quickly. 

Once you've done that, take note of the following environment variables configured on your Relax Go instance (you'll need to use the exact same ones for your Node.js app):

- `REDIS_URL`
- `RELAX_BOTS_KEY`
- `RELAX_BOTS_PUBSUB`
- `RELAX_EVENTS_QUEUE`
- `RELAX_MUTEX_KEY`

If you haven't already, go ahead and create a new Node.js app. Next, install the `relax-js` node module in either of the following ways:

Install directly with npm:

```
$ npm install relax-js
```
or add to your package.json file and then npm install:

```js
"relax-js": "0.0.1"
```

```
$ npm install
```

## Usage

Before using this module, make sure your environment variables (the 5 listed above) are set and match the ones used on your Relax Go instance. If they aren't set, Relax-js will throw errors.

**Initialize a new Relax-js instance**

```js
var Relax = require('relax-js');
// create new relax instance
var relax = new Relax();
```

**Spin up a new bot running on Relax**

You can create a new bot for your Slack team [here](https://my.slack.com/services/new/bot) if you haven't already. 
The Slack API should also be able to provide you with your team's uid, which you will need along with your bot's token in order to spin up a bot on Relax.

```js
relax.createBot(teamUid, botToken);
```

**Listen for bot events from Relax**

Here is an example of setting up an event handler for new messages. For information about all events generated by Relax, go [here](https://github.com/zerobotlabs/relax#events).

```js
// Set up your event listeners
relax.on('message_new', function (data) {
  console.log('Heard new message!', data);

  // data object should look something like this:
  // {
  //   type: 'message_new',
  //   user_uid: 'UXXXXXXXX',
  //   channel_uid: 'DXXXXXXXX',
  //   team_uid: 'TXXXXXXXX',
  //   im: true, // or false
  //   text: 'This is a message',
  //   relax_bot_uid: 'UXXXXXXXX',
  //   timestamp: '1462579994.000021',
  //   provider: 'slack',
  //   event_timestamp: '1462579994.000021',
  //   namespace: '' 
  // }
});

// Start listening for messages
relax.start();
```

## License
MIT
