Git Product home page Git Product logo

whats2api's Introduction

WHATS2API - WhatsApp REST API


npm version

Welcome to best REST API for WhatsApp. This library provide you an complete solution for WhatsApp REST API without any necessities of code or change anything, obviously, if you want, you can contribute but give this information for us over PR. - Important to know, it's not an official solution - if you need something professional please don't use this package.

Special thanks to developers of amazing packages used here, Mohammed Shah (smashah) and Total.js (totaljs)

SETUP

This package was thought to be used as service for each connection, so you need to understand the structure of files and folders before start - please read with attention.

Each connection with WhatsApp will need an dedicated running of this, you can run a lot of connections on the same server using PM2 or forever. For better approach too we will provide you an Docker solution.

Each client is undertood as INSTANCE, we gonna set an instance NUMBER for him, to identify after if necessary. If you are running this probably you need to receive messages, the struture of receive will be over WEBHOOK post events, so you need to configure your preffered address too. (TIP: use some service as https://requestcatcher.com/ to test ). Another important thing to set is some TOKEN, this is used for protect your inbound endpoint. For a high level of changes you will need to set an MASTERKEY too, that can be used to do profound changes on server like restarts and force stops.

Configuring the enviroment

To start - download the package, open /config file and set variables below - please respect type of variables or your code will not run properly

//WhatsApp Configurations		
instance            : 83430
webhook             : http://localhost:3000
token               : j19ksi1mim1lksm12213
masterKey           : w2apiisthebestlibrary

Start the project

> npm install && node debug.js

As soon you start the project navigate to address http://localhost/{{instanceNumber}}/qrCode and wait for qrCode loads, this page also will auto refresh your qrCode each 30s, so if you does not have success on first attempt just wait and your qrCode will updeta himself. FYI this is run over WebSockets.

MultiInstances

If you are looking how to run this on multi-instances please let me introduce another way, it's better then old fashion of PM2 and multiple clients on same server(if you don't know what is pm2, dont worry it's not important). The best you can do is run each instance separated, when you realize all that primary updates will be designed thinking on this. That way you avoid troubles on production enviroment effect multiple instances. To do this we gonna use Docker - Advise you to study a little bit docker for run this propperly. (an Docker-compose is alredy prepared for use)

Docker image is available on DockerHub: https://hub.docker.com/r/joaomirandas/whats2api

You can built your own image with modifications, clone this repo to your preffered local, and run:

> docker build -t joaomirandas/whats2api .

This above command will create an container image of whats2API and prepare for run, when you feel good, just run:

> docker run --name instance1 -p 8001:8000 joaomirandas/whats2api

Documentation

Full documentation library can be found over Postman Collection, best way to share regardless of the language you prefer to use.

Run in Postman 0.0.8

Using Library

All the examples below are provided in CURL to allow as many users as possible to enjoy the solutions created, however if you use any specific language feel free to download the Postman collection here and get the code in its best language for you.

At any request you can use "chatId" or "phone" paramether but at the least one of them is necessary to works.

Sending Messages

curl --location --request POST 'localhost:8000/83430/sendMessage?token=j19ksi1mim1lksm12213' \
--header 'Content-Type: application/json' \
--data-raw '{
	"chatId":"[email protected]",
	"body": "W2API - Best REST API for WhatsApp"
}'

Sending Audios

This will be sent as attached file, it's not like an audio recorded on mobile device. Attention, at this example we use PHONE body paramether, so it's possible send requests using

curl --location --request POST 'localhost:8000/83430/sendPTT?token=j19ksi1mim1lksm12213' \
--header 'Content-Type: application/json' \
--data-raw '{
	"phone":"PHONE_NUMBER",
	"audio": "https://upload.wikimedia.org/wikipedia/en/6/68/Crescendo_example.ogg"
}'

Request response:

{
    "status": true
}

Sending Files (Image, PDF, TXT, etc..)

This will be sent as attached file, it's important pay attention on maxfilesize allowed.

curl --location --request POST 'localhost:8000/83430/sendFile?token=j19ksi1mim1lksm12213' \
--header 'Content-Type: application/json' \
--data-raw '{
	"phone":"PHONE_NUMBER",
	"body": "https://upload.wikimedia.org/wikipedia/ru/3/33/NatureCover2001.jpg",
	"filename": "NatureCover2001.jpg",
	"caption": "W2API - Best REST API for WhatsApp"
}'

Sending Location

This will send a location provided to user. Performance Operational on version 0.0.8

curl --location --request POST 'localhost:8000/83430/sendLocation?token=j19ksi1mim1lksm12213' \
--header 'Content-Type: application/json' \
--data-raw '{
	"chatId":"[email protected]",
	"lat": "51.5074",
	"lng": "0.1278",
	"address": "LONDON!"
}'

Sending Links with preview

This will send a link with thumb preview Performance operational on version 0.0.8

curl --location --request POST 'localhost:8000/83430/sendLinkPreview?token=j19ksi1mim1lksm12213' \
--header 'Content-Type: application/json' \
--header 'Content-Type: text/plain' \
--data-raw '{
	"chatId":"[email protected]",
	"link": "https://github.com/joaomirandasa/whats2api/",
	"text": "W2API - Best REST API for WhatsApp"
}'

Send Contact

This route allow you to send a lot of information within an VCard. Performance Operational on version 0.0.8

curl --location --request POST 'localhost:8000/83430/sendContact?token=j19ksi1mim1lksm12213' \
--header 'Content-Type: application/json' \
--header 'Content-Type: text/plain' \
--data-raw '{
	"chatId":"[email protected]",
	"firstName": "Joao Vitor",
	"middleName": "Miranda",
	"lastName": "Santos",
	"organization": "W2API",
	"photo": "https://avatars3.githubusercontent.com/u/29043431?s=460",
	"workPhone":"+5511995126279",
	"title": "Best API Builder",
	"url": "www.w2api.io",
	"note": "W2API - Best REST API for WhatsApp"
}'

Sending Giphy

This will send an Giphy to user based on his web address. Performance Operational on version 0.0.8

curl --location --request POST 'localhost:8000/83430/sendGiphy?token=j19ksi1mim1lksm12213' \
--header 'Content-Type: application/json' \
--data-raw '{
	"chatId":"[email protected]",
	"link": "https://media.giphy.com/media/WirhZMBF1AZVK/giphy.gif",
	"caption": "W2API - Best REST API for WhatsApp"
}'

Change your Status

This route allow you to change status information of number connected. Performance Operational on version 0.0.8

curl --location --request POST 'localhost:8000/83430/setMyStatus?token=j19ksi1mim1lksm12213' \
--header 'Content-Type: application/json' \
--data-raw '{
	"newStatus": "Work & Trust"
}'

Get Battery Level

This route allow you to check battery level of physical device running WhatsApp. Performance Operational on version 0.0.8

curl --location --request GET 'localhost:8000/83430/w2apiisthebestlibrary/batteryLevel?token=j19ksi1mim1lksm12213'

Read Instance

This route allow you get a lot of informations about instance running and number connected to them. Performance Operational on version 0.0.8

curl --location --request GET 'localhost:8000/w2apiisthebestlibrary/readInstance'

Restart Instance

This route allow you restart your instance without aceess them over CLI or SSH, this really usefull with Docker. Performance Operational on version 0.0.8

curl --location --request GET 'localhost:8000/w2apiisthebestlibrary/reloadServer'

Change Webhook

This route allow you to change webhook address on production without restart your server or instance, it's an not permanent change, so if you change this in production and restart, on your next session webhook will continue to be the old one. Tho keep this change permanent you need to change config file on root folder of library. Performance Operational on version 0.0.8

curl --location --request POST 'localhost:8000/w2apiisthebestlibrary/setWebhook' \
--header 'Content-Type: application/json' \
--data-raw '{
    "webhook": "https://example2.requestcatcher.com"
}'

Receiving files

Since version 0.0.8 it's implemented a new method to receive files, if some message is received as a file, PDF, Image or Audio your webhook will be fired containing an base64 encoded file in that format.

data:[<media type>][;charset=<character set>][;base64],<data>

But to became more aasy for who doenst know how to handle files in base64 format this library will save received files into his own cdn, so when messages contain a file, body of mesage will have an filelink address, and you can just access and get your file from there. These files as saved on /public/cdn/ and you can manage there if you preffer.

For users running into docker containers it's important realize of storage files as deletes every time you rebuild the container, for persist these files you need to create Volumes.

Security

To avoid issues with use of this library, it's not allowed start conversations, you only can answer who already talked to you. I can't garantee your number will not be blocked from WhatsApp - You need to be aware of the use you will be making of this tool, be aware you are only responsible for each and every use of this library.

Remember, be blocked from WhatsApp can be unreversible.

Contributing

I'm looking forward to seeing what you can build out of it, so if you create something using this library, please let me know. If you develop something interesting we will be waiting for your PULL REQUEST.

License

MIT

Legal

This code is in no way affiliated with, authorized, maintained, sponsored or endorsed by WhatsApp or any of its affiliates or subsidiaries. This is an independent and unofficial software. Use at your own risk.

Cryptography Notice

This distribution includes cryptographic software. The country in which you currently reside may have restrictions on the import, possession, use, and/or re-export to another country, of encryption software. BEFORE using any encryption software, please check your country's laws, regulations and policies concerning the import, possession, or use, and re-export of encryption software, to see if this is permitted. See http://www.wassenaar.org/ for more information.

The U.S. Government Department of Commerce, Bureau of Industry and Security (BIS), has classified this software as Export Commodity Control Number (ECCN) 5D002.C.1, which includes information security software using or performing cryptographic functions with asymmetric algorithms. The form and manner of this distribution makes it eligible for export under the License Exception ENC Technology Software Unrestricted (TSU) exception (see the BIS Export Administration Regulations, Section 740.13) for both object code and source code.

whats2api's People

Contributors

joaomirandas avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

whats2api's Issues

How to run w2api in AWS EC2 Ubuntu 18

Greetings, this is the way to configure w2api on Ubuntu 18.0 on AWS EC2

You must first install the necessary dependencies:

sudo apt install -y gconf-service libasound2 libatk1.0-0 libc6 libcairo2 libcups2 libdbus-1-3 libexpat1 libfontconfig1 libgcc1 libgconf-2-4 libgdk-pixbuf2.0-0 libglib2.0-0 libgtk-3-0 libnspr4 libpango-1.0-0 libpangocairo-1.0-0 libstdc++6 libx11-6 libx11-xcb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 libxext6 libxfixes3 libxi6 libxrandr2 libxrender1 libxss1 libxtst6 ca-certificates fonts-liberation libappindicator1 libnss3 lsb-release xdg-utils wget xvfb

Clone the w2api repository to the directory you want

git clone https://github.com/joaomirandasa/w2api

run npm install

To finish, type this command in terminal:

xvfb-run node debug.js

after a while the api stops responding.

I deployed this api on my digitalocean server and after a few hours it stops sending messages when I make the request. it gives the status: true but does not send.

404 not found

Hi, I am configuring w2api on AWS EC2 Linux Ami instance with NVM Node v 10.19.

When I start the application everything seems normal, but when looking for the URL http: // localhost: 8000/83430 / qrcode I get a 404 error and I can't find the path.

This is my configuration:

name : W2API
author : João Vitor Miranda Santos
version : 0.0.1

//WhatsApp Configurations
instance : 83430
webhook : https://abc123.requestcatcher.com/test
token : ugictxugo7gugif
masterKey : igi7icufjf

How check received message

How I check if people send me any message?
How I check if people send like a "Hi" and the api send Hello (automatic)

SendFile more than one file

It's possible send more than one file or is better make a foreach ( each file and make 3 or 4 requests).
Thx

Wrong token authentication

Everytime i tried to send a messge it is saying wrong token authentication.
{
"status": false,
"err": "Wrong token authentication"
}

//WhatsApp Configurations
instance : 22139
webhook : https://villaments.win
token : j19ksi1mim1lksm12213
masterKey : w2apiisthebestlibrary

Session is invalid, Retrying

Just after scanning QrCode it shows

✔️WAPI Reinjected
❌ Session is invalid, Retrying

and then it again restarts the script i guess from starting and again it prompts to scan Qrcode
also shows

Protocol error (Runtime.callFunctionOn): Session closed. Most likely the page has been closed. Error: Protocol error (Runtime.callFunctionOn): Session closed. Most likely the page has been closed.

i Tried Updating the Whats2api using

  1. sudo npm i @open-wa/wa-automate
  2. sudo npm update @open-wa/wa-automate

even updated npm , puppeteer and node to latest version

but it was not updating

it shows

Update available 1.9.105 → 2.0.25
Run npm i @open-wa/wa-automate to update

please , how to fix it

I Appreciate your work

Thank-you
Ketan

Error on saving file

good afternoon brother I am having a very important detail and I hope you help me when I receive an image and I have the phone connected to the api in the console it gives me this error "#Error on saving file" and it gives me a base 64 text string but the same when I send it to s3 it sends me a super small file and the image is blurred any information regarding this bug?

Webhook Not Working

Posts are not been posted to the webhook, I tested with requestcatcher.com

Send contact is not defined

I download the library and run with >node debug.js and i get the error:

sendContact is not defined.

What i'm doing wrong?

image send error

CMD CURL: curl --location --request POST 'localhost:8000/83430/sendFile?token=j19ksi1mim1lksm12213' \
--header 'Content-Type: application/json' \
--data-raw '{
	"phone":"[email protected]",
	"body": "https://upload.wikimedia.org/wikipedia/ru/3/33/NatureCover2001.jpg",
	"filename": "NatureCover2001.jpg",
	"caption": "W2API - Best REST API for WhatsApp"
}'

Output:
======= 2020-05-20 13:56:13: Error: Evaluation failed: t Error: Evaluation failed: t
at ExecutionContext._evaluateInternal (/home/daniel/Desktop/whats2api-master/node_modules/puppeteer/lib/ExecutionContext.js:102:19)
at process._tickCallback (internal/process/next_tick.js:68:7)
-- ASYNC --
at ExecutionContext. (/home/daniel/Desktop/whats2api-master/node_modules/puppeteer/lib/helper.js:94:19)
at DOMWorld.evaluate (/home/daniel/Desktop/whats2api-master/node_modules/puppeteer/lib/DOMWorld.js:89:24)
at process._tickCallback (internal/process/next_tick.js:68:7)
-- ASYNC --
at Frame. (/home/daniel/Desktop/whats2api-master/node_modules/puppeteer/lib/helper.js:94:19)
at Page.evaluate (/home/daniel/Desktop/whats2api-master/node_modules/puppeteer/lib/Page.js:591:14)
at Page. (/home/daniel/Desktop/whats2api-master/node_modules/puppeteer/lib/helper.js:95:27)
at Client. (/home/daniel/Desktop/whats2api-master/node_modules/@open-wa/wa-automate/dist/api/Client.js:430:50)
at step (/home/daniel/Desktop/whats2api-master/node_modules/@open-wa/wa-automate/dist/api/Client.js:44:23)
at Object.next (/home/daniel/Desktop/whats2api-master/node_modules/@open-wa/wa-automate/dist/api/Client.js:25:53)
at /home/daniel/Desktop/whats2api-master/node_modules/@open-wa/wa-automate/dist/api/Client.js:19:71
at new Promise ()
at __awaiter (/home/daniel/Desktop/whats2api-master/node_modules/@open-wa/wa-automate/dist/api/Client.js:15:12)
at Client.sendImage (/home/daniel/Desktop/whats2api-master/node_modules/@open-wa/wa-automate/dist/api/Client.js:427:16)
-- ASYNC --
at Page. (/home/daniel/Desktop/whats2api-master/node_modules/puppeteer/lib/helper.js:94:19)
at Client. (/home/daniel/Desktop/whats2api-master/node_modules/@open-wa/wa-automate/dist/api/Client.js:430:50)
at step (/home/daniel/Desktop/whats2api-master/node_modules/@open-wa/wa-automate/dist/api/Client.js:44:23)
at Object.next (/home/daniel/Desktop/whats2api-master/node_modules/@open-wa/wa-automate/dist/api/Client.js:25:53)
at /home/daniel/Desktop/whats2api-master/node_modules/@open-wa/wa-automate/dist/api/Client.js:19:71
at new Promise ()
at __awaiter (/home/daniel/Desktop/whats2api-master/node_modules/@open-wa/wa-automate/dist/api/Client.js:15:12)
at Client.sendImage (/home/daniel/Desktop/whats2api-master/node_modules/@open-wa/wa-automate/dist/api/Client.js:427:16)
at Client. (/home/daniel/Desktop/whats2api-master/node_modules/@open-wa/wa-automate/dist/api/Client.js:481:33)
at step (/home/daniel/Desktop/whats2api-master/node_modules/@open-wa/wa-automate/dist/api/Client.js:44:23)

I tried update npm, all repos and the same error :/

send message to non-contact

Folks,
I did not find in the documentation how we do to send messages to people that I never had any conversation on whats.
when i try to send it to someone tell me the message on the console * not a contact *.
is there how to do this?
@joaomirandasa

profilePic

Bom dia desde a atualização da versão do wa_automate não estou conseguindo pegar o profilePic.
Alguma orientação?

Reload "config" file

I'm using docker to run multiple instances, it's possible to reload changes in config file using some command or webhook?

Áudio recebido <data>: undefined

Percebi que ao receber o áudio o data está indefinido:

{'messages': [{'id': '[email protected]_XXXXX', 'body': 'data:audio/ogg; codecs=opus;base64,undefined', 'filelink': None, 'fromMe': False, 'self': 0, 'isForwarded': False, 'author': '[email protected]', 'time': 1589756239, 'chatId': '[email protected]', 'type': 'ptt', 'caption': None, 'quotedMsgBody': None, 'chatName': '+55 XXXXXXXXX'}], 'instanceId': 83430}

authentication error

authentication error, nothing happens after scanning the code and when executing another request it tells me that there is a token error

{"status": false, "err": "Wrong token authentication"}

How to send line breaks in messages?

Do not send \n
Ex: test\ntest

`<?php

$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => "localhost:8000/83430/sendMessage?token=j19ksi1mim1lksm12213",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS =>"{\n\t"phone":"55998888888",\n\t"body": "W2API - Best REST \n API for WhatsApp"\n}",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json"
),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

Return 400: Bad Request
`

Wrong token authentication on Docker

RUN
> docker build -t joaomirandas/whats2api .

RUN
> docker run --name instance1 -p 8001:8000 joaomirandas/whats2api AND
> docker run --name instance2 -p 8002:8000 joaomirandas/whats2api

both instance1 & instance2 can show on browser and barcode show on http://localhost:8001/instance1/qrcode and http://localhost:8002/instance2/qrcode

when i call via postman
http://localhost:8001/instance1/sendMessage?token=j19ksi1mim1lksm12213 AND
http://localhost:8002/instance2/sendMessage?token=j19ksi1mim1lksm12213

{
    "status": false,
    "err": "Wrong token authentication"
}

No change on config file, any suggest ?

thanks

BROKEN METHODS DETECTED

After scanning the qr, I get this message:

!!!BROKEN METHODS DETECTED!!!

Please update to the latest version: 1.9.46 2.2021.3

Add this to the body of the issue:
[ 'Store.WidFactory.createUserWid', 'Store.WidFactory.createWid' ]

!!!!!BROKEN METHODS DETECTED!!!!

Cannot send message to unsaved contact

hello, sorry if i miss the documentation,
but i have problem to send any message to contact who unlisted in my whatsapp contact

sorry for my bad in english

thank before

Phone Number Format

Hi,

i already try to test your bot, just want to ask regarding the phone number format, what is the phone number format if i want to send the messages?because i tried using +62(Indonesian Country Code) and try using 0xxx but the messages isn't send but the response status is "true".

Thanks

sendFile don't work (version 0.0.8)

Hi, i am not able to send any file using sendFile, even using the link example in the documents. The message after POST is “status:true”, but any file is sent.
Someone have an idea about this problem?

Webhook message is like below:
{"ack":[{"id":"[email protected]_3EB0120F98202769298F","chatId":"[email protected]","status":"viewed"}],"instanceId":83430}
only have "status":"viewed" message, there isn`t "sent" nor "delivered"

PostMan Collection Not UptoDate

The postman collection is currently not up to date with the library, there are some api available in postman collection that are no longer provided by the library again.

Some routes not working. Unable to send messages, set status etc.

Hi,

Really amazed to see your work. Hats off!!!

I was finally able to run this successfully and tested some routes via postman.

Battery level, getchatbyid, etc worked flawlessly. However, there are some which I would want to use in particular.

I am unable to send message, send files, set status etc. I am getting Response 200 OK and the following :

{
    "status": true
}

However, none of these requests actually had any effect.

Authentication Error

After scanning code it stuck to Authenticate to continue ✓ Loading QR and stuck

404 not found

Oi Joao Miranda, estou com problemas para enviar mensagens , a instancia conecta, eu conecto o qr code mas ele não envia, já tentei os testes pelo postman tudo, pode me ajudar 34 9.92525666 Filipe

receive messages?

noticed that I can know that the chat has unread messages, but how do I receive those messages?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.