index.js
/*
* Function to handle v1 webhook requests from Dialogflow
*/
Function to handle v1 webhook requests from Dialogflow
Function to handle v1 webhook requests from Dialogflow
function processV1Request (request, response) {
  let action = request.body.result.action; 
  let parameters = request.body.result.parameters; 
  let inputContexts = request.body.result.contexts; 
  let requestSource = (request.body.originalRequest) ? request.body.originalRequest.source : undefined;
  const googleAssistantRequest = 'google'; 
  const app = new DialogflowApp({request: request, response: response});
  const actionHandlers = {
    'input.welcome': () => {
      if (requestSource === googleAssistantRequest) {
        sendGoogleResponse('Hello, Welcome to my Dialogflow agent!'); 
      } else {
        sendResponse('Hello, Welcome to my Dialogflow agent!'); 
      }
    },
    'input.unknown': () => {
      if (requestSource === googleAssistantRequest) {
        sendGoogleResponse('I\'m having trouble, can you try that again?'); 
      } else {
        sendResponse('I\'m having trouble, can you try that again?'); 
      }
    },
    'Topics': () => {
      var topic = request.body.result.parameters.Topics;
      var tech = request.body.result.parameters.technology;
	  var number = request.body.result.parameters.number;
	  if ( number != null && tech == 'analytics' ) {
		  sendResponse('Analytics has overall *465* applications, are you looking for some particluar application?');
	  }
      var appcenter = 'https://www.sapappcenter.com/apps/10611#!overview';
	  var details = 'https://www.infosys.com/SAP/offerings/Documents/SAP-analytics-custom-extensions.pdf';
      if (userip_tech === 'analytics') {
        sendResponse('The appcenter link that you have requested is: ' + appcenter);
      } else {
        sendResponse('The appcenter link that you have requested is: ' + appcenter + '		  More details can be found in ' + details ); 
      }
    },
    'default': () => {
      if (requestSource === googleAssistantRequest) {
        let responseToUser = {
          speech: 'This message is from Dialogflow\'s Cloud Functions for Firebase editor!', 
          text: 'This is from Dialogflow\'s Cloud Functions for Firebase editor! :-)' 
        };
        sendGoogleResponse(responseToUser);
      } else {
        let responseToUser = {
          speech: 'This message is from Dialogflow\'s Cloud Functions for Firebase editor!', 
          text: 'This is from Dialogflow\'s Cloud Functions for Firebase editor! :-)' 
        };
        sendResponse(responseToUser);
      }
    }
  };
  if (!actionHandlers[action]) {
    action = 'default';
  }
  actionHandlers[action]();
  function sendGoogleResponse (responseToUser) {
    if (typeof responseToUser === 'string') {
      app.ask(responseToUser); 
    } else {
      let googleResponse = app.buildRichResponse().addSimpleResponse({
        speech: responseToUser.speech || responseToUser.displayText,
        displayText: responseToUser.displayText || responseToUser.speech
      });
      if (responseToUser.googleRichResponse) {
        googleResponse = responseToUser.googleRichResponse;
      }
      if (responseToUser.googleOutputContexts) {
        app.setContext(...responseToUser.googleOutputContexts);
      }
      console.log('Response to Dialogflow (AoG): ' + JSON.stringify(googleResponse));
      app.ask(googleResponse); 
    }
  }
  function sendResponse (responseToUser) {
    if (typeof responseToUser === 'string') {
      let responseJson = {};
      responseJson.speech = responseToUser; 
      responseJson.displayText = responseToUser; 
      response.json(responseJson); 
    } else {
      let responseJson = {};
      responseJson.speech = responseToUser.speech || responseToUser.displayText;
      responseJson.displayText = responseToUser.displayText || responseToUser.speech;
      responseJson.data = responseToUser.data;
      responseJson.contextOut = responseToUser.outputContexts;
      console.log('Response to Dialogflow: ' + JSON.stringify(responseJson));
      response.json(responseJson); 
    }
  }
}
function processV1Request ( request , response ) { let action = request . body . result . action ; let parameters = request . body . result . parameters ; let inputContexts = request . body . result . contexts ; let requestSource = ( request . body . originalRequest ) ? request . body . originalRequest . source : undefined ; const googleAssistantRequest = 'google' ; const app = new DialogflowApp ( { request : request , response : response } ) ; const actionHandlers = { 'input.welcome' : ( ) => { if ( requestSource === googleAssistantRequest ) { sendGoogleResponse ( 'Hello, Welcome to my Dialogflow agent!' ) ; } else { sendResponse ( 'Hello, Welcome to my Dialogflow agent!' ) ; } } , 'input.unknown' : ( ) => { if ( requestSource === googleAssistantRequest ) { sendGoogleResponse ( 'I\'m having trouble, can you try that again?' ) ; } else { sendResponse ( 'I\'m having trouble, can you try that again?' ) ; } } , 'Topics' : ( ) => { var topic = request . body . result . parameters . Topics ; var tech = request . body . result . parameters . technology ; var number = request . body . result . parameters . number ; if ( number != null && tech == 'analytics' ) { sendResponse ( 'Analytics has overall *465* applications, are you looking for some particluar application?' ) ; } var appcenter = 'https://www.sapappcenter.com/apps/10611#!overview' ; var details = 'https://www.infosys.com/SAP/offerings/Documents/SAP-analytics-custom-extensions.pdf' ; if ( userip_tech === 'analytics' ) { sendResponse ( 'The appcenter link that you have requested is: ' + appcenter ) ; } else { sendResponse ( 'The appcenter link that you have requested is: ' + appcenter + '		  More details can be found in ' + details ) ; } } , 'default' : ( ) => { if ( requestSource === googleAssistantRequest ) { let responseToUser = { speech : 'This message is from Dialogflow\'s Cloud Functions for Firebase editor!' , text : 'This is from Dialogflow\'s Cloud Functions for Firebase editor! :-)' } ; sendGoogleResponse ( responseToUser ) ; } else { let responseToUser = { speech : 'This message is from Dialogflow\'s Cloud Functions for Firebase editor!' , text : 'This is from Dialogflow\'s Cloud Functions for Firebase editor! :-)' } ; sendResponse ( responseToUser ) ; } } } ; if ( ! actionHandlers [ action ] ) { action = 'default' ; } actionHandlers [ action ] ( ) ; function sendGoogleResponse ( responseToUser ) { if ( typeof responseToUser === 'string' ) { app . ask ( responseToUser ) ; } else { let googleResponse = app . buildRichResponse ( ) . addSimpleResponse ( { speech : responseToUser . speech || responseToUser . displayText , displayText : responseToUser . displayText || responseToUser . speech } ) ; if ( responseToUser . googleRichResponse ) { googleResponse = responseToUser . googleRichResponse ; } if ( responseToUser . googleOutputContexts ) { app . setContext ( ... responseToUser . googleOutputContexts ) ; } console . log ( 'Response to Dialogflow (AoG): ' + JSON . stringify ( googleResponse ) ) ; app . ask ( googleResponse ) ; } } function sendResponse ( responseToUser ) { if ( typeof responseToUser === 'string' ) { let responseJson = { } ; responseJson . speech = responseToUser ; responseJson . displayText = responseToUser ; response . json ( responseJson ) ; } else { let responseJson = { } ; responseJson . speech = responseToUser . speech || responseToUser . displayText ; responseJson . displayText = responseToUser . displayText || responseToUser . speech ; responseJson . data = responseToUser . data ; responseJson . contextOut = responseToUser . outputContexts ; console . log ( 'Response to Dialogflow: ' + JSON . stringify ( responseJson ) ) ; response . json ( responseJson ) ; } } }
Function to handle v1 webhook requests from Dialogflow
Function to handle v1 webhook requests from Dialogflow
// https://dialogflow.com/docs/actions-and-parameters // https://dialogflow.com/docs/actions-and-parameters // https://dialogflow.com/docs/contexts // Constant to identify Google Assistant requests // Create handlers for Dialogflow actions as well as a 'default' handler // The default welcome intent has been matched, welcome the user (https://dialogflow.com/docs/events#default_welcome_intent) // Use the Actions on Google lib to respond to Google requests; for other requests use JSON // Send simple response to user // Send simple response to user // The default fallback intent has been matched, try to recover (https://dialogflow.com/docs/intents#fallback_intents) // Use the Actions on Google lib to respond to Google requests; for other requests use JSON // Send simple response to user // Send simple response to user // Topics Intent // Use the Actions on Google lib to respond to Google requests; for other requests use JSON // Send simple response to user // Default handler for unknown or undefined actions // Use the Actions on Google lib to respond to Google requests; for other requests use JSON //googleRichResponse: googleRichResponse, // Optional, uncomment to enable //googleOutputContexts: ['weather', 2, { ['city']: 'rome' }], // Optional, uncomment to enable // spoken response // displayed response //data: richResponsesV1, // Optional, uncomment to enable //outputContexts: [{'name': 'weather', 'lifespan': 2, 'parameters': {'city': 'Rome'}}], // Optional, uncomment to enable // spoken response // displayed response // If undefined or unknown action use the default handler // Run the proper handler function to handle the request from Dialogflow // Function to send correctly formatted Google Assistant responses to Dialogflow which are then sent to the user // Google Assistant response // If speech or displayText is defined use it to respond // Optional: Overwrite previous response with rich response // Optional: add contexts (https://dialogflow.com/docs/contexts) // Send response to Dialogflow and Google Assistant // Function to send correctly formatted responses to Dialogflow which are then sent to the user // if the response is a string send it as a response to the user // spoken response // displayed response // Send response to Dialogflow // If the response to the user includes rich responses or contexts send them to Dialogflow // If speech or displayText is defined, use it to respond (if one isn't defined use the other's value) // Optional: add rich messages for integrations (https://dialogflow.com/docs/rich-messages) // Optional: add contexts (https://dialogflow.com/docs/contexts) // Send response to Dialogflow //send the data /*function toappcenter(topics, ) { 
	/*ROS-bridge endpoint*/ /*var WebSocket = require('ws'); 

	/*webSocket Cloud options*/ /*var ws = new WebSocket('wss://ws-broadcast-server-d062507.cfapps.eu10.hana.ondemand.com'); 
 
	ws.on('open', function open() { 
		console.log("connected to HANA websocket");
		ws.send(speech); 
	}); 
  }*/