npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

botsapp

v0.1.0

Published

A Whatapp bot library

Downloads

2

Readme

botsapp

A WhatsApp bot framework in Node

Botsapp is simple framework for creating WhatsApp bots (using the awesome whatsapi project).

Disclaimer

I know. I'm sorry. I really hope this doesn't end up powering lots of annoying WhatsApp bots, but I needed this for a personal project.

Be warned that there are plenty of reports of people getting their number banned from WhatsApp when using anything other than the official clients. Using this code may result in your account being banned. The bot will attempt to follow the protocol as closely as possible to avoid that, but this is based on annecdotal evidence rather than watching network traffic (as that is against the WhatsApp terms and conditions).

Install

npm install --save botsapp

Usage

'use strict';

var Botsapp = require('botsapp');
var process = require('process');

var yourBot = new Botsapp.Bot({
  adapter: {
    msisdn: '123456789', // phone number with country code
    username: 'YourBot', // your name on WhatsApp
    password: 'asdfghjkl', // WhatsApp password
    ccode: '44' // country code
  }
});

// Register a handler which logs every message
var anyMessage = new Botsapp.Trigger().always();
yourBot.registerTrigger(anyMessage, function onTrigger(event) {
  console.log(event);
});

// Get a thumbsup, give a thumbsup
var thumbsupEmoji = new Buffer([240, 159, 145, 141]);
var thumbsUp = new Botsapp.Trigger().withEmoji(thumbsupEmoji);

yourBot.registerTrigger(thumbsUp, function onTrigger(event) {
  var emoji = thumbsupEmoji.toString('utf8');

  yourBot.sendMessage(event.from, emoji, function onSend() {
    console.log('Sent emoji to', event.from);
  });
});

// Get hello from a specific user
var author = '[email protected]';
var helloFromMe = new Botsapp.Trigger()
  .from(author)
  .withText('hello');

yourBot.registerTrigger(helloFromMe, function onTrigger(event) {
  console.log('Got hello from', author, event.body);
});

// Connect to the server
yourBot.connect(function() {
  console.log("I'm alive!");
});

yourBot.on('error', function gracefulShutdown() {
  yourBot.destroy();
  process.exit(1);
});

API

There are a few exports:

  • Bot
  • Trigger
  • Dispatcher
  • DrainDisptacher

Bot

Make an instance of Bot to establish a connection. The bot provides methods to register actions and to interact with WhatsApp. Currently, only sending a message (to a user or group) is supported.

Trigger

Triggers are sets of conditions which trigger functions when all (default) or any of those conditions are met. Triggers can be arbitrarily constructed with a chain.

Matching any conditions:

var someWords = new Trigger({
 mode: 'any',
}).withText('foo').withText('bar')

Matching all conditions:

var helloInGroup = new Trigger()
 .withText('hello')
 .inGroup()

Dispatcher (+ DrainDispatcher)

The Dispatcher is used to dispatch events from WhatsApp, check if they match triggers and invoke actions. By default the Dispatcher class will dispatch every event from the server to the triggers.

When the bot logs in, you will automatically be dispatched any events which happened when the bot was offline. If you'd like to ignore all those events and only process going forwards, you should use the DrainDispatcher:

var beanBot = new Botsapp.Bot({
  dispatcher: new Botsapp.DrainDispatcher(),
  adapter: {
    ...
  }
});

TODO

  • [ ] Groups (join, leave, edit, invite, promote, demote, info)
  • [ ] Send pictures, videos, vcards, locations
  • [ ] Sync contacts (to avoid bans)
  • [ ] Capture group notifications (joins/leaves/edits)
  • [ ] Unit tests!

Note: Many of these thigs can be done directly with whatsapi on bot.adapter.

Full API description:


Bot(options: Object) => {
  connect: (callback: Function) => void,
  destroy: () => void,
  registerTrigger: (trigger: Trigger, handler: Function, callback: Function) => void,
  sendMessage: (recipient: String, text: String, callback: Function) => void,
}

Trigger(options: Object) => {
  matches: (event: Object) => Boolean,
  always: () => Trigger,
  withText: (text: String, options: Object) => Trigger,
  withEmoji: (emoji: Buffer) => Trigger,
  from: (author: String) => Trigger,
  inGroup: (options: Object) => Trigger,
  custom: (predicate: (event: Object) => Boolean) => Trigger
}