ProGrammer avatar

How To Send Message TO WhatsApp Using JavaScript/Node.js-- Twilio

pakgamer

Published: 20 Jun 2020 › Updated: 20 Jun 2020How To Send Message TO WhatsApp Using JavaScript/Node.js-- Twilio

How To Send Message TO WhatsApp Using JavaScript/Node.js-- Twilio

Hello everyone

In this post, we are going to learn how to send a message to WhatsApp using JavaScript/node.js. In this video, we will send some messages to my WhatsApp number through a small piece of code. We are going to use Twilio Node Library. This will help us connect our code to the WhatsApp. So let's begin the tutorial.

  • First of all, if you don't have the Nodejs installed please install it from the official site.

  • Now go to the Twilio website and create a free account.

image.png
After creating the account login to your account and click on the three dots.

image.png

Now click on the programmable SMS .

image.png

Click on the whatsapp beta and then activate the sanbox

image.png

Now send the given code to the given number in order to connect your WhatsApp number.
image.png

You will get a message like this after sending the code to the number.

image.png

Now go back to the main page of Twilio and copy the following keys.

image.png

Open VS Code and install the Twilio library by running the following command.
npm install twilio
Wait for the installation and then copy-paste the below code to a newly .js file

const accountSid =YOUR_SID;
const authToken = YOUR_KEY;
const client = require('twilio')(accountSid, authToken);



 client.messages
      .create({
         from: 'whatsapp:+14155238886',
         body: 'Hello from nodejs',
         to: RECIVER-NUMBER
       })
      .then(message => console.log(message.sid));

Assign the keys to the variables.

image.png

In my case, I have created a JSON file and everything ting is in the JSON. Just for privacy purpose you can just assign them to the variables.

Let's run the script now

image.png

If your message is sent successfully you should get output like this
image.png

As you can see the messages are successfully sent. If you want to change the message just change the text in body and rerun the code.

image.png

I think this much is enough for today. Now if you like the post please upvote and comment if you want to give me some advise
ntitled-1.png

Leave How To Send Message TO WhatsApp Using JavaScript/Node.js-- Twilio to:

Written by

Loves Coding, Gaming, Photography and LIFE

Read more #hive-148441 posts


Best Posts From ProGrammer

We have not curated any of pakgamer's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.

More Posts From ProGrammer