0.0.22 • Published 6 years ago

rxbot v0.0.22

Weekly downloads
89
License
-
Repository
-
Last release
6 years ago

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:

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:

bot.hear("hi")
   .reply("(hi|yo)")
   .start();

Or you can use say to talk to another user or channel:

bot.hear("hi")
   .say("@bob", "(hi|yo) bob")
   .start();

bot.hear returns an observable of message objects. A message looks like this:

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:

// 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:

// 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:

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:

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:

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:

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:

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:

// this will match to "hi @botname"
bot.hear("hi $me")				
   .reply(m => "hi hi " + m.user.username)
   .start();
0.0.22

6 years ago

0.0.21

6 years ago

0.0.20

6 years ago

0.0.19

6 years ago

0.0.18

6 years ago

0.0.17

6 years ago

0.0.16

6 years ago

0.0.15

7 years ago

0.0.14

7 years ago

0.0.13

7 years ago

0.0.12

7 years ago

0.0.11

7 years ago

0.0.10

7 years ago

0.0.9

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago