rex.js is a Slack bot framework built on top of rx.js. The conversation context is modelled using observables which fits well with the asynchronous nature of instant messaging and means you can use the huge library of rx operators to define how your bot works.

rex.js also allows for flexible plugins that provide additional functionality such as authorization.

Here's a simple, but complete bot:

```javascript
const Bot = require('rexjs').Bot;
var botx = new Bot(slackBotToken);

bot.connect().then(() => {
   bot.say('hi everybody');
});

bot.hear('hello')
   .reply('hi hi')
   .start();
```

The start() method is necessary to start the observable and calls subscribe() under the hood.

Replying back to a user is easy. In this example the bot will randomly respond with either "hi" or "yo" in the original channel or private message:
```javascript
bot.hear("hi")
   .reply("(hi|yo)")
   .start();
```

Or you can use `say` to talk to another user or channel:
```javascript
bot.hear("hi")
   .say("@bob", "(hi|yo) bob")
   .start();
```

`bot.hear` returns an observable of message objects. A message looks like this:
```javascript
class Message {
	text: string;
	user: User;
	channel: string;
	privateMessage: boolean;
}
```

And as the converation is an observable any standard Rx operators can be used. This opens up a huge range of options for jobs like filtering, debouncing etc:
```javascript
// only reply once within a ten second period, and ignore anything not in #random
bot.hear("hi")
   .filter(m => m.channel == "#random")
   .debounce(() => Rx.Observable.timer(10000))		
   .reply("hi")
   .start();
```

The bot.hear() method supports a powerful string matching language. For example:

```javascript
// responds to either "hi" or "hello" 
bot.hear("(hi|hello)")		
// * matches anything, e.g. "hi bob"
bot.hear("hi *")			
```

And you can also capture arguments which will be added to the message object:
```javascript
bot.hear("say {word}")
   .reply(m => "Ok, saying " + m.word)
   .start();
```

Arguments can also be typed as below. Note here that reply() can also take a lambda function that takes a message and returns a string allowing for much more flexible responses:
```javascript
bot.hear("{first:number} + {second:number}")
   .reply(m => m.first + m.second)
   .start();
```

Combined with the rx.js do() operator you can start to build more complex and useful bots:
```javascript
var request = require('request');				
bot.hear("ping google")
   .do(m => {  
		request('http://www.google.com', function (error, response, body) { 
			bot.say("google's status is: " + response.statusCode);
		}); 
   })
   .start();
```

If you need to ask the user for more information you can use the ask() operator. This will wait for the user to reply and capture their response in the message object. By default it will be added to the messgae object with the name "ask" like so:
```javascript
bot.hear("hi")
   .ask("hey, what's your name??")
   .reply(m => "hi " + m.ask)
   .start();
```

Alternatively you can define how the response should be handled:
```javascript
bot.hear("hi")
   .ask("hey, what's your name??", { then: (m, resp) => m.name = resp })
   .reply(m => "hi " + m.name)
   .start();
```

//add predicate here

You can refer to the bot's name using $me:
```javascript
// this will match to "hi @botname"
bot.hear("hi $me")				
   .reply(m => "hi hi " + m.user.username)
   .start();
```
