@broid/kit
v1.1.0
Published
Bot framework supported all messaging plateforms and middlewares.
Downloads
55
Readme
Broid Kit
Broid Kit aims to ease the creation of bots communicating through messaging platforms. Broid Kit is powered by Broid Integrations 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.
Quick Example
Note: Options for the integration examples can be found on the Discord, Messenger, and Slack documentation respectively.
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.
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);
});
Documentation
Receive all group messages
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);
});
Receive all private messages
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);
});
Receive all
Because Broid Kit is built with Observables, you can subscribe to multiple sources.
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);
});
Matching patterns and keywords
bot.hears(["keyword", "hello.*"], "Group")
.subscribe(data => {
console.log("Data:", JSON.stringify(data, null, 2));
});
Node callback is supported
bot.hear("hello.*", "Group", (data, error) => {
console.log("Data:", JSON.stringify(data, null, 2));
});
bot.hears(["keyword", "hello.*"], "Group", (data, error) => {
console.log("Data:", JSON.stringify(data, null, 2));
});
Send a simple message
bot.sendText("Hello world.", data.message);
Send a video or image message
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:
{
"content": "description of the meta",
"title": "title for the media"
}
Middleware
Broid kit supports middleware to allow you to preprocess received or sent messages.
Example of Middleware preprocessing
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:
bot.use(new MyMiddleware());
WebHooks
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
.
Contribute
See CONTRIBUTE.md.