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

whatsapp-chapi

v1.1.1

Published

WhatsApp chat api

Downloads

103

Readme

WhatsApp Node.JS Bot API

Use this library to develop a bot for the WhatsApp platform. The library is available on GitHub as well as a package on npm.

Get Started Video

Demo

Send a message to + (374) 98 445878 through WhatsApp and test the API.

License

This library is released under the terms of the MIT license. See License for more information.

Library Prerequisites

  1. Node >= 8.0.0
  2. WhatsApp account.
  3. Active Instance Id - Get an instance here.
  4. Account authentication token - unique account identifier used to validate your instance in all API requests.
  5. Webhook - Please use a server endpoint URL that supports HTTP/HTTPS.

Installation

This library is released on npm.

npm

Install with npm install whatsapp-chapi --save

Express

If you are already using express or equivalent, you can do the following:

app.use("/whatsapp/webhook", bot.middleware());

Please revisit app.use() documentation. For more information see bot.middleware().

Let's get started!

Creating a basic WhatsApp bot is simple:

  1. Import whatsapp-chapi library to your project
  2. Create an endpoint for notifications
  3. Create a 'Chapi' instance
  4. Scan your QR here
  5. Start your web server
  6. Call setWebhook(url) with your web server url

Creating an echo Bot

Firstly, let's import and configure our bot:

'use strict';
var express = require('express');
const Chapi = require('whatsapp-chapi');

const app = express();
const bot = new Chapi(YOUR_INSTANCE_ID_HERE, YOUR_AUTH_TOKEN_HERE);
bot.signIn('YOUR_WHATSAPP_ACCOUNT_PHONE');

app.post('/YOUR_WEBHOOK_HERE', function (req, res) {
  bot.sendMessage(req.body.messages[0].author, 'ECHO TEXT');
  res.sendStatus(200);
});


// Wasn't that easy? Let's create HTTPS server and set the webhook:
const http = require('http');
const port = process.env.PORT || 8080;

// Chapi will push messages sent to this URL. Web server should be internet-facing.
const webhookUrl = process.env.WEBHOOK_URL;
http.createServer(app).listen(port, () => bot.setWebhook(webhookUrl));

API

bot.setWebhook(url)

| Param | Type | Description | | --- | --- | --- | | url | string | Full url to your endpoint for message notification |

Returns a promise.JSON.

bot.setWebhook("https://my.bot/incoming")
  .then(() => yourBot.doSomething())
  .catch(err => console.log(err));

bot.signIn(phone)

| Param | Type | Description | | --- | --- | --- | | phone | string | Your whatsApp account phone |

Returns a promise.JSON.

bot.signIn("12345678911")
  .then(() => yourBot.doSomething())
  .catch(err => console.log(err));

Note: Phone number should be in following format 12345678912, without + or any other symbols

bot.sendMessage(phone, message)

| Param | Type | Description | | --- | --- | --- | | phone | string | WhatsAppPhoneNumber string | | message | string | text message to send |

Note: Phone number should be in following format 12345678912, without + or any other symbols

Returns a promise.JSON.

// Single message
const Chapi = require('whatsapp-chapi');
const bot = new Chapi(YOUR_INSTANCE_ID_HERE, YOUR_AUTH_TOKEN_HERE);
bot.sendMessage('12345678912', 'Hello');

bot.sendPreviewUrl(phone, urlContent)

| Param | Type | Description | | --- | --- | --- | | phone | string | WhatsAppPhoneNumber string | | urlContent | JSON | text message to send |

Note: Phone number should be in following format 12345678912, without + or any other symbols

Returns a promise.JSON.

// Single message
const Chapi = require('whatsapp-chapi');
const bot = new Chapi(YOUR_INSTANCE_ID_HERE, YOUR_AUTH_TOKEN_HERE);
bot.sendPreviewUrl('12345678912', {
  title : 'Preview title', 
  desc : 'Preview description', 
  text : 'Preview text',
  url : 'Preview url', 
  thumb : 'Preview image'
});

Note: Preview image should be in following format Base64

bot.sendFile(phone, url)

| Param | Type | Description | | --- | --- | --- | | phone | string | WhatsAppPhoneNumber string | | url | string | download url for file |

Returns a promise.JSON.

// File message
const Chapi = require('whatsapp-chapi');
const bot = new Chapi(YOUR_INSTANCE_ID_HERE, YOUR_AUTH_TOKEN_HERE);
bot.sendFile('12345678912', 'https://assets.fireside.fm/file/fireside-images/podcasts/images/b/bc7f1faf-8aad-4135-bb12-83a8af679756/cover_medium.jpg');

bot.getStatus()

Returns a promise.JSON.

// File message
const Chapi = require('whatsapp-chapi');
const bot = new Chapi(YOUR_INSTANCE_ID_HERE, YOUR_AUTH_TOKEN_HERE);
bot.getStatus()
.then((res) => {
  console.log(res); // returns account status, return QR code if status is pending
});

Docs & Community

Sample project

We've created the sample project to help you get started.