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

@green-api/whatsapp-bot

v0.2.5

Published

WhatsApp Bot for NodeJs

Downloads

21

Readme

WhatsApp chat bot library for nodejs

License GitHub release npm version

Introduction

The WhatsApp chat bot designed for writing own chat bots. Users can interact with bots by sending them command messages in private or group chats. The bot uses Green-API (green-api.com) provider WhatsApp API protocol under hood to support WhatsApp. Green API whatsApp protocol exists in two versions:

  • V0 - requires plugged and active phone. The protocol provides reach WhatsApp API including messages, media, phone state, location etc. Have look at whatsapp-api-client wrapper REST protocol library for more information
  • V1 - phone-free protocol that requires only phone number without physical device. The protocol implements only limited part of WhatsApp API. See details here v1-whatsapp-api-client

Installation

npm i @green-api/whatsapp-bot

Getting started

1. Get green api account

To use the WhatsApp Bot API, you first have to visit green-api.com and get free developer account for API-V0. Green Api will give you id instance and api token, something like

ID_INSTANCE: "0000",
API_TOKEN_INSTANCE: "000000000000000000AAAAAAAAAAAAAA"

In case you want API-V1 protocol you also have to visit green-api.com and choose Chat bot price option. Green API will give you a free trial period if you ask them. Access token looks like this:

token = 'gr.abcdefg...'

2. Add import

You can import library using modern ES6 syntax (you have to add "type":"module" to package.json):

import WhatsAppBot from '@green-api/whatsapp-bot'

or using classic syntax:

const WhatsAppBot = require('@green-api/whatsapp-bot')

3. Initiliaze new WhatsApp Bot with aquired account data

For API-V0 protocol

const bot = new WhatsAppBot({
    idInstance: "0000",
    apiTokenInstance: "000000000000000000AAAAAAAAAAAAAA"
})

For API-V1 protocol

const bot = new WhatsAppBot(process.env.TOKEN_V1, {apiType: WhatsAppBot.GreenApiV1})

3. Start coding

A WhatsApp bot was inpired by telegram bot framework - Telegraf. But the WhatsApp bot library inherited limited part of Telegraf API. At this moment whatsapp bot can send and receive text, interact with user by telegraf scenes and use sessions. The bot supports only long-polling mode. To understand basics have look at examples below.

Examples

Hello world example responds with a plain text phrase to any users print:

const WhatsAppBot = require('@green-api/whatsapp-bot')

const bot = new WhatsAppBot({
    idInstance: process.env.ID_INSTANCE,
    apiTokenInstance: process.env.API_TOKEN_INSTANCE
})
bot.on('message', (ctx) => ctx.reply('Hello world!'))
bot.launch()

Bot listens for users command beginning with the / symbol

const WhatsAppBot = require('@green-api/whatsapp-bot')

const bot = new WhatsAppBot({
    idInstance: process.env.ID_INSTANCE,
    apiTokenInstance: process.env.API_TOKEN_INSTANCE
})
bot.command('oldschool', (ctx) => ctx.reply('Hello'))
bot.command('modern', ({ reply }) => reply('Yo'))
bot.command('hipster', WhatsAppBot.reply('λ'))
bot.on('message', (ctx) => ctx.reply('Send /oldschool, /modern or /hipster to launch bot'))
bot.launch()

There's some cool V0-V1 api examples too. The bots are great for running in docker containers. Take a look at dockerized simple-reg-bot example

Documentation

License

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

Third-party libraries