
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
A WhatsApp bot framework in Node
Botsapp is simple framework for creating WhatsApp bots (using the awesome whatsapi project).
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).
npm install --save botsapp
'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 = '123456789@s.whatsapp.net';
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);
});
There are a few exports:
Bot
Trigger
Dispatcher
DrainDisptacher
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.
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()
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: {
...
}
});
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
}
FAQs
A Whatapp bot library
The npm package botsapp receives a total of 2 weekly downloads. As such, botsapp popularity was classified as not popular.
We found that botsapp demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.