To utilize the WhatsApp cloud API with Node.js and Express to send messages, you will need to follow these steps:
npm install twilio
.const accountSid = 'YOUR_ACCOUNT_SID';
const authToken = 'YOUR_AUTH_TOKEN';
const client = require('twilio')(accountSid, authToken);
const fromNumber = 'whatsapp:' + YOUR_WHATSAPP_SENDER_NUMBER;
app.post('/whatsapp', (req, res) => {
const { body, from } = req.body;
const message = {
body: 'Hello! This is a WhatsApp message sent using Node.js and the Twilio API.',
from: fromNumber,
to: from,
};
client.messages.create(message)
.then(() => {
console.log('Message sent successfully!');
res.status(200).send('Message sent successfully!');
})
.catch((err) => {
console.error(err);
res.status(500).send('Error sending message: ' + err);
});
});
Start your Express server using the listen
method and specify the port for the server to listen on.
To send a WhatsApp message, make a HTTP POST request to the Express route you created in step 6, passing the recipient phone number and message in the request body.
That's it! You should now be able to send WhatsApp messages using the WhatsApp cloud API and Node.js with Twilio.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2022-03-04 11:00:00 +0000
Seen: 8 times
Last updated: Mar 08 '23
How can the counter value on RabbitMQ be lowered?
What is the method of focusing on a specific ESLint error message in the scripts of my package.json?
What does the message "CopyPlistFile duplicated task in Xcode 10" signify?
How can I compose an email with HTML formatting using TextBox?
What does the error message "Import 'pandas' could not be resolved from source Pylance" mean?