Open Futurekheed opened 4 months ago
mkdir whatsapp-bot cd whatsapp-bot npm init -y npm install express twilio body-parser const express = require('express'); const bodyParser = require('body-parser'); const twilio = require('twilio');
const app = express(); const port = process.env.PORT || 3000;
const accountSid = 'your_twilio_account_sid'; const authToken = 'your_twilio_auth_token'; const client = twilio(accountSid, authToken);
app.use(bodyParser.urlencoded({ extended: false }));
app.post('/whatsapp', (req, res) => { const incomingMessage = req.body.Body; const from = req.body.From;
let responseMessage = 'Hello!';
if (incomingMessage.toLowerCase() === 'hi') { responseMessage = 'Hi there! How can I help you today?'; }
client.messages .create({ body: responseMessage, from: 'whatsapp:+14155238886', // Twilio sandbox number to: from }) .then(message => console.log(message.sid)) .catch(err => console.error(err));
res.sendStatus(200); });
app.listen(port, () => { console.log(Server running on port ${port}); });
Server running on port ${port}
Mkdir
mkdir whatsapp-bot cd whatsapp-bot npm init -y npm install express twilio body-parser const express = require('express'); const bodyParser = require('body-parser'); const twilio = require('twilio');
const app = express(); const port = process.env.PORT || 3000;
const accountSid = 'your_twilio_account_sid'; const authToken = 'your_twilio_auth_token'; const client = twilio(accountSid, authToken);
app.use(bodyParser.urlencoded({ extended: false }));
app.post('/whatsapp', (req, res) => { const incomingMessage = req.body.Body; const from = req.body.From;
let responseMessage = 'Hello!';
if (incomingMessage.toLowerCase() === 'hi') { responseMessage = 'Hi there! How can I help you today?'; }
client.messages .create({ body: responseMessage, from: 'whatsapp:+14155238886', // Twilio sandbox number to: from }) .then(message => console.log(message.sid)) .catch(err => console.error(err));
res.sendStatus(200); });
app.listen(port, () => { console.log(
Server running on port ${port}
); });