All Projects → green-api → whatsapp-api-client

green-api / whatsapp-api-client

Licence: MIT license
This library helps you easily create a javascript application with Whatsapp support

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to whatsapp-api-client

whatsapp-http-api
WhatsApp HTTP API that you can configure in a click!
Stars: ✭ 229 (+487.18%)
Mutual labels:  whatsapp, whatsapp-bot, whatsapp-api, whatsapp-chat
waio
Is a pretty simple and fully asynchronous framework for WhatsApp Business API written in Python 3.7 with asyncio and aiohttp.
Stars: ✭ 18 (-53.85%)
Mutual labels:  whatsapp, whatsapp-bot, whatsapp-api
wppconnect-server
Wppconnect Server is a ready-to-use API, just download, install, and start using, simple as that.
Stars: ✭ 290 (+643.59%)
Mutual labels:  whatsapp, whatsapp-bot, whatsapp-api
tithiwa
Automate Web WhatsApp with selenium in python.
Stars: ✭ 17 (-56.41%)
Mutual labels:  whatsapp, whatsapp-bot, whatsapp-api
WhatsappWeb4j
Standalone fully-featured Whatsapp Multi Device Web API for Java and Kotlin
Stars: ✭ 198 (+407.69%)
Mutual labels:  whatsapp-bot, whatsapp-api, whatsapp-chat
whatsapp-chatbot-1c-example
Внешняя обработка для программы 1С:Предприятие, которая демонстрирует реализацию простого чат-бота для мессенджера WhatsApp.
Stars: ✭ 13 (-66.67%)
Mutual labels:  whatsapp, whatsapp-bot, whatsapp-api
Whatsapp Web.js
A WhatsApp client library for NodeJS that connects through the WhatsApp Web browser app
Stars: ✭ 4,103 (+10420.51%)
Mutual labels:  whatsapp, whatsapp-bot, whatsapp-api
Venom
Venom is the most complete javascript library for Whatsapp, 100% Open Source.
Stars: ✭ 3,457 (+8764.1%)
Mutual labels:  whatsapp, whatsapp-bot, whatsapp-api
BocchiBot
BocchiBot is a multipurpose WhatsApp bot using wa-automate-nodejs library!
Stars: ✭ 228 (+484.62%)
Mutual labels:  whatsapp, whatsapp-bot
whatsapp-bot
whatsapp bot build with nodejs
Stars: ✭ 62 (+58.97%)
Mutual labels:  whatsapp, whatsapp-bot
bot-whatsapp
Unmaintained - Multipurpose WhatsApp Bot 🤖 using open-wa/wa-automate-nodejs library! ✨
Stars: ✭ 78 (+100%)
Mutual labels:  whatsapp, whatsapp-bot
alice
Alice is a whatsapp bot made to manage coding groups.
Stars: ✭ 14 (-64.1%)
Mutual labels:  whatsapp, whatsapp-bot
superchats
SuperChats is a premium library with unique features that control Whatsapp functions. With Superchats you can build service bots, multiservice chats or any system that uses whatsapp
Stars: ✭ 100 (+156.41%)
Mutual labels:  whatsapp, whatsapp-api
Nana-MD
Nana Multi Device Testing Bot
Stars: ✭ 29 (-25.64%)
Mutual labels:  whatsapp, whatsapp-bot
whatsapp-bot
Piyobot adalah whatsapp bot pintar
Stars: ✭ 124 (+217.95%)
Mutual labels:  whatsapp, whatsapp-bot
ruby-whatsapp-bots
A repo of WhatsApp bots built in Ruby
Stars: ✭ 18 (-53.85%)
Mutual labels:  whatsapp, whatsapp-bot
whatsie
Qt Based WhatsApp Client
Stars: ✭ 437 (+1020.51%)
Mutual labels:  whatsapp, whatsapp-chat
Chatistics
A WhatsApp Chat analyzer and statistics.
Stars: ✭ 32 (-17.95%)
Mutual labels:  whatsapp, whatsapp-chat
Whatsapp-Bot
Web.whatsapp.com bot made with selenium
Stars: ✭ 39 (+0%)
Mutual labels:  whatsapp, whatsapp-bot
whatsapp-bot
Made with Python and Selenium, can be used to send multiple messages and send messages as characters made of emojis
Stars: ✭ 34 (-12.82%)
Mutual labels:  whatsapp, whatsapp-bot

whatsapp-api-client library for javascript

build License GitHub release npm version

This library helps you easily create a javascript application to connect the WhatsApp API using service green-api.com. You need to get ID_INSTANCEand API_TOKEN_INSTANCE in the control panel in order to use this library. It's free for developers.

API

The API corresponds with REST API from green-api since the library wraps own methods as https calls to the service. Therefore using these docs is highly encouraged.

Installing

Library supports both browser environment without package managers and Node/Webpack apps with package manager that can handle require or import module expressions. For webpack and npm based apps:

npm i @green-api/whatsapp-api-client

For vanilla html-js website modify index.html:

<script src="https://unpkg.com/@green-api/whatsapp-api-client/lib/whatsapp-api-client.min.js"></script>

Importing

There are several ways to import the library in a project

Using common javascript

const whatsAppClient = require("@green-api/whatsapp-api-client");

Using ES6 javascript

import whatsAppClient from "@green-api/whatsapp-api-client";

Using typescript

import * as whatsAppClient from "@green-api/whatsapp-api-client";

Using browser javascript

<script src="https://unpkg.com/@green-api/whatsapp-api-client/lib/whatsapp-api-client.min.js"></script>

Authentification

Sending WhatsApp message like any other call to the API requires account registered on green-api.com and authentication completed on mobile WhatsApp app. To register account you have to proceed to the control panel. After registering you wll get own unique pair of ID_INSTANCE and API_TOKEN_INSTANCE keys.

WhatsApp mobile app authentication may be achived by using control panel. You need to scan QR-code generated within the control panel.

Examples

Send WhatsApp message

Use common javascript

const whatsAppClient = require("@green-api/whatsapp-api-client");

const restAPI = whatsAppClient.restAPI({
  idInstance: "YOUR_ID_INSTANCE",
  apiTokenInstance: "YOUR_API_TOKEN_INSTANCE",
});

restAPI.message.sendMessage(null, 79999999999, "hello world").then((data) => {
  console.log(data);
});

or use browser js script

<script src="https://unpkg.com/@green-api/whatsapp-api-client/lib/whatsapp-api-client.min.js"></script>
<script>
  const restAPI = whatsAppClient.restAPI({
    idInstance: "YOUR_ID_INSTANCE",
    apiTokenInstance: "YOUR_API_TOKEN_INSTANCE",
  });
  restAPI.message
    .sendMessage(null, 79999999999, "hello world")
    .then((data) => {
      console.log(data);
    })
    .catch((reason) => {
      console.error(reason);
    });
</script>

Or use ES6 syntax. For node js app, you propably have to add in package.json property "type": "module". Notice that all examples below are ES6 based.

import whatsAppClient from "@green-api/whatsapp-api-client";

(async () => {
  const restAPI = whatsAppClient.restAPI({
    idInstance: "YOUR_ID_INSTANCE",
    apiTokenInstance: "YOUR_API_TOKEN_INSTANCE",
  });
  const response = await restAPI.message.sendMessage(
    null,
    79999999999,
    "hello world"
  );
})();

Using credentials file for ID_INSTANCE and API_TOKEN_INSTANCE keys (nodejs only!)

You might want to store yours credentials separatedly from code. The library allow you to create a text file with preferred name and location with the format:

API_TOKEN_INSTANCE = "MY_API_TOKEN_INSTANCE"
ID_INSTANCE = "MY_ID_INSTANCE"

And then pass this file as described below:

const restAPI = whatsAppClient.restAPI({
  credentialsPath: "examples\\credentials",
});

Receive notifications using webhook service REST API

import whatsAppClient from "@green-api/whatsapp-api-client";

(async () => {
  let restAPI = whatsAppClient.restAPI({
    idInstance: "YOUR_ID_INSTANCE",
    apiTokenInstance: "YOUR_API_TOKEN_INSTANCE",
  });

  try {
    // Receive WhatsApp notifications.
    console.log("Waiting incoming notifications...");
    await restAPI.webhookService.startReceivingNotifications();
    restAPI.webhookService.onReceivingMessageText((body) => {
      console.log(body);
      restAPI.webhookService.stopReceivingNotifications();
      //console.log("Notifications is about to stop in 20 sec if no messages will be queued...")
    });
    restAPI.webhookService.onReceivingDeviceStatus((body) => {
      console.log(body);
    });
    restAPI.webhookService.onReceivingAccountStatus((body) => {
      console.log(body);
    });
  } catch (ex) {
    console.error(ex.toString());
  }
})();

Send WhatsApp file

import whatsAppClient from "@green-api/whatsapp-api-client";

(async () => {
  const restAPI = whatsAppClient.restAPI({
    idInstance: "YOUR_ID_INSTANCE",
    apiTokenInstance: "YOUR_API_TOKEN_INSTANCE",
  });
  const response = await restAPI.file.sendFileByUrl(
    null,
    79999999999,
    "https://avatars.mds.yandex.net/get-pdb/477388/77f64197-87d2-42cf-9305-14f49c65f1da/s375",
    "horse.png",
    "horse"
  );
})();

Send WhatsApp message and receive webhook

Webhooks are event-based callbacks invoked by green-api server as responses to client API calls. Webhooks support node js and express based apps only.

import whatsAppClient from "@green-api/whatsapp-api-client";
import express from "express";
import bodyParser from "body-parser";

(async () => {
  try {
    // Set http url, where webhooks are hosted.
    // Url must have public domain address.
    await restAPI.settings.setSettings({
      webhookUrl: "MY_HTTP_SERVER:3000/webhooks",
    });

    const app = express();
    app.use(bodyParser.json());
    const webHookAPI = whatsAppClient.webhookAPI(app, "/webhooks");

    // Subscribe to webhook happened when WhatsApp delivered a message
    webHookAPI.onIncomingMessageText(
      (data, idInstance, idMessage, sender, typeMessage, textMessage) => {
        console.log(`outgoingMessageStatus data ${data.toString()}`);
      }
    );

    // Run web server with public domain address
    app.listen(3000, async () => {
      console.log(`Started. App listening on port 3000!`);

      const restAPI = whatsAppClient.restAPI({
        idInstance: MY_ID_INSTANCE,
        apiTokenInstance: MY_API_TOKEN_INSTANCE,
      });
      // Send test message that triggers webhook
      const response = await restAPI.message.sendMessage(
        null,
        79999999999,
        "hello world"
      );
    });
  } catch (error) {
    console.error(error);
    process.exit(1);
  }
})();

There's some cool examples too.

Deploying development environment

Any help with development and bug fixing is appreciated. In order to deploy test-ready environment please make the steps:

  1. Сlone repo with git clone
  2. Install dependencies with npm install
  3. Install globally libraries rollup for bundled builds.
  4. Add webhooks as new dev express via npm npm install express --save-dev. Dont forget to delete it before making pull request
  5. Create .env file in root folder and add environment variables using example file env.example
  6. Add "type": "module" to the package.json

Build

Compile browser and node|webpack versions with single command:

npm run build

Publish to npm if you have access

npm publish

Third-party libraries

License

Licensed on MIT terms. For additional info have look at LICENSE

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].