4th Street Bar Hive-Bar

Hive-Bar Powered by Hive beta-fdb5b5b

Community post

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](https://images.hive.blog/DQmd3WcHH8i6U2JkmnrvEvd4FwjDQxJtG3Tnhzxdjswktdx/ntitled-1.png)
102 upvotes $5.48

Replies (2)

Review before signing

Posting as . Signing with . Keychain permission: Posting. Hive Keychain will ask you to approve this action next.


  
Technical details

Operation fingerprint: