Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- const functions = require('firebase-functions');
- const {WebhookClient} = require('dialogflow-fulfillment');
- const {Card, Suggestion, Payload} = require('dialogflow-fulfillment');
- const axios = require('axios');
- const moment = require('moment');
- const slugify = require('slugify');
- process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
- exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
- const agent = new WebhookClient({ request, response });
- async function getListOfTownshipOrResidence(agent) {
- let config = {
- "message": "Please choose the following Township/Residences that you would like to enquire.",
- "platform": "kommunicate",
- "metadata": {
- "contentType": "300",
- "templateId": "7",
- "payload": {
- "headerText": "List Of Property/Residence",
- "elements": [],
- }
- }
- };
- await axios.get('http://www.amock.io/api/megaworld/townships')
- .then(response => {
- response.data.data.forEach((property) => {
- config.metadata.payload.elements.push({
- "imgSrc": property.image,
- "description": property.description,
- "title": property.name,
- "action": {
- "type": "quick_reply",
- "text": property.name
- }
- })
- });
- return response.data.data;
- })
- .catch(error => {
- console.log(error.response)
- });
- agent.add(new Payload("PLATFORM_UNSPECIFIED", [config]));
- }
- async function storeAppointment(agent) {
- // console.log('name', agent.getContext('awaiting_name').parameters.name);
- // console.log('email', agent.getContext('awaiting_email').parameters.email);
- // console.log('contact number', agent.getContext('awaiting_contact_number').parameters.contact_number);
- // console.log('datetime', agent.getContext('awaiting_datetime').parameters.datetime.date_time);
- // console.log('property', agent.getContext('awaiting_datetime').parameters.property);
- let name = agent.getContext('awaiting_name').parameters.name;
- let email = agent.getContext('awaiting_email').parameters.email;
- let phoneNumber = agent.getContext('awaiting_contact_number').parameters.contact_number;
- let datetime = agent.getContext('awaiting_datetime').parameters.datetime;
- let appointmentDate = moment(datetime).format('YYYY-MM-DD HH:mm:ss');
- let property = agent.getContext('awaiting_datetime').parameters.property;
- let inputs = {
- type: 'appointment',
- name: name,
- email: email,
- contact_number: phoneNumber,
- appointment_date: appointmentDate,
- message: 'Appointment for ' + property,
- own_megaworld_property: 0
- };
- await axios.post('http://34.87.118.115/appointment', inputs).then(response => {
- return response;
- }).catch(error => {
- console.log('error', error.response);
- agent.add(`Sorry! We are having some error.`);
- });
- agent.add(`Thank you. You should receive a confirmation email.`);
- }
- async function getResidences(agent) {
- let config = {
- "message": "Please choose one of residence you would like to know?",
- "platform": "kommunicate",
- "metadata": {
- "contentType": "300",
- "templateId": "10",
- "payload": []
- }
- };
- await axios.get('https://testapi.io/api/cupin06/megaworld/residences')
- .then(response => {
- response.data.data.forEach((residence) => {
- config.metadata.payload.push({
- "title": residence.name,
- "header": {
- "imgSrc": residence.image
- },
- "description": residence.desc,
- "buttons": [
- {
- "name": "Choose",
- "action": {
- "type": "quickReply",
- "payload": {
- "message": residence.name,
- "replyMetadata": {
- "id": residence.id
- }
- }
- }
- }
- ]
- })
- });
- return response.data.data;
- })
- .catch(error => {
- console.log(error.response)
- });
- agent.add(new Payload("PLATFORM_UNSPECIFIED", [config]));
- }
- async function displayResidenceLocation(agent) {
- let config = {
- "message": "Please choose one of residence you would like to know?",
- "platform": "kommunicate",
- "metadata": {
- "contentType": "300",
- "templateId": "10",
- "payload": []
- }
- };
- await axios.get('https://testapi.io/api/cupin06/megaworld/residences')
- .then(response => {
- response.data.data.forEach((residence) => {
- config.metadata.payload.push({
- "title": residence.name,
- "header": {
- "imgSrc": residence.image
- },
- "description": residence.desc,
- "buttons": [
- {
- "name": "Choose",
- "action": {
- "type": "quickReply",
- "payload": {
- "message": residence.name,
- "replyMetadata": {
- "id": residence.id
- }
- }
- }
- }
- ]
- })
- });
- return response.data.data;
- })
- .catch(error => {
- console.log(error.response)
- });
- agent.add(new Payload("PLATFORM_UNSPECIFIED", [config]));
- }
- async function displayResidenceAmenities(agent) {
- let inputs = agent.getContext('residenceinfo-followup').parameters;
- let residenceName = slugify(inputs.residence).toLowerCase();
- await axios.get('https://testapi.io/api/cupin06/megaworld/residences')
- .then(response => {
- let residence = response.data.data;
- let config = {
- "message": "List of amenities of " + residence.name,
- "platform": "kommunicate",
- "metadata": {
- "contentType": "300",
- "templateId": "7",
- "payload": {
- "headerImgSrc": residence.image,
- "headerText": residence.name,
- "elements": [],
- }
- }
- };
- residence.amenities.forEach((amenity) => {
- config.metadata.payload.elements.push({
- "title": amenity,
- });
- });
- return response.data.data;
- })
- .catch(error => {
- console.log(error.response)
- });
- agent.add(new Payload("PLATFORM_UNSPECIFIED", [config]));
- }
- let intentMap = new Map();
- intentMap.set('UserProvidePhoneNumber', getListOfTownshipOrResidence);
- intentMap.set('SaveAppointment', storeAppointment);
- intentMap.set('residence.info', getResidences);
- intentMap.set('ResidenceLocation', displayResidenceLocation);
- intentMap.set('ResidenceAmenities', displayResidenceAmenities);
- agent.handleRequest(intentMap);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement