1

I am developing a chatbot in urdu with wit.ai framework. Chatbot works fine but the issue is when i leave the chatbot for sometime after asking some questions, it start answering the previously told answers in a sequence by starting from first answer to the last one.it means it repeats a sentence but only once. when all the answers are resent, then chatbot does not send them again. I a using Pusher API for realtime responses.I am adding here things in the code which i am not sure about like i don't know what is the purpose of using cors.

Here is some part of my server.js file.

const cors = require('cors');    

app.post('/chat', (req, res) => {
  const { message } = req.body;

  const responses = {
    helloo: ["ہیلو! میں امید کرتا ہوں کہ آپ خیریت سے ہیں", ],
  };

  const firstEntityValue = (entities, entity) => {
    const val =
      entities &&
      entities[entity] &&
      Array.isArray(entities[entity]) &&
      entities[entity].length > 0 &&
      entities[entity][0].value;

    if (!val) {
      return null;
    }

    return val;
  };

  const handleMessage = ({ entities }) => {

    const helloo = firstEntityValue(entities, 'hello');

    if(helloo){
      return pusher.trigger('bot', 'bot-response', {
          message:
            responses.helloo[Math.floor(Math.random() * responses.helloo.length)
            ],
        });
    }   

    return pusher.trigger('bot', 'bot-response', {
      message: "میں معزرت خواہ ہوں۔ ",
    });
  };

  client
    .message(message)
    .then(data => {
      handleMessage(data);
    })
    .catch(error => console.log(error));
});

All this code works fine but i don't know where the issue is that chatbot answers the same answer twice.i am new to javascript and react also. i don't see a loop here which gives answer twice. kindly help me solve the issue. https://drive.google.com/drive/folders/1TxbqAz_Hfv9bpgY60fLf5TVXwyux_fcI This is the drive link to my project.

0 Answers0