0

I am creating a chabot in dialogflow, my goal is to create a ticket in zendesk with the parameters of dialogflow by calling a webhook. When the ticket is created I must return to the chat the folio number with which it was created., my code already works, I call the webhook and I create the ticket in Zendesk and I have a folio number. my problem is that when I call the function to create the ticket in zendesk, it does not wait for the ticket to be finished and returns me to chat zero as a folio number, then in the console I see that if the ticket was created and the Folio number that should have been sent to the chat. How do I wait for the ticket to be created?

  "use strict";
  const express = require("express");
  const bodyParser = require("body-parser");
  const restService = express();
  var zendesk = require('node-zendesk');
  var nfolio = 0;
  restService.use(
    bodyParser.urlencoded({
      extended: true
    })
  );
  restService.use(bodyParser.json());
  restService.post("/echo", function(req, res) {
    var serial = req.body.queryResult.parameters['serial'];
    var problem = req.body.queryResult.parameters['problem'];
    var email = req.body.queryResult.parameters['email'];
    var subject = req.body.queryResult.parameters['subject'];
    var description = req.body.queryResult.parameters['description'];    
    createTicket(serial);
    console.log("xxx" + nfolio);
    console.log(req.body.queryResult.parameters);
    console.log(email);
    console.log(req.body.queryResult);
    var speech =  "Folio: ."+ nfolio;
    return res.json({

    "fulfillmentText": speech,
    "fulfillmentMessages": [
      {
        "text": {
          "text": [speech]
        }
      }
    ],
    "source": "<webhookpn1>"
    });
  });

  restService.listen(process.env.PORT || 80, function() {
    console.log("Server up and listening");
  });

function createTicket(nserie) {

var client = zendesk.createClient({
username:  'XXXXXXXX',
token:     'XXXXXXXXX',
remoteUri: 'https://XXXXX.zendesk.com/api/v2'
});
nfolio = 88;
/* client.users.list(function (err, req, result) {
if (err) {
    console.log(err);
    return;
}
console.log(JSON.stringify(result[0], null, 2, true));//gets the first page 
});
*/

var ticket = {
"ticket":
    {
    "subject":"TESTING CREATING TICKET NODE JS IGNORAR!" + nserie,
    "comment": {
        "body": "The smoke is very colorful."
    }
    }
};


client.tickets.create(ticket,  function(err, req, result) {
if (err)  {
    return handleError(err);
} else {
    console.log(result.subject);
    console.log(result.id);  
    nfolio = result.id;
    console.log(" folio  " + nfolio);
  //  console.log(JSON.stringify(result, null, 2, true));//gets the first page
  //  return(result.id);
    }
    }

); 

}

0 Answers0