How to Create a WhatsApp Chatbot in Node.js to Provide Quick Academic Assistance

How to Create a WhatsApp Chatbot in Node.js to Provide Quick Academic Assistance

How to Create a WhatsApp Chatbot in Node.js to Provide Quick Academic Assistance

PLEASE COMMENT IF YOU ARE INTERESTED FOR A VIDEO VERSION ON YOUTUBE CHANNEL

Creating a WhatsApp chatbot that can scrape answers with step by step solutions from doubtnut.com is a great way to provide quick and convenient academic assistance to students. In this tutorial, we will use Node.js, Twilio's API for WhatsApp, and the Doubtnut.com web scraping API to create a chatbot that can answer academic questions.

Here are the steps to create a WhatsApp chatbot in Node.js that can scrape answers with step by step solutions from doubtnut.com:

Step 1: Set up your Twilio account

To get started, you'll need a Twilio account. Sign up for a free account at twilio.com and follow the prompts to verify your phone number and create a new WhatsApp Sandbox.

Step 2: Set up your Node.js environment

Next, you'll need to set up a Node.js environment on your local machine. Download and install Node.js from nodejs.org, then create a new project folder.

Step 3: Install the required dependencies

Open a terminal window, navigate to your project folder, and run the following command to install the required dependencies:

npm install twilio axios cheerio dotenv

Step 4: Create a new .env file

Create a new file called .env in your project folder and add the following variables:

codeTWILIO_ACCOUNT_SID=your_account_sid
TWILIO_AUTH_TOKEN=your_auth_token
DOUBTNUT_API_KEY=your_api_key

Replace your_account_sid, your_auth_token, and your_api_key with the corresponding values from your Twilio and Doubtnut.com accounts.

Step 5: Create a new index.js file

Create a new file called index.js in your project folder and add the following code:

require('dotenv').config();
const twilio = require('twilio');
const axios = require('axios');
const cheerio = require('cheerio');

const client = twilio(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN);

const sendWhatsappMessage = (from, to, body) => {
  client.messages.create({
    from: `whatsapp:${from}`,
    to: `whatsapp:${to}`,
    body,
  }).then((message) => {
    console.log(`Message sent: ${message.sid}`);
  }).catch((error) => {
    console.error(`Error sending message: ${error}`);
  });
};

const doubtnutAPI = axios.create({
  baseURL: 'https://api.doubtnut.com/api/v1',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': `Token ${process.env.DOUBTNUT_API_KEY}`,
  },
});

const scrapeQuestionAnswer = async (imageUrl) => {
  try {
    const { data: { data } } = await doubtnutAPI.post('/image-query', {
      input_data: {
        image_url: imageUrl,
      },
    });

    const answerUrl = data.answers[0].video_solution.video_url;

    const { data: html } = await axios.get(answerUrl);

    const $ = cheerio.load(html);

    const steps = [];

    $('div.step').each((index, element) => {
      const text = $(element).find('span.text').text();
      steps.push(text);
    });

    const solution = steps.join('\n');

    return solution;
  } catch (error) {
    console.error(`Error scraping question answer: ${error}`);
    return null;
  }
};

const handleMessage = async (message) => {
  const { From: from, Body: body, MediaUrl0: imageUrl } = message;

  if (imageUrl) {
    const solution= await scrapeQuestionAnswer(imageUrl);

if (solution) {
  sendWhatsappMessage('your_twilio_number', from, solution);
} else {
  sendWhatsappMessage('your_twilio_number', from, 'Sorry, I could not find an answer to your question.');
}
} else {
sendWhatsappMessage('your_twilio_number', from, 'Please send a photo of an academic question.');
}
};

const express = require('express');
const app = express();

app.use(express.urlencoded({ extended: false }));

app.post('/whatsapp', (req, res) => {
handleMessage(req.body);
res.sendStatus(200);
});

app.listen(3000, () => {
console.log('Server is listening on port 3000');
});

Replace your_twilio_number with your Twilio Sandbox number.

Step 6: Test your chatbot

Run the following command in your terminal to start your server:

node index.js

Next, send a photo of an academic question to your Twilio Sandbox number on WhatsApp. If everything is set up correctly, your chatbot should respond with the answer and step-by-step solution scraped from doubtnut.com.

Congratulations, you have successfully created a WhatsApp chatbot in Node.js that can scrape answers with step-by-step solutions from doubtnut.com!

Did you find this article valuable?

Support Akash Srinivasan by becoming a sponsor. Any amount is appreciated!