role-model
v2.0.0
Published
Robot on LeanEngine
Downloads
4
Readme
RoLE model
What's this?
RoLE stands for Robot on LeanEngine. It's a library for writing chat robot hosted on LeanEngine. LeanEngine is a service provided by LeanCloud.
Instructions
RoLE is available on NPM. Add "role-model": "^0.0.9"
to your package.json
.
The package only exports one method createRobotApp()
:
const role = require('role-model');
const app = role.createRobotApp({
chatService: 'bearychat',
chatServiceOptions: {
team: process.env.BEARYCHAT_TEAM,
token: process.env.BEARYCHAT_TOKEN
}
});
As of this writing, BearyChat and Zulip are the only supported service, but it is
very easy to add support to other services (look at lib\zulip.js
for an example).
Please contribute. In the above example, the credentials were read from environment variables which can be set on the LeanCloud web console.
The returned object has three properties:
expressApp
- an Express app instance, which you can use to add additional routes and middleware.leanEngine
- a LeanEngine SDK instance that you would otherwise get fromrequire('leanengine')
. This is provided just in case you need LeanCloud functionalities such as data storage.robot
- this is the object you would use to define the behavior of your robot.
The robot is programmed by defining predicates to match incoming messages and their handlers.
You can use a list of keywords:
app.robot.addHandler(['ping'],
context => context.respond('pong'));
If the incoming message has all the keywords, the handler will be executed.
Or you can use a regular expression:
app.robot.addHandler(/ping/,
context => context.respond('pong'));
Or to be most flexible, just a plain function:
app.robot.addHandler((msg) => msg === "ping",
context => context.respond('pong'));
At the end, call
app.run()
This needs to be the last line in your main program, because it's
blocking (calls expressApp.listen()
).
Now deploy your project to LeanCloud, configure your web hosting domain name, and add a robot of type "Hubot" to your BearyChat team. You should be able to receive responses from your new bot.
Make sure you use Node 4+, because RoLE uses some ES6 features. So add the following to your package.json
:
"engines": {
"node": ">=4.x"
}