Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

BotkitConversation not executing when using axios but its executing fine when calling inside request? #2228

Open
samsonmarandi opened this issue Sep 22, 2022 · 0 comments

Comments

@samsonmarandi
Copy link

samsonmarandi commented Sep 22, 2022

If I have not provide enough information, please do reply.
First I have added the code which is not executing properly which is using axios to get data from the api, after that at last I have added the same code which is working fine which uses request to get data from the api.

My goal is to make my code look cleaner by using axios

helpers/apicall.js file
`
const axios = require("axios");

module.exports = {
checkContext: async(data) => {

const options = {
  url: `${process.env.API_URL}/help`,
  data: data,
  headers: {
    'Content-Type': 'application/json'
  },
  method: "POST",
  // timeout: 1000 * 20
};

const response = await axios(options);
return response;

}
}
features/main.js file
const apiCall = require('../helpers/apicall');

module.exports = function (controller) {
controller.on('message,direct_message', async (bot, message) => {
console.log('i heard something')

  const question = { question: message.text }
  const res = await apiCall.checkContext(question);
    console.log(res.status)
    if (res.status === 200) {
      if(res.data.actionCode==='1') {
        await bot.changeContext(message.reference)
        await bot.reply(message, `Hey <@${message.user}>! ${res.data.response}`);
        await bot.beginDialog("addFile");
      }
      else if(res.data.actionCode==='4') {
        await bot.changeContext(message.reference)
        await bot.reply(message, `Hey <@${message.user}>! ${res.data.response}`);
        await bot.beginDialog("testFileUpload");
      }
    }
    else {
      await bot.changeContext(message.reference)
      await bot.reply(message, `Server resonded with error ${res.status}. Please contact your admin`);
    }
});

}

features/functions.js file

module.exports = function(controller) {
const addFile = new BotkitConversation('addFile', controller);

addFile.ask('Please upload your file.', async (answer, addfile, bot, message) => {

console.log('adding file')
try{
  if(answer!==null){
    console.log('canceled all dialogs')
    // await bot.cancelAllDialogs();
    console.log('this is message.text',message.text)
    const question = { question: answer }
    const res = await apiCall.checkContext(question);
    if(res.status===200){
      console.log(res.data)
    if(res.data.actionCode==='1'){
      await bot.changeContext(message.reference)
      await bot.reply(message, `${res.data.response}`)
      await bot.beginDialog("addFile");
    }
        else if(res.data.actionCode==='4') {
        await bot.changeContext(message.reference)
        console.log('user is ',message.user)
        await bot.reply(message, `Hey <@${message.user}>! ${res.data.response}`);

        await bot.beginDialog("testFileUpload");
      }
    else {
      await bot.changeContext(message.reference)
        await bot.reply(message, `You entered a wrong command`);
    }
  }
  }
  else {
     //body 
  }
}
catch (e){
  console.log(`some error occured - ${e}`)
}

}, { key: 'cccapation' });

controller.addDialog(addFile);

const testFileUpload = new BotkitConversation('testFileUpload', controller);

testFileUpload.ask('Waiting for a file', async (answer, convo, bot, message) => {
try{

console.log('printing answer')
console.log(answer)
// if(answer!==null && message.files===undefvar){
//   await bot.cancelAllDialogs();
// }
if(answer!==null){
  await bot.cancelAllDialogs();

  const question = { question: answer }
  const res = await apiCall.checkContext(question);

  if (res.status === 200) {
    console.log('status 200')
            if(res.data.actionCode==='1'){
      await bot.changeContext(message.reference)
      await bot.reply(message, `${res.data.response}`)
      await bot.beginDialog("addFile");
    }
        else if(res.data.actionCode==='4') {
          console.log('intiating create test')
        await bot.changeContext(message.reference)
        await bot.reply(message, `Hey <@${message.user}>! ${res.data.response}`);

        await bot.beginDialog("testFileUpload");
      }
    else {
      await bot.changeContext(message.reference)
        await bot.reply(message, `You entered a wrong command`);
    }
    
  }
  else {
    console.log('response was not 200')
  }
}
else {
  //body 
}
}
catch(error){
  console.log('error occured',error)
}

}, { key: 'qna' });

controller.addDialog(testFileUpload);
}
`
When this code is exeuted let's say first addFile is exucuted by user command main.js was the entry point which is normal.
But the problem arise when a user wants to change conversation from addFile to testFileUpload by typing the command
only the question is executed which is present in testFileUpload.ask(Waiting for a file) but the body never executes even when a user uploads the file or enter any commands using keyboard instead conversation ends without the execution of the body and the code in main.js file executes when the code inside conversation body was supposed to execute.

Below I have added the code which is working fine but the code is very bulky.
This code is working fine. When a user want to change conversation from addFile to testFileUpload and vice versa, every code executes fine without any problem
features/main.js
`
var request = require('request');

module.exports = function (controller) {
controller.on('message,direct_message', async (bot, message) => {
console.log('i heard something')

  const options_to_check = {
  method: 'POST',
  url: `${process.env.API_URL}/help`,
  headers: {
    Accept: '*/*',
    'User-Agent': 'Thunder Client (https://www.thunderclient.com)',
    'Content-Type': 'application/json'
  },
  body: { question: message.text },
  json: true
};

  request(options_to_check, async function(error, response, body) {
    if (response.statusCode === 200) {
      if(response.body.actionCode==='1') {
        await bot.changeContext(message.reference)
        await bot.reply(message, `Hey <@${message.user}>! ${response.body.response}`);

        await bot.beginDialog("addFile");
      }
      else if(response.body.actionCode==='4') {

        await bot.changeContext(message.reference)
        await bot.reply(message, `Hey <@${message.user}>! ${response.body.response}`);
        // await bot.beginDialog("createQuestions");
        await bot.beginDialog("testFileUpload");
      }
    }
    else {
      await bot.changeContext(message.reference)
      await bot.reply(message, `Server resonded with error ${response.statusCode}. Please contact your admin`);
    }
  });
  console.log('no error')
    
});

}
`

features/functions.js

`
module.exports = function(controller) {
const addFile = new BotkitConversation('addFile', controller);

addFile.ask('Please upload your file.', async (answer, addFile, bot, message) => {

console.log('adding file')
try{
  if(answer!==null){
    console.log('canceled all dialogs')
    await bot.cancelAllDialogs();
    const options_to_check = {
  method: 'POST',
  url: `${process.env.API_URL}/help`,
  headers: {
    Accept: '*/*',
    'User-Agent': 'Thunder Client (https://www.thunderclient.com)',
    'Content-Type': 'application/json'
  },
  body: { question: answer },
  json: true
};

    request(options_to_check, async function(error, response, body) {
    if (response.statusCode === 200) {
      if(response.body.actionCode==='1') {
        await bot.changeContext(message.reference)
        await bot.reply(message, `Hey <@${message.user}>! ${response.body.response}`);

        await bot.beginDialog("addFile");
      }
      else if(response.body.actionCode==='4') {
        await bot.changeContext(message.reference)
        await bot.reply(message, `Hey <@${message.user}>! ${response.body.response}`);
        await bot.beginDialog("testFileUpload");
      }
    }
    else {
      await bot.changeContext(message.reference)
      await bot.reply(message, `Server resonded with error ${response.statusCode}. Please contact your admin`);
    }
  });
  }
  else {
   //body
  }
}
catch (e){
  console.log(`some error occured - ${e}`)
}

}, { key: 'cccapation' });

controller.addDialog(addFile);

const testFileUpload = new BotkitConversation('testFileUpload', controller);

testFileUpload.ask('Waiting for a file', async (answer, convo, bot, message) => {
try{
convo.setVar('testname', '')

console.log('printing answer')
console.log(answer)
// if(answer!==null && message.files===undefvar){
//   await bot.cancelAllDialogs();
// }
if(answer!==null){
  await bot.cancelAllDialogs();

  const options_to_check = {
  method: 'POST',
  url: `${process.env.API_URL}/help`,
  headers: {
    Accept: '*/*',
    'User-Agent': 'Thunder Client (https://www.thunderclient.com)',
    'Content-Type': 'application/json'
  },
  body: { question: answer },
  json: true
};

  request(options_to_check, async function(error, response, body) {
    if (response.statusCode === 200) {
      if(response.body.actionCode==='1') {
        await bot.changeContext(message.reference)
        await bot.reply(message, `Hey <@${message.user}>! ${response.body.response}`);

        await bot.beginDialog("addFile");
      }
      else if(response.body.actionCode==='4') {

        await bot.changeContext(message.reference)
        await bot.reply(message, `Hey <@${message.user}>! ${response.body.response}`);
        // await bot.beginDialog("createQuestions");
        await bot.beginDialog("testFileUpload");
      }
    }
    else {
      await bot.changeContext(message.reference)
      await bot.reply(message, `Server resonded with error ${response.statusCode}. Please contact your admin`);
    }
  });
}
else {
  //body
}
}
catch(error){
  console.log('error occured',error)
}

}, { key: 'qna' });
}
`

@samsonmarandi samsonmarandi changed the title How to change the input of the user in this BotkitConversation? BotkitConversation not executing when using axios but its executing fine when calling inside request? Oct 14, 2022
@samsonmarandi samsonmarandi reopened this Oct 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant