如何从Node.js向Watson Assistant发送响应

时间:2018-07-24 08:22:24

标签: node.js watson-conversation

我想从node.js应用程序向Watson Assistant发送响应。 在数据库中注册了投诉数据之后,将向客户发送一条消息:您的索赔号...已被注册。 当前,请求的数据存储在数据库中,但未发送响应。 代码是:


function updateMessage(input, response) {
   return new Promise(function(resolve, reject) {
        var responseText = null;  
        var a = response.intents;
        a.forEach(function(entry) {
        console.log(entry);
        if (entry.intent==="Confirmer")
        {         
            // console.log(response);
            console.log(response.context);

            var num_contrat = response.context.num_contrat;
            var compagnie = response.context.compagnie;
            var prenom = response.context.prenom;
            var nom = response.context.nom;
            var obj_reclamation = response.context.obj_reclamation;
            obj_reclamation = obj_reclamation.replace(/'/g, '\\\'');
            nom = nom.replace(/'/g, '\\\'');

            con.connect(function(err) {
                if (err) throw err;
                console.log("Connecté !");
                var sql = "INSERT INTO reclamation (num_contrat, compagnie, nom, prenom, reclamation_mot_justif) VALUES ('"+num_contrat+"', '"+compagnie+"','"+nom+"','"+prenom+"','"+obj_reclamation+"')";
                con.query(sql, function (err, result) {
                    if (err) throw err;
                        console.log("1 enregistrement a été inséré dans la base de donnée");
                        console.log("ID = ",result.insertId); // dernier ID inséré
                        responseText = "Votre réclamation N° " + result.insertId +" a été bien enregistrée.";
                        console.log("responseText = ", responseText);
                        response.output.text = responseText;
                        console.log("response.output.text = ", response.output.text);
                        console.log(response);
                        //response.output.text.push(responseText);
                        resolve(response);
                    });       
                });             
            }
        });


      if (!response.output) {
      response.output = {};
    } else if (response.intents[0] === undefined) {
      resolve(response);
    }       

      else if (response.intents[0].intent === "discovery" || response.output.text == '' ) {

      responseText = sendToDiscovery(input.input.text);
      responseText.then(function(responseText) { 
          response.output.text = responseText;       
         resolve(response);
      });
    } else {
      resolve(response);
    }
  });
}

谢谢。

0 个答案:

没有答案