Code Monkey home page Code Monkey logo

broid-kit's Introduction

npm node bithound bithoundscore nsp-checked

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.


Broid.ai



gitter join-slack

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.

broid-kit's People

Contributors

dustinblackman avatar killix avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.