@broid/kit
Version:
Bot framework supported all messaging plateforms and middlewares.
250 lines (179 loc) • 6.74 kB
Markdown
[]:https://img.shields.io/badge/npm-broid-green.svg?style=flat
[]:https://www.npmjs.com/org/broid
[]:https://img.shields.io/node/v/@broid/broid-kit.svg
[]:https://nodejs.org
[]:https://img.shields.io/travis/broidHQ/broid-kit/master.svg
[]:https://travis-ci.org/broidHQ/broid-kit
[]:https://img.shields.io/bithound/code/github/broidHQ/broid-kit.svg
[]:https://www.bithound.io/github/broidHQ/broid-kit
[]:https://www.bithound.io/github/broidHQ/broid-kit/badges/score.svg
[]:https://www.bithound.io/github/broidHQ/broid-kit
[]:https://img.shields.io/badge/nsp-checked-green.svg?style=flat
[]:https://nodesecurity.io
[]:https://badges.gitter.im/broidHQ/broid.svg
[]:https://t.broid.ai/c/Blwjlw?utm_source=github&utm_medium=readme&utm_campaign=top&link=gitter
[]:https://img.shields.io/badge/chat-on_slack-lightgrey.svg?style=flat
[]:http://slackin.broid.ai/
[![npm][npm]][npm-url]
[![node][node]][node-url]
[![bithound][bithound]][bithound-url]
[![bithoundscore][bithoundscore]][bithoundscore-url]
[![nsp-checked][nsp-checked]][nsp-checked-url]
Broid Kit aims to ease the creation of bots communicating through messaging platforms. Broid Kit is powered by [Broid Integrations](https://github.com/broidHQ/integrations/edit/master/README.md) which allows you to leverage the largest collection of messaging channels integrated in a given framework.
> Connect your App to Multiple Messaging Channels with the W3C Open standards.
<br>
<p align="center">
<a href="https://github.com/broidHQ/integrations">
<img alt="Broid.ai" src="https://cloud.githubusercontent.com/assets/8091600/24985411/f0667b2c-1fc1-11e7-8a8a-012655cf0d15.png">
</a>
</p>
<br>
<br>
[![gitter][gitter]][gitter-url] [![join-slack][join-slack]][join-slack-url]
_Note:_ Options for the integration examples can be found on the [Discord](https://github.com/broidHQ/integrations/tree/master/broid-discord), [Messenger](https://github.com/broidHQ/integrations/tree/master/broid-messenger), and [Slack](https://github.com/broidHQ/integrations/tree/master/broid-slack) documentation respectively.
```javascript
const Bot = require("@broid/kit");
const BroidDiscord = require("@broid/discord");
const BroidMessenger = require("@broid/messenger");
const BroidSlack = require("@broid/slack");
const bot = new Bot({
logLevel: "info",
http: {
host: "0.0.0.0",
port: 8080,
}
});
bot.use(new BroidDiscord({...options}));
bot.use(new BroidMessenger({...options}));
bot.use(new BroidSlack({...options}));
// Listening for public starting by regex match for `hello`
bot.hear("hello.*", "Group")
.subscribe((data) => {
console.log("Data:", JSON.stringify(data, null, 2));
// Reply to the message
bot.sendText("Hi, How are you?", data.message);
});
```
Broid-Kit can also be used with your existing Express setup.
```javascript
const Bot = require("@broid/kit");
const BroidDiscord = require("@broid/discord");
const BroidMessenger = require("@broid/messenger");
const BroidSlack = require("@broid/slack");
const express = require("express");
const bot = new Bot({
logLevel: "info"
});
bot.use(new BroidDiscord({...options}));
bot.use(new BroidMessenger({...options}));
bot.use(new BroidSlack({...options}));
// Setup express
const app = express();
app.use("/", bot.getRouter());
app.listen(8080);
// Listening for public starting by regex match for `hello`
bot.hear("hello.*", "Group")
.subscribe((data) => {
console.log("Data:", JSON.stringify(data, null, 2));
// Reply to the message
bot.sendText("Hi, How are you?", data.message);
});
```
```javascript
bot.on("Group")
.subscribe((data) => {
console.log("Data:", JSON.stringify(data, null, 2));
// Reply to the message
bot.sendText("i am listening all messages", data.message);
});
```
```javascript
bot.on("Person")
.subscribe((data) => {
console.log("Data:", JSON.stringify(data, null, 2));
// Reply to the message
bot.sendText("i am listening all messages", data.message);
});
```
Because Broid Kit is built with [Observables](https://github.com/ReactiveX/RxJS), you can subscribe to multiple sources.
```javascript
const Observable = require("rxjs").Observable;
Observable.merge(bot.on("Group"), bot.on("Person"))
.subscribe((data) => {
console.log("Data:", JSON.stringify(data, null, 2));
// Reply to the message
bot.sendText("i am listening all messages", data.message);
});
```
```javascript
bot.hears(["keyword", "hello.*"], "Group")
.subscribe(data => {
console.log("Data:", JSON.stringify(data, null, 2));
});
```
```javascript
bot.hear("hello.*", "Group", (data, error) => {
console.log("Data:", JSON.stringify(data, null, 2));
});
```
```javascript
bot.hears(["keyword", "hello.*"], "Group", (data, error) => {
console.log("Data:", JSON.stringify(data, null, 2));
});
```
```javascript
bot.sendText("Hello world.", data.message);
```
```javascript
bot.sendImage("http://url-of-media", data.message, optionalMeta);
// OR
bot.sendVideo("http://url-of-media", data.message, optionalMeta);
```
`optionalMeta` is an object of optional information for the media.
It should like:
```json
{
"content": "description of the meta",
"title": "title for the media"
}
```
Broid kit supports middleware to allow you to preprocess received or sent messages.
Example of Middleware preprocessing
```javascript
class MyMiddleware {
constructor() {}
serviceName() {
return "MyMiddleware";
}
incoming(bot, message) {
// the return value can be an Promise<object>, Observable<object> or null
return "Hello world!";
}
outgoing(bot, message) {
// the return value can be an Promise<object>, Observable<object> or null
// The object.content field will be use to update the text of the message sent.
return "Good bye world!";
}
}
```
This middleware can be used like so:
```javascript
bot.use(new MyMiddleware());
```
Broid Kit provide an http server and creates a default webhook route when the integration requires it. By default, the webhook path follows the naming convention: `webhook/<integration>`, integration is the name provide by the `getServiceName` method.
In case of `@broid/skype` the webhook route will be `/webhook/skype`.
See [CONTRIBUTE.md](CONTRIBUTE.md).