Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. const functions = require('firebase-functions');
  4. const {WebhookClient} = require('dialogflow-fulfillment');
  5. const {Card, Suggestion, Payload} = require('dialogflow-fulfillment');
  6. const axios = require('axios');
  7. const moment = require('moment');
  8. const slugify = require('slugify');
  9.  
  10. process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
  11.  
  12. exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  13.  
  14.     const agent = new WebhookClient({ request, response });
  15.  
  16.     async function getListOfTownshipOrResidence(agent) {
  17.         let config = {
  18.             "message": "Please choose the following Township/Residences that you would like to enquire.",
  19.             "platform": "kommunicate",
  20.             "metadata": {
  21.                 "contentType": "300",
  22.                 "templateId": "7",
  23.                 "payload": {
  24.                     "headerText": "List Of Property/Residence",
  25.                     "elements": [],
  26.                 }
  27.             }
  28.         };
  29.  
  30.         await axios.get('http://www.amock.io/api/megaworld/townships')
  31.             .then(response => {
  32.                 response.data.data.forEach((property) => {
  33.                     config.metadata.payload.elements.push({
  34.                         "imgSrc": property.image,
  35.                         "description": property.description,
  36.                         "title": property.name,
  37.                         "action": {
  38.                             "type": "quick_reply",
  39.                             "text": property.name
  40.                         }
  41.                     })
  42.                 });
  43.                 return response.data.data;
  44.             })
  45.             .catch(error => {
  46.                 console.log(error.response)
  47.             });
  48.  
  49.         agent.add(new Payload("PLATFORM_UNSPECIFIED", [config]));
  50.     }
  51.  
  52.     async function storeAppointment(agent) {
  53. // console.log('name', agent.getContext('awaiting_name').parameters.name);
  54. // console.log('email', agent.getContext('awaiting_email').parameters.email);
  55. // console.log('contact number', agent.getContext('awaiting_contact_number').parameters.contact_number);
  56. // console.log('datetime', agent.getContext('awaiting_datetime').parameters.datetime.date_time);
  57. // console.log('property', agent.getContext('awaiting_datetime').parameters.property);
  58.  
  59.         let name = agent.getContext('awaiting_name').parameters.name;
  60.         let email = agent.getContext('awaiting_email').parameters.email;
  61.         let phoneNumber = agent.getContext('awaiting_contact_number').parameters.contact_number;
  62.         let datetime = agent.getContext('awaiting_datetime').parameters.datetime;
  63.         let appointmentDate = moment(datetime).format('YYYY-MM-DD HH:mm:ss');
  64.         let property = agent.getContext('awaiting_datetime').parameters.property;
  65.  
  66.         let inputs = {
  67.             type: 'appointment',
  68.             name: name,
  69.             email: email,
  70.             contact_number: phoneNumber,
  71.             appointment_date: appointmentDate,
  72.             message: 'Appointment for ' + property,
  73.             own_megaworld_property: 0
  74.         };
  75.  
  76.         await axios.post('http://34.87.118.115/appointment', inputs).then(response => {
  77.  
  78.             return response;
  79.  
  80.         }).catch(error => {
  81.             console.log('error', error.response);
  82.             agent.add(`Sorry! We are having some error.`);
  83.         });
  84.  
  85.         agent.add(`Thank you. You should receive a confirmation email.`);
  86.     }
  87.  
  88.     async function getResidences(agent) {
  89.         let config = {
  90.             "message": "Please choose one of residence you would like to know?",
  91.             "platform": "kommunicate",
  92.             "metadata": {
  93.                 "contentType": "300",
  94.                 "templateId": "10",
  95.                 "payload": []
  96.             }
  97.         };
  98.  
  99.         await axios.get('https://testapi.io/api/cupin06/megaworld/residences')
  100.             .then(response => {
  101.                 response.data.data.forEach((residence) => {
  102.                     config.metadata.payload.push({
  103.                         "title": residence.name,
  104.                         "header": {
  105.                             "imgSrc": residence.image
  106.                         },
  107.                         "description": residence.desc,
  108.                         "buttons": [
  109.                             {
  110.                                 "name": "Choose",
  111.                                 "action": {
  112.                                     "type": "quickReply",
  113.                                     "payload": {
  114.                                         "message": residence.name,
  115.                                         "replyMetadata": {
  116.                                             "id": residence.id
  117.                                         }
  118.                                     }
  119.                                 }
  120.                             }
  121.                         ]
  122.                     })
  123.                 });
  124.                 return response.data.data;
  125.             })
  126.             .catch(error => {
  127.                 console.log(error.response)
  128.             });
  129.  
  130.         agent.add(new Payload("PLATFORM_UNSPECIFIED", [config]));
  131.     }
  132.  
  133.     async function displayResidenceLocation(agent) {
  134.         let config = {
  135.             "message": "Please choose one of residence you would like to know?",
  136.             "platform": "kommunicate",
  137.             "metadata": {
  138.                 "contentType": "300",
  139.                 "templateId": "10",
  140.                 "payload": []
  141.             }
  142.         };
  143.  
  144.         await axios.get('https://testapi.io/api/cupin06/megaworld/residences')
  145.             .then(response => {
  146.                 response.data.data.forEach((residence) => {
  147.                     config.metadata.payload.push({
  148.                         "title": residence.name,
  149.                         "header": {
  150.                             "imgSrc": residence.image
  151.                         },
  152.                         "description": residence.desc,
  153.                         "buttons": [
  154.                             {
  155.                                 "name": "Choose",
  156.                                 "action": {
  157.                                     "type": "quickReply",
  158.                                     "payload": {
  159.                                         "message": residence.name,
  160.                                         "replyMetadata": {
  161.                                             "id": residence.id
  162.                                         }
  163.                                     }
  164.                                 }
  165.                             }
  166.                         ]
  167.                     })
  168.                 });
  169.                 return response.data.data;
  170.             })
  171.             .catch(error => {
  172.                 console.log(error.response)
  173.             });
  174.  
  175.         agent.add(new Payload("PLATFORM_UNSPECIFIED", [config]));
  176.     }
  177.  
  178.     async function displayResidenceAmenities(agent) {
  179.         let inputs = agent.getContext('residenceinfo-followup').parameters;
  180.  
  181.         let residenceName = slugify(inputs.residence).toLowerCase();
  182.  
  183.         await axios.get('https://testapi.io/api/cupin06/megaworld/residences')
  184.             .then(response => {
  185.                 let residence = response.data.data;
  186.  
  187.                 let config = {
  188.                     "message": "List of amenities of " + residence.name,
  189.                     "platform": "kommunicate",
  190.                     "metadata": {
  191.                         "contentType": "300",
  192.                         "templateId": "7",
  193.                         "payload": {
  194.                             "headerImgSrc": residence.image,
  195.                             "headerText": residence.name,
  196.                             "elements": [],
  197.                         }
  198.                     }
  199.                 };
  200.  
  201.                 residence.amenities.forEach((amenity) => {
  202.                     config.metadata.payload.elements.push({
  203.                         "title": amenity,
  204.                     });
  205.                 });
  206.                 return response.data.data;
  207.             })
  208.             .catch(error => {
  209.                 console.log(error.response)
  210.             });
  211.  
  212.         agent.add(new Payload("PLATFORM_UNSPECIFIED", [config]));
  213.     }
  214.  
  215.     let intentMap = new Map();
  216.     intentMap.set('UserProvidePhoneNumber', getListOfTownshipOrResidence);
  217.     intentMap.set('SaveAppointment', storeAppointment);
  218.     intentMap.set('residence.info', getResidences);
  219.     intentMap.set('ResidenceLocation', displayResidenceLocation);
  220.     intentMap.set('ResidenceAmenities', displayResidenceAmenities);
  221.  
  222.     agent.handleRequest(intentMap);
  223. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement