function search_video(req, res, lang) {
  var userId = req.userId;

  var query = req.slot("VideoQuery");
  console.log("Requesting search ... " + query);

  return new Promise((resolve, reject) => {
    var search = heroku + "/alexa/v3/search/" + new Buffer(query).toString("base64");

    // Populate URL with correct language 
    if (lang === "de-DE") {
      search += "?language=de";
    } else if (lang === "fr-FR") {
      search += "?language=fr";
    } else if (lang === "it-IT") {
      search += "?language=it";
    }

    // Make search request to server
    request(search, function(err, res, body) {
      if (err) {
        // Error in the request
        reject(err.message);
      } else {
        // Convert body text in response to JSON object
        var body_json = JSON.parse(body);
        if (body_json.status === "error" && body_json.message === "No results found") {
          // Query did not return any video
          resolve({
            message: response_messages[lang]["NO_RESULTS_FOUND"].formatUnicorn(query),
            metadata: null
          });
        } else {
          // Extract and return metadata
          var metadata = body_json.video;
          console.log("Found ... " + metadata.title + " @ " + metadata.link);
          resolve({
            message: response_messages[lang]["ASK_TO_PLAY"].formatUnicorn(metadata.title),
            metadata: metadata
          });
        }
      }
    });
  }).then(function(content) {
    // Have Alexa say the message fetched from the Heroku server
    var speech = new ssml();
    speech.say(content.message);
    res.say(speech.ssml(true));

    if (content.metadata) {
      var metadata = content.metadata;

      // Generate card for the Alexa mobile app
      res.card({
        type: "Simple",
        title: "Search for \"" + query + "\"",
        content: "Alexa found \"" + metadata.title + "\" at " + metadata.link + "."
      });

      // Set most recently searched for video
      buffer_search[userId] = metadata;

      res.reprompt().shouldEndSession(false);
    }

    // Send response to Alexa device
    res.send();
  }).catch(function(reason) {
    // Error occurred in the promise
    res.fail(reason);
  });
}
/**
 * Searches a YouTube video matching the user's query.
 *
 * @param  {Object} req  A request from an Alexa device
 * @param  {Object} res  A response that will be sent to the device
 * @param  {String} lang The language of the query
 * @return {Promise} Execution of the request
 */
Searches a YouTube video matching the user's query.
@param  {Object} req  A request from an Alexa device
@param  {Object} res  A response that will be sent to the device
@param  {String} lang The language of the query
@return {Promise} Execution of the request
Searches a YouTube video matching the user ' s query . @param { Object } req A request from an Alexa device @param { Object } res A response that will be sent to the device @param { String } lang The language of the query @return { Promise } Execution of the request
function search_video(req, res, lang) {
  var userId = req.userId;
  var query = req.slot("VideoQuery");
  console.log("Requesting search ... " + query);
  return new Promise((resolve, reject) => {
    var search = heroku + "/alexa/v3/search/" + new Buffer(query).toString("base64");
    if (lang === "de-DE") {
      search += "?language=de";
    } else if (lang === "fr-FR") {
      search += "?language=fr";
    } else if (lang === "it-IT") {
      search += "?language=it";
    }
    request(search, function(err, res, body) {
      if (err) {
        reject(err.message);
      } else {
        var body_json = JSON.parse(body);
        if (body_json.status === "error" && body_json.message === "No results found") {
          resolve({
            message: response_messages[lang]["NO_RESULTS_FOUND"].formatUnicorn(query),
            metadata: null
          });
        } else {
          var metadata = body_json.video;
          console.log("Found ... " + metadata.title + " @ " + metadata.link);
          resolve({
            message: response_messages[lang]["ASK_TO_PLAY"].formatUnicorn(metadata.title),
            metadata: metadata
          });
        }
      }
    });
  }).then(function(content) {
    var speech = new ssml();
    speech.say(content.message);
    res.say(speech.ssml(true));
    if (content.metadata) {
      var metadata = content.metadata;
      res.card({
        type: "Simple",
        title: "Search for \"" + query + "\"",
        content: "Alexa found \"" + metadata.title + "\" at " + metadata.link + "."
      });
      buffer_search[userId] = metadata;
      res.reprompt().shouldEndSession(false);
    }
    res.send();
  }).catch(function(reason) {
    res.fail(reason);
  });
}
function search_video ( req , res , lang ) { var userId = req . userId ; var query = req . slot ( "VideoQuery" ) ; console . log ( "Requesting search ... " + query ) ; return new Promise ( ( resolve , reject ) => { var search = heroku + "/alexa/v3/search/" + new Buffer ( query ) . toString ( "base64" ) ; if ( lang === "de-DE" ) { search += "?language=de" ; } else if ( lang === "fr-FR" ) { search += "?language=fr" ; } else if ( lang === "it-IT" ) { search += "?language=it" ; } request ( search , function ( err , res , body ) { if ( err ) { reject ( err . message ) ; } else { var body_json = JSON . parse ( body ) ; if ( body_json . status === "error" && body_json . message === "No results found" ) { resolve ( { message : response_messages [ lang ] [ "NO_RESULTS_FOUND" ] . formatUnicorn ( query ) , metadata : null } ) ; } else { var metadata = body_json . video ; console . log ( "Found ... " + metadata . title + " @ " + metadata . link ) ; resolve ( { message : response_messages [ lang ] [ "ASK_TO_PLAY" ] . formatUnicorn ( metadata . title ) , metadata : metadata } ) ; } } } ) ; } ) . then ( function ( content ) { var speech = new ssml ( ) ; speech . say ( content . message ) ; res . say ( speech . ssml ( true ) ) ; if ( content . metadata ) { var metadata = content . metadata ; res . card ( { type : "Simple" , title : "Search for \"" + query + "\"" , content : "Alexa found \"" + metadata . title + "\" at " + metadata . link + "." } ) ; buffer_search [ userId ] = metadata ; res . reprompt ( ) . shouldEndSession ( false ) ; } res . send ( ) ; } ) . catch ( function ( reason ) { res . fail ( reason ) ; } ) ; }
Searches a YouTube video matching the user's query.
Searches a YouTube video matching the user ' s query .
// Populate URL with correct language  // Make search request to server // Error in the request // Convert body text in response to JSON object // Query did not return any video // Extract and return metadata // Have Alexa say the message fetched from the Heroku server // Generate card for the Alexa mobile app // Set most recently searched for video // Send response to Alexa device // Error occurred in the promise