Skip to content

Instantly share code, notes, and snippets.

@am-MongoDB
Created March 13, 2017 08:58
Show Gist options
  • Save am-MongoDB/8bf49fd414d92ed00f021bafe4ba2c71 to your computer and use it in GitHub Desktop.
Save am-MongoDB/8bf49fd414d92ed00f021bafe4ba2c71 to your computer and use it in GitHub Desktop.

Revisions

  1. am-MongoDB created this gist Mar 13, 2017.
    136 changes: 136 additions & 0 deletions index.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,136 @@
    "use strict";

    var Alexa = require("alexa-sdk");
    var request = require("request");
    var config = require("./config.js");

    exports.handler = function(event, context, callback) {

    console.log("In handler");

    var alexa = Alexa.handler(event, context);
    alexa.appId = config.appId;

    alexa.registerHandlers(handlers);
    alexa.execute();
    };

    var handlers = {
    "AMAZON.HelpIntent": function () {
    this.emit(':tellWithCard',
    "Ask where Andrew is or ask how many times he has checked in",
    "Mongo – Where's Andrew",
    "Ask where Andrew is or ask how many times he has checked in",
    {
    smallImageUrl: "https://media.licdn.com/mpr/mpr/shrinknp_200_200/AAEAAQAAAAAAAALQAAAAJDhkZWUxZDQxLTM0NjctNDcxZS04NmJiLTA1YzRhNGVlNWY0ZQ.jpg",
    largeImageUrl: "https://media.licdn.com/mpr/mpr/shrinknp_200_200/AAEAAQAAAAAAAALQAAAAJDhkZWUxZDQxLTM0NjctNDcxZS04NmJiLTA1YzRhNGVlNWY0ZQ.jpg"
    }
    )},
    "CountIntent": function () {

    console.log("In CountIntent");

    var countURL = config.mongopopAPI + "checkInCount";
    var _this = this;

    request({url: countURL, json: true}, function (error, response, body) {
    console.log("in callback");
    if (error || response.statusCode != 200) {
    console.log("Failed to count checkins: " + error.message);
    _this.emit(':tellWithCard',
    "Network error, check Alexa app for details",
    "Mongo – Error",
    "Network error: " + error.message,
    {
    smallImageUrl: "https://cdn3.iconfinder.com/data/icons/wifi-2/460/connection-error-512.png",
    largeImageUrl: "https://cdn3.iconfinder.com/data/icons/wifi-2/460/connection-error-512.png"
    }
    )
    } else {
    if (body.success) {

    var successString = "Andrew has checked in " + body.count + " times.";
    _this.emit(':tellWithCard',
    successString,
    "Mongo – Where's Andrew",
    successString,
    {
    smallImageUrl: "https://media.licdn.com/mpr/mpr/shrinknp_200_200/AAEAAQAAAAAAAALQAAAAJDhkZWUxZDQxLTM0NjctNDcxZS04NmJiLTA1YzRhNGVlNWY0ZQ.jpg",
    largeImageUrl: "https://media.licdn.com/mpr/mpr/shrinknp_200_200/AAEAAQAAAAAAAALQAAAAJDhkZWUxZDQxLTM0NjctNDcxZS04NmJiLTA1YzRhNGVlNWY0ZQ.jpg"
    }
    )
    } else {
    console.log("Failed to count checkins: " + body.error);
    _this.emit(':tellWithCard',
    "Application error, check Alexa app for details",
    "Mongo – Error",
    "Application error: " + body.error,
    {
    smallImageUrl: "https://upload.wikimedia.org/wikipedia/commons/thumb/b/bf/Sad_face.gif/1024px-Sad_face.gif",
    largeImageUrl: "https://upload.wikimedia.org/wikipedia/commons/thumb/b/bf/Sad_face.gif/1024px-Sad_face.gif"
    }
    )
    }
    }
    })
    },
    "WhereIntent": function () {

    console.log("In WhereIntent");

    var latestCheckinURL = config.mongopopAPI + "latestCheckin";
    var _this = this;

    request({url: latestCheckinURL, json: true}, function (error, response, body) {
    console.log("in callback");
    if (error || response.statusCode != 200) {
    console.log("Failed to fetch latest Checkin, network problem: " + error.message);
    _this.emit(':tellWithCard',
    "Network error, check Alexa app for details",
    "Mongo – Error",
    "Network error: " + error.message,
    {
    smallImageUrl: "https://cdn3.iconfinder.com/data/icons/wifi-2/460/connection-error-512.png",
    largeImageUrl: "https://cdn3.iconfinder.com/data/icons/wifi-2/460/connection-error-512.png"
    }
    )
    } else {
    if (body.success) {
    var successString = "Andrew last checked in to " + body.venue + " on " + body.date;
    var imgURL = body.location.replace("http", "https");
    _this.emit(':tellWithCard',
    successString,
    "Mongo – Where's Andrew",
    successString + ". Location URL: " + body.url + ". View venue: " + body.location + ".",
    {
    smallImageUrl: imgURL,
    largeImageUrl: imgURL
    }
    )
    } else {
    console.log("Failed to fetch latest checkin, app error: " + body.error);
    _this.emit(':tellWithCard',
    "Application error, check Alexa app for details",
    "Mongo – Error",
    "Application error: " + body.error,
    {
    smallImageUrl: "https://upload.wikimedia.org/wikipedia/commons/thumb/b/bf/Sad_face.gif/1024px-Sad_face.gif",
    largeImageUrl: "https://upload.wikimedia.org/wikipedia/commons/thumb/b/bf/Sad_face.gif/1024px-Sad_face.gif"
    }
    )
    }
    }
    })
    },
    "Unhandled": function () {
    this.emit(':tellWithCard',
    "Unhandled event",
    "Mongo – Error",
    "Unhandled event",
    {
    smallImageUrl: "https://upload.wikimedia.org/wikipedia/commons/thumb/b/bf/Sad_face.gif/1024px-Sad_face.gif",
    largeImageUrl: "https://upload.wikimedia.org/wikipedia/commons/thumb/b/bf/Sad_face.gif/1024px-Sad_face.gif"
    }
    )
    }
    };