Nicolas Grenié
Created June 5, 2017 © CC BY-NC

StartupInfo - Learn startups facts

With StartupInfo learn all you need about startups. Who are the founders, who invested in it, and what they do.

IntermediateShowcase (no instructions)12
StartupInfo - Learn startups facts

Things used in this project

Hardware components

Amazon Echo
Amazon Alexa Amazon Echo
×1

Software apps and online services

AWS Lambda
Amazon Web Services AWS Lambda
Crunchbase API

Story

Read more

Schematics

schema

Code

alexa.js

JavaScript
 'use strict';

var http = require('http');
var _ = require('underscore');
var Q = require('q');

var crunchbase = require('./crunchbase');
exports.handler = function (event, context) {
    try {
        console.log("event.session.application.applicationId=" + event.session.application.applicationId);
        if (event.session.new) {
            onSessionStarted({requestId: event.request.requestId}, event.session);
        }

        if (event.request.type === "LaunchRequest") {
            onLaunch(event.request,
                     event.session,
                     function callback(sessionAttributes, speechletResponse) {
                        context.succeed(buildResponse(sessionAttributes, speechletResponse));
                     });
        }  else if (event.request.type === "IntentRequest") {
            onIntent(event.request,
                     event.session,
                     function callback(sessionAttributes, speechletResponse) {
                         context.succeed(buildResponse(sessionAttributes, speechletResponse));
                     });
        } else if (event.request.type === "SessionEndedRequest") {
            onSessionEnded(event.request, event.session);
            context.succeed();
        }
    } catch (e) {
        context.fail("Exception: " + e);
    }
};

/**
 * Called when the session starts.
 */
function onSessionStarted(sessionStartedRequest, session) {
    console.log("onSessionStarted requestId=" + sessionStartedRequest.requestId +
            ", sessionId=" + session.sessionId);
}

/**
 * Called when the user launches the skill without specifying what they want.
 */
function onLaunch(launchRequest, session, callback) {
    console.log("onLaunch requestId=" + launchRequest.requestId +
            ", sessionId=" + session.sessionId);

    // Dispatch to your skill's launch.
    getWelcomeResponse(callback);
}

/**
 * Called when the user specifies an intent for this skill.
 */
function onIntent(intentRequest, session, callback) {
    console.log("onIntent requestId=" + intentRequest.requestId +
            ", sessionId=" + session.sessionId+","+intentRequest.intent);

    var intent = intentRequest.intent,
        intentName = intentRequest.intent.name;

    console.log(intent)
    // Dispatch to your skill's intent handlers
    if("GetCompanyInfo" === intentName) {
      getCompanyInfo(intent, session, callback);
    }else if("GetFounders" === intentName) {
      getFoundersInfo(intent, session, callback);
    } else if ("AMAZON.HelpIntent" === intentName) {
        getWelcomeResponse(callback);
    } else {
        throw "Invalid intent";
    }
}

/**
 * Called when the user ends the session.
 * Is not called when the skill returns shouldEndSession=true.
 */
function onSessionEnded(sessionEndedRequest, session) {
    console.log("onSessionEnded requestId=" + sessionEndedRequest.requestId +
            ", sessionId=" + session.sessionId);
    // Add cleanup logic here
}

function getFoundersInfo(intent, session, callback){

}

function getCompanyInfo(intent, session, callback){
  console.log(intent);
  var company = intent.slots.CompanyName.value;
  var cardTitle = "get info about "+company;
  var repromptText = "";
  var sessionAttributes = {};
  var shouldEndSession = true;
  var speechOutput = "";

  if(company){

    var URL = "http://api.crunchbase.com/v/3/organizations?organization_types=company"
    URL+= "&name="+company;
    URL +="&user_key=YOURUSERKEY"

    console.log(URL);
    http.get(URL, function (result) {
      var body = ""
      result.on('data',function(chunk){
        body += chunk
      });

      result.on('end',function(){
        var res = JSON.parse(body)
        if(res.data.items.length >0){
          var items = _.sortBy(res.data.items,function(e){return e.properties.created_at})
          var info = items[0].properties;
          console.log(info.short_description);
          speechOutput = "<speak>"
          speechOutput += "<s>Here is what I have found on Crunchbase about "+company+"</s>"
          speechOutput += "<s>"+info.short_description+"</s>"
          speechOutput += "</speak>"

          var cardContent = info.short_description

          repromptText = "You can ask for more details like funding, or founders";
          console.log(speechOutput)
          callback(sessionAttributes,
                 buildSpeechletResponse(cardTitle, speechOutput,cardContent, repromptText, shouldEndSession));
        }else{
          speechOutput = "I'm not sure I could find the company" ;
          repromptText = "I'm not sure I could find the company you can say for example " +
                  "search for Google";
          callback(sessionAttributes,
                   buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
        }
      });
    }).on('error', function (err) {
      console.log('Error, with: ' + err.message);
      speechOutput = "I'm not sure I could find the company" ;
      repromptText = "I'm not sure I could find the company you can say for example " +
              "search for Google";
      callback(sessionAttributes,
               buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
    });

  }else{

  }


}
// --------------- Functions that control the skill's behavior -----------------------

function getWelcomeResponse(callback) {
    // If we wanted to initialize the session to have some attributes we could add those here.
    var sessionAttributes = {};
    var cardTitle = "Welcome";
    var speechOutput = "Welcome to the Crunch Alexa Skill , " +
            "Search for companies info by saying" +
            "ask about slack";
    // If the user either does not reply to the welcome message or says something that is not
    // understood, they will be prompted again with this text.
    var repromptText = "Search about companies like this" +
            "ask about slack";
    var shouldEndSession = false;

    callback(sessionAttributes,
             buildSpeechletResponse(cardTitle, speechOutput, repromptText, shouldEndSession));
}

// --------------- Helpers that build all of the responses -----------------------

function buildSpeechletResponse(title, speechOutput, cardContent, repromptText, shouldEndSession) {
    return {
        outputSpeech: {
            type: "SSML",
            ssml: speechOutput,
        },
        card: {
            type: "Simple",
            title: "Crunchbase - " + title,
            content: cardContent
        },
        reprompt: {
            outputSpeech: {
                type: "PlainText",
                text: repromptText
            }
        },
        shouldEndSession: shouldEndSession
    };
}

function buildResponse(sessionAttributes, speechletResponse) {
    return {
        version: "1.0",
        sessionAttributes: sessionAttributes,
        response: speechletResponse
    };
}

Credits

Nicolas Grenié
1 project • 4 followers

Comments