Menu

Connecting Amazon Echo to Conducttr

Robert Pratten 8 years ago 0 30

Here’s how to give Conducttr some Alexa Skills 🙂


Here’s the code used in the video example

INTENT JSON
{
  “intents”: [
    {
      “intent”: “GetFacebookMsg”
    }
  ]
}
LAMBDA CODE
//
// Credit to https://www.youtube.com/watch?v=zt9WdE5kR6g for the code base!
//
var https = require(‘https’)
exports.handler = (event, context) => {
  try {
    if (event.session.new) {
      // New Session
      console.log(“NEW SESSION”)
    }
    switch (event.request.type) {
      case “LaunchRequest”:
        // Launch Request
        console.log(`LAUNCH REQUEST`)
        context.succeed(
          generateResponse(
            buildSpeechletResponse(“Welcome to Conducttr”, true),
            {}
          )
        )
        break;
      case “IntentRequest”:
        // Intent Request
        console.log(`INTENT REQUEST`)
        switch(event.request.intent.name) {
          case “”: // INTENT GOES HERE
            var endpoint = “” // ENDPOINT GOES HERE
            var body = “”
            https.get(endpoint, (response) => {
              response.on(‘data’, (chunk) => { body += chunk })
              response.on(‘end’, () => {
                var data = JSON.parse(body)
                var last_comment = data.results[0].this_tweet  // MODIFY THIS TO SUIT YOUR DATA
                context.succeed(
                  generateResponse(
                    buildSpeechletResponse(`hmmm… ${last_comment}`, true),
                    {}
                  )
                )
              })
            })
            break;
          default:
            throw “Invalid intent”
        }
        break;
      case “SessionEndedRequest”:
        // Session Ended Request
        console.log(`SESSION ENDED REQUEST`)
        break;
      default:
        context.fail(`INVALID REQUEST TYPE: ${event.request.type}`)
    }
  } catch(error) { context.fail(`Exception: ${error}`) }
}
// Helpers
buildSpeechletResponse = (outputText, shouldEndSession) => {
  return {
    outputSpeech: {
      type: “PlainText”,
      text: outputText
    },
    shouldEndSession: shouldEndSession
  }
}
generateResponse = (speechletResponse, sessionAttributes) => {
  return {
    version: “1.0”,
    sessionAttributes: sessionAttributes,
    response: speechletResponse
  }
}
Written By