Skip to main content

carousel code for google assistant

'use strict';

const express = require('express');
const bodyParser = require('body-parser');
const NodeCache = require("node-cache");
var HashMap = require('hashmap');
//var courses = require('./index');
var chromeCast = require('./test.js');
const opn = require('opn');
let ActionsSdkApp = require('actions-on-google').ActionsSdkApp;


process.env.DEBUG = 'actions-on-google:*';
const App = require('actions-on-google').DialogflowApp;
const {
    WebhookClient,
    Payload
} = require('dialogflow-fulfillment');

var ytc = require('./testy2');
var search = require('./search');

const restService = express();
restService.use(bodyParser.json());
const {
    BasicCard
} = require('actions-on-google') //use v2 alpha

const DialogflowApp = require('actions-on-google').DialogflowApp; // Google Assistant helper library

const googleAssistantRequest = 'google'; // Constant to identify Google Assistant requests

restService.post('/look', function(request, response) {
    console.log('look request');
    start(request, response);
});

restService.listen((process.env.PORT || 4000), function() {
    console.log("Server listening 4000");
});


// Construct rich response for Google Assistant
const app = new DialogflowApp();

function start(request, response) {
    // An action is a string used to identify what needs to be done in fulfillment
    let action = request.body.result.action; // https://dialogflow.com/docs/actions-and-parameters

    const agent = new WebhookClient({
        request,
        response
    });


    const userQuery = request.body.result.resolvedQuery;
    console.log(userQuery);
    // Parameters are any entites that Dialogflow has extracted from the request.
    const parameters = request.body.result.parameters; // https://dialogflow.com/docs/actions-and-parameters
    var params = parameters;
    var sessionID = request.body.sessionId;

    // getting meta data of the response
    const metadata = request.body.result.metadata;
    const curentIntentName = metadata.intentName;

    // Contexts are objects used to track and store conversation state
    const inputContexts = request.body.result.contexts; // https://dialogflow.com/docs/contexts

    // Get the request source (Google Assistant, Slack, API, etc) and initialize DialogflowApp
    const requestSource = (request.body.originalRequest) ? request.body.originalRequest.source : undefined;
    const app = new DialogflowApp({
        request: request,
        response: response
    });

    // Create handlers for Dialogflow actions as well as a 'default' handler
    const actionHandlers = {
        // The default welcome intent has been matched, welcome the user (https://dialogflow.com/docs/events#default_welcome_intent)
        'input.welcome': () => {
            sendResponse('Hello, Welcome to enterprise tiger BOT!'); // Send simple response to user
        },
        // The default fallback intent has been matched, try to recover (https://dialogflow.com/docs/intents#fallback_intents)
        'input.unknown': () => {
            let responseToUser = {
                googleRichResponse: multiRichResponse, // Optional, uncomment to enable
                //googleOutputContexts: ['weather', 2, { ['city']: 'rome' }], // Optional, uncomment to enable
                speech: 'This message is from Dialogflow\'s Cloud Functions for Firebase editor!', // spoken response
                text: 'This is from Dialogflow\'s Cloud Functions for Firebase editor! :-)' // displayed response
            };
            sendGoogleResponse(responseToUser);
        },
        // Default handler for unknown or undefined actions
        'default': () => {
            // Use the Actions on Google lib to respond to Google requests; for other requests use JSON
            let responseToUser = {
                googleRichResponse: multiRichResponse, // Optional, uncomment to enable
                //googleOutputContexts: ['weather', 2, { ['city']: 'rome' }], // Optional, uncomment to enable
                speech: 'This message is from Dialogflow\'s Cloud Functions for Firebase editor!', // spoken response
                text: 'This is from Dialogflow\'s Cloud Functions for Firebase editor! :-)' // displayed response
            };
            sendGoogleResponse(responseToUser);
        },
        'question': () => {
            // search.findVideoAndTime(userQuery, function(err, videoId , starttime){
            //   console.log("videoId is " + videoId + " starttime is "+ starttime);
            //   if(startime-5 > 0){
            //     startime = startime -5;
            //   }
            //   ytc.youtubecast(videoId,startime);
            // });
            search.findVideoAndTime(userQuery, function(err, top3VideosDetails) {
                //var reply = videoResult(top3VideosDetails)

              //  console.log("top3 videoa details : \n\n"+ JSON.stringify(top3VideosDetails[0]) + " type \n "+ JSON.parse(top3VideosDetails[0]).title);
              //  var topvideoResult =  getGoogleRichRespo(top3VideosDetails);
              //  console.log("top : "+ topvideoResult);
              // const app = new ActionsSdkApp({request, response});
              // // carousel(app);
              //   let responseToUser = {
              //       googleRichResponse: cor, // Optional, uncomment to enable
              //       //googleOutputContexts: ['weather', 2, { ['city']: 'rome' }], // Optional, uncomment to enable
              //       speech: 'This message is from Dialogflow\'s Cloud Functions for Firebase editor!', // spoken response
              //       text: 'This is from Dialogflow\'s Cloud Functions for Firebase editor! :-)' // displayed response
              //   };


                //sendGoogleResponse(responseToUser);


            //    const app = new ActionsSdkApp({request, response});
                // app.ask({
                //   speech: 'Howdy! I can tell you fun facts about ' +
                //   'almost any number, like 42. What do you have in mind?',
                //   displayText: 'Howdy! I can tell you fun facts about almost any ' +
                //   'number. What do you have in mind?'
                // });

            });

            app.askWithCarousel('Alright! Here are a few things you can learn. Which sounds interesting?',
        // Build a carousel
        app.buildCarousel()
        // Add the first item to the carousel
        .addItems(app.buildOptionItem('MATH_AND_PRIME',
          ['math', 'math and prime', 'prime numbers', 'prime'])
          .setTitle('Math & prime numbers')
          .setDescription('42 is an abundant number because the sum of its ' +
            'proper divisors 54 is greater…')
          .setImage('http://example.com/math_and_prime.jpg', 'Math & prime numbers'))
        // Add the second item to the carousel
        .addItems(app.buildOptionItem('EGYPT',
          ['religion', 'egpyt', 'ancient egyptian'])
          .setTitle('Ancient Egyptian religion')
          .setDescription('42 gods who ruled on the fate of the dead in the ' +
            'afterworld. Throughout the under…')
          .setImage('http://example.com/egypt', 'Egypt')
        )
        // Add third item to the carousel
        .addItems(app.buildOptionItem('RECIPES',
          ['recipes', 'recipe', '42 recipes'])
          .setTitle('42 recipes with 42 ingredients')
          .setDescription('Here\'s a beautifully simple recipe that\'s full ' +
            'of flavor! All you need is some ginger and…')
          .setImage('http://example.com/recipe', 'Recipe')
        )
      );

        },
    };

    // If undefined or unknown action use the default handler
    if (!actionHandlers[action]) {
        action = 'default';
    }

    // Run the proper handler function to handle the request from Dialogflow
    actionHandlers[action]();

    // Function to send correctly formatted Google Assistant responses to Dialogflow which are then sent to the user
    function sendGoogleResponse(responseToUser) {
        if (typeof responseToUser === 'string') {
            app.ask(responseToUser); // Google Assistant response
        } else {
            // If speech or displayText is defined use it to respond
            let googleResponse = app.buildRichResponse().addSimpleResponse({
                speech: responseToUser.speech || responseToUser.displayText,
                displayText: responseToUser.displayText || responseToUser.speech
            });
            // Optional: Overwrite previous response with rich response
            if (responseToUser.googleRichResponse) {
                googleResponse = responseToUser.googleRichResponse;
            }
            // Optional: add contexts (https://dialogflow.com/docs/contexts)
            if (responseToUser.googleOutputContexts) {
                app.setContext(...responseToUser.googleOutputContexts);
            }
            console.log('Response to Dialogflow (AoG): ' + JSON.stringify(googleResponse));
            app.ask(googleResponse); // Send response to Dialogflow and Google Assistant
        }
    }

    // Function to send correctly formatted responses to Dialogflow which are then sent to the user
    function sendResponse(responseToUser) {
        // if the response is a string send it as a response to the user
        if (typeof responseToUser === 'string') {
            console.log("inside string if");
            let responseJson = {};
            responseJson.speech = responseToUser; // spoken response
            responseJson.displayText = responseToUser; // displayed response
            response.json(responseJson); // Send response to Dialogflow
        } else {
            console.log("inside non string else");
            // If the response to the user includes rich responses or contexts send them to Dialogflow
            let responseJson = {};

            // If speech or displayText is defined, use it to respond (if one isn't defined use the other's value)
            responseJson.speech = responseToUser.speech || responseToUser.displayText;
            responseJson.displayText = responseToUser.displayText || responseToUser.speech;
            responseJson.followupEvent = responseToUser.followupEvent;

            // Optional: add rich messages for integrations (https://dialogflow.com/docs/rich-messages)
            responseJson.data = responseToUser.richResponses;

            // Optional: add contexts (https://dialogflow.com/docs/contexts)
            responseJson.contextOut = responseToUser.outputContexts;
            console.log("outputjson : " + JSON.stringify(responseJson));
            response.json(responseJson); // Send response to Dialogflow

        }
    }
}

function videoResult(top3VideosDetails) {
    console.log("\n\ntesting REsult : " + top3VideosDetails + "\n\n legnth = " + top3VideosDetails.length);
    var replyText = '';
    for (var i = 0; i < top3VideosDetails.length; i++) {
        var obj = JSON.parse(top3VideosDetails[i]);
        console.log("\n obj is" + obj);
        replyText = replyText + " \n  \n " + (i + 1) + ". Title : " + obj.title + " \n  \n Snippet: " + obj.text + " \n  \n ";
    }
    //replyText =  'asdwdsaawdasdasdsadasdassdadashrtdfdkmrpadwasdasdasdasdasdsasdasdfsdfsdfsddfhuFDSFSLKEWKRMASKMFKDSLFzxzc'
    ytc.youtubecast('MZZPF9rXzes', 130);
    return replyText;
}


function getGoogleRichRespo(top3VideosDetails) {
    var query = ' on ';
      var googleRichResponse = null;
    if (top3VideosDetails.length == 1) {
      console.log("length is one");
         googleRichResponse = app.buildRichResponse()
            .addSimpleResponse('Result of Video Search  ')
            .addSuggestions(
                ['Play 1st', 'Play 2nd', 'Play 3rd'])
            // Create a basic card and add it to the rich response
            .addBasicCard(app.buildBasicCard("Snippet :" + JSON.parse(top3VideosDetails[0]).text)
                .setTitle(JSON.parse(top3VideosDetails[0]).title)
                .addButton('Play in youtube App', 'https://assistant.google.com/'))

    } else if (top3VideosDetails.length == 2) {
         console.log("length is 2");
       googleRichResponse = app.buildRichResponse()
          .addSimpleResponse('Result of Video Search  ')
          .addSuggestions(
              ['Play 1st', 'Play 2nd', 'Play 3rd'])
          // Create a basic card and add it to the rich response
          .addBasicCard(app.buildBasicCard("Snippet :" + JSON.parse(top3VideosDetails[0]).text)
              .setTitle(JSON.parse(top3VideosDetails[0]).title)
              .addButton('Play in youtube App', 'https://assistant.google.com/'))
          .addBasicCard(app.buildBasicCard("Snippet :" + JSON.parse(top3VideosDetails[1]).text)
              .setTitle(JSON.parse(top3VideosDetails[1].title))
              .addButton('Play in youtube App', 'https://www.youtube.com/'))

    } else if (top3VideosDetails.length == 3) {
      console.log("length is 3");
       googleRichResponse = app.buildRichResponse()
          .addSimpleResponse('Result of Video Search  ')
          .addSuggestions(
              ['Play 1st', 'Play 2nd', 'Play 3rd'])
          // Create a basic card and add it to the rich response
          .addBasicCard(app.buildBasicCard("Snippet :" + JSON.parse(top3VideosDetails[0]).text)
              .setTitle(JSON.parse(top3VideosDetails[0]).title)
              .addButton('Play in youtube App', 'https://assistant.google.com/'))
          .addBasicCard(app.buildBasicCard("Snippet :" + JSON.parse(top3VideosDetails[1]).text)
              .setTitle(JSON.parse(top3VideosDetails[1]).title)
              .addButton('Play in youtube App', 'https://www.youtube.com/'))
          .addBasicCard(app.buildBasicCard("Snippet :" + JSON.parse(top3VideosDetails[2]).text)
              .setTitle(JSON.parse(top3VideosDetails[2]).title)
              .addButton('Play in youtube App', 'https://www.youtube.com/'))

    }
console.log("return response "+ JSON.stringify(googleRichResponse));
    return googleRichResponse;
}

//
// const multiRichResponse = app.buildRichResponse()
//    .addSimpleResponse('Result of test  ')
//    .addBrowseCarousel(app.buildBrowseCarousel)



function carousel (app) {
  app.askWithCarousel('Alright! Here are a few things you can learn. Which sounds interesting?',
    // Build a carousel
    app.buildCarousel()
    // Add the first item to the carousel
    .addItems(app.buildOptionItem('MATH_AND_PRIME',
      ['math', 'math and prime', 'prime numbers', 'prime'])
      .setTitle('Math & prime numbers')
      .setDescription('42 is an abundant number because the sum of its ' +
        'proper divisors 54 is greater…')
      .setImage('http://example.com/math_and_prime.jpg', 'Math & prime numbers'))
    // Add the second item to the carousel
    .addItems(app.buildOptionItem('EGYPT',
      ['religion', 'egpyt', 'ancient egyptian'])
      .setTitle('Ancient Egyptian religion')
      .setDescription('42 gods who ruled on the fate of the dead in the ' +
        'afterworld. Throughout the under…')
      .setImage('http://example.com/egypt', 'Egypt')
    )
    // Add third item to the carousel
    .addItems(app.buildOptionItem('RECIPES',
      ['recipes', 'recipe', '42 recipes'])
      .setTitle('42 recipes with 42 ingredients')
      .setDescription('Here\'s a beautifully simple recipe that\'s full ' +
        'of flavor! All you need is some ginger and…')
      .setImage('http://example.com/recipe', 'Recipe')
    )
  );
}

function basicCard (request , response) {
  const app = new ActionsSdkApp({request, response});
  app.ask(app.buildRichResponse()
    // Create a basic card and add it to the rich response
    .addSimpleResponse('Math and prime numbers it is!')
    .addBasicCard(app.buildBasicCard('42 is an even composite number. It' +
      'is composed of three distinct prime numbers multiplied together. It' +
      'has a total of eight divisors. 42 is an abundant number, because the' +
      'sum of its proper divisors 54 is greater than itself. To count from' +
      '1 to 42 would take you about twenty-one…')
      .setTitle('Math & prime numbers')
      .addButton('Read more', 'https://example.google.com/mathandprimes')
      .setImage('https://example.google.com/42.png', 'Image alternate text')
      .setImageDisplay('CROPPED')
    )
  );
}

Comments

Popular posts from this blog

opening multiple ports tunnels ngrok in ubuntu

Location for the config yml file /home/example/.ngrok2/ngrok.yml content of config file authtoken: 4nq9771bPxe8ctg7LKr_2ClH7Y15Zqe4bWLWF9p tunnels: app-foo: addr: 80 proto: http host_header: app-foo.dev app-bar: addr: 80 proto: http host_header: app-bar.dev how to start ngrok with considering the config file: ngrok start --all

rename field in elastic Search

https://qiita.com/tkprof/items/e50368eb1473497a16d0 How to Rename an Elasticsearch field from columns: - {name: xxx, type: double} to columns: - {name: yyy, type: double} Pipeline API and reindex create a new Pipeline API : Rename Processor PUT _ingest/pipeline/pipeline_rename_xxx { "description" : "rename xxx", "processors" : [ { "rename": { "field": "xxx", "target_field": "yyy" } } ] } { "acknowledged": true } then reindex POST _reindex { "source": { "index": "source" }, "dest": { "index": "dest", "pipeline": "pipeline_rename_xxx" } }

Sumeru enterprise tiger privacy policy

Sumeru Enterprise Tiger Business Solutions Pvt. Ltd. Data Privacy Policy At Sumeru Enterprise Tiger Business Solutions Pvt. Ltd. we are committed to providing you with digitalization software products and services to meet your needs. Our commitment includes protecting personally identifiable information we obtain about you when you register to use one of our websites or become our customer (“Personal Information”). We want to earn your trust by providing strict safeguards to protect your Personal Information. This Policy applies to members, customers, former customers, users, and applicants. In the course of our business activities, Sumeru Enterprise Tiger Business Solutions Pvt. Ltd. collects, processes, and shares Personal Information. Indian law gives individuals the right to limit some but not all sharing. This Policy explains what Personal Information we collect, process, and share. We describe how we do so, and why. The Policy also describes your rights to access a...