dialogflow-nodejs-client icon indicating copy to clipboard operation
dialogflow-nodejs-client copied to clipboard

Why are the responses different between onsite agent and Node?

Open DirkWolthuis opened this issue 9 years ago • 9 comments

Why does api.ai does not register intent when I ask a question via my node app (FB-messengerbot)? If I ask the same question via the agent on the website it does register the intent.

DirkWolthuis avatar Jun 05 '16 21:06 DirkWolthuis

Hi,

Can you send me example of code and intent information? Which language you use?

sstepashka avatar Jun 08 '16 06:06 sstepashka

Hi. I'm using Dutch as the language. Stuff like dates are working in the onsite agent. If I use for example 12 juli, it works even if I haven't added that exact date. But via my Facebook Messenger bots I does not recognize the prompt input.

This is a other example of differences: screen shot 2016-06-11 at 13 02 20 screen shot 2016-06-11 at 13 02 24

I don't think it has anything to do with my code as it just sends the feedback form your API.

DirkWolthuis avatar Jun 11 '16 11:06 DirkWolthuis

Hi, @DirkWolthuis Do you use custom bot implementation or start the bot from api.ai?

If you ran it on Heroku, please send me list of the app parameters. It should contain APIAI_LANG parameter with value nl.

Thanks, Danil.

xVir avatar Jun 14 '16 09:06 xVir

Hi, sorry for the late reaction. I don't use a custom implementation. I made a video to show you the differences. And I will post the code from the bot here. I hope you can help me to identify the problem.

video: https://goo.gl/photos/7cTSuS1am5xBHiNu8

var token = "TOKEN";
var express = require('express');
var app = express();
var http = require('http');
var https = require('https');
var fs = require('fs');
var options = {
      cert: fs.readFileSync('/etc/letsencrypt/live/pannenkoekenbootdenbosch.nl/cert.pem'),
      key: fs.readFileSync('/etc/letsencrypt/live/pannenkoekenbootdenbosch.nl/privkey.pem'),
      ca: fs.readFileSync('/etc/letsencrypt/live/pannenkoekenbootdenbosch.nl/chain.pem')
    };
var server = https.createServer(options, app);
var bodyParser = require('body-parser');
var request = require('request');
var apiai = require('apiai');
var ai = apiai("CODE");
var nodemailer = require('nodemailer');
var response;

//nodemailer stuff
var transporter = nodemailer.createTransport('PASSWORD');

//everything inside the app is automatic parsed
app.use(bodyParser.json());

//setting up connection with page and bot
app.get('/bot/', function (req, res) {
  if (req.query['hub.verify_token'] === 'TOKEN') {
    res.send(req.query['hub.challenge']);
  }
  res.send('Error, wrong validation token');
})

//defining port to listen
server.listen(3000, function(){
  console.log("server running at 3000")
});

//main loop for handeling msg's
//from facebook https://developers.facebook.com/docs/messenger-platform/quickstart
app.post('/bot/', function (req, res) {
  messaging_events = req.body.entry[0].messaging;
  for (i = 0; i < messaging_events.length; i++) {
    event = req.body.entry[0].messaging[i];
    sender = event.sender.id;
    if (event.message && event.message.text) {
      //if there is a new msg do stuff
      text = event.message.text;
      console.log("Incomming msg: " + text);

      //api ai stuff
      //send text to apiai
      var requestAI = ai.textRequest(text);

      requestAI.on('response', function(response) {
          //check if action is completed, if so send e-mail
          reservationFinished(response);

          //send facebook sender the response

          if(response.result.fulfillment.speech == null || response.result.fulfillment.speech == undefined || response.result.fulfillment.speech == ''){

            response = 'Dat begrijp ik helaas niet.';

            sendTextMessage(sender, response);

          }

          else {
            response = response.result.fulfillment.speech;

            sendTextMessage(sender, response);

          }


      });

      requestAI.on('error', function(error) {
          console.log(error);
      });

      requestAI.end();

    }
  }
  res.sendStatus(200);
});

//function to send back text-msg
function sendTextMessage(sender, text) {
  messageData = {
    text:text
  }
  request({
    url: 'https://graph.facebook.com/v2.6/me/messages',
    qs: {access_token:token},
    method: 'POST',
    json: {
      recipient: {id:sender},
      message: messageData,
    }
  }, function(error, response, body) {
    if (error) {
      console.log('Error sending message: ', error);
    } else if (response.body.error) {
      console.log('Error: ', response.body.error);
    }
  });
}

function reservationFinished(response){

  if (response.result.action === "make.reservation" && response.result.actionIncomplete === false){

    var mailOptions = {
    from: response.result.parameters.name + '<' + response.result.parameters.email +'>',  // sender address
    replyTo: response.result.parameters.email,
    to: '[email protected]', //'[email protected]', // list of receivers
    subject: 'Reservering Pannenkoekenboot via bot op naam: ' + response.result.parameters.name, // Subject line
    text: 'Name: ' + response.result.parameters.name + 'Datum: ' + response.result.parameters.date + 'Personen: ' + response.result.parameters.personen + 'Email: ' + response.result.parameters.email , // plaintext body
    html: '<p>Name: ' + response.result.parameters.name + '</p><p>Datum: ' + response.result.parameters.date +'</p><p>Personen: ' + response.result.parameters.personen + '</p><p>Email: ' + response.result.parameters.email + '</p>' // html body
  };

    // send mail with defined transport object
    transporter.sendMail(mailOptions, function(error, info){
        if(error){
            return console.log(error);
        }
        console.log('Message sent: ' + info.response);
    });

  }

}

DirkWolthuis avatar Jun 16 '16 10:06 DirkWolthuis

Hi, @DirkWolthuis I don't see language parameter in your code.

Could you please try to change

var ai = apiai("CODE");

to

var ai = apiai("CODE", {language: "nl"});

Language parameter is required for non-English agents.

xVir avatar Jun 16 '16 12:06 xVir

Thanks! That solved it. Thanks! Hmm, but know the stop command does not work. Is there a dutch alternative?

DirkWolthuis avatar Jun 16 '16 12:06 DirkWolthuis

Do you mean phrase for resetting parameters? "Cancel" word also not working?

xVir avatar Jun 16 '16 12:06 xVir

Nope also not working.

DirkWolthuis avatar Jun 16 '16 12:06 DirkWolthuis

Ok, let me check this case. Thanks.

xVir avatar Jun 16 '16 13:06 xVir