Untitled

 avatar
unknown
plain_text
2 years ago
3.2 kB
1
Indexable
// See https://github.com/dialogflow/dialogflow-fulfillment-nodejs
// for Dialogflow fulfillment library docs, samples, and to report issues
'use strict';
 
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion} = require('dialogflow-fulfillment');
const axios = require('axios');
 
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
 
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  const agent = new WebhookClient({ request, response });
  console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
  console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
 
  function welcome(agent) {
    agent.add(`Welcome to my agent!`);
  }
 
  function fallback(agent) {
    return new Promise((resolve, reject) => {
      const queryText = request.body.queryResult.queryText;
      axios.post('https://sheetdb.io/api/v1/cc5lerg5sq2k7?sheet=failed', {
          "data": {
            "content": queryText
          }
      });

      agent.add(`Connector chưa hiểu lắm`);
      agent.add(`Bạn có thể thử lại không?`);
    });
  }
  
  function userinput(agent) {
    console.log('Now handeling userinput...');
    
    let partner = agent.parameters.partner;
    let product = agent.parameters.product;
    let url = agent.parameters.url;
    let email = agent.parameters.email;

    console.log(agent.parameters);
    console.log(agent);
    
    let data_rows = [{
      "Partner": partner,
      "Product": product,
      "URL": url,
      "Email": email,
      "Created": new Date()
    }];
   
    return new Promise((resolve, reject) => {
      axios.get(`https://sheetdb.io/api/v1/cc5lerg5sq2k7/search?name=*${product}*`).then(function(res) {
      	let found = res.data[0];
        
        if (found) {
          axios.post('https://sheetdb.io/api/v1/cc5lerg5sq2k7?sheet=dialogflow_Input', {
            "data": data_rows
          }).catch(error => {
            console.log("Error:");
            console.log(error);
            reject();
          });
        
          agent.add("Cám ơn bạn. Chúng tôi sẽ gửi bạn danh sách kết nối đến " + email);
        } else {
          agent.add("Rất tiếc. Connector hiện không có " + product + " trong dữ liệu. Thông tin của bạn đã được lưu lại để chung tôi connect khi có dữ liệu");
          axios.post('https://sheetdb.io/api/v1/cc5lerg5sq2k7?sheet=Failed', {
            "data": data_rows
          }).then(response => {
              console.log("Response:" + response);
          }).catch(error => {
              console.log("Error:");
              console.log(error);
              reject();
          });
        }
        
        resolve();
      });
    });
  }
  // Run the proper function handler based on the matched Dialogflow intent name
  let intentMap = new Map();
  //intentMap.set('Default Welcome Intent', welcome);
  //intentMap.set('Default Fallback Intent', fallback);
  intentMap.set('connect', userinput);

  agent.handleRequest(intentMap);
});
Editor is loading...