Ram Vaidya
Created November 22, 2016

Facts About Numbers

Ever felt curious about the strange and mysterious ways of numbers? This skill will give you an interesting fact about a random number!

16
Facts About Numbers

Things used in this project

Hardware components

Amazon Echo
Amazon Alexa Amazon Echo
×1

Software apps and online services

Alexa Voice Service
Amazon Alexa Alexa Voice Service

Story

Read more

Code

express server

JavaScript
install, dependencies, run the javascript file using node
var express = require('express');
var bodyParser = require('body-parser');
var verifier = require('alexa-verifier');
var request = require('request');
var app = express();
app.use(function(req, res, next) {
        if (!req.headers
                .signaturecertchainurl
        ) {
                return next();
        }
        req._body =
                true;
        req.rawBody =
                '';
        req.on('data',
                function(
                        data
                ) {
                        return req
                                .rawBody +=
                                data;
                }
        );
        req.on('end',
                function() {
                        var
                                cert_url,
                                er,
                                error,
                                requestBody,
                                signature;
                        try {
                                req
                                        .body =
                                        JSON
                                        .parse(
                                                req
                                                .rawBody
                                        );
                        }
                        catch (
                                error
                        ) {
                                er
                                        =
                                        error;
                                req
                                        .body = {};
                        }
                        cert_url
                                =
                                req
                                .headers
                                .signaturecertchainurl;
                        signature
                                =
                                req
                                .headers
                                .signature;
                        requestBody
                                =
                                req
                                .rawBody;
                        verifier
                                (
                                        cert_url,
                                        signature,
                                        requestBody,
                                        function(
                                                er
                                        ) {
                                                if (
                                                        er
                                                ) {
                                                        console
                                                                .error(
                                                                        'error validating the alexa cert:',
                                                                        er
                                                                );
                                                        res
                                                                .status(
                                                                        401
                                                                )
                                                                .json({
                                                                        status: 'failure',
                                                                        reason: er
                                                                });
                                                }
                                                else {
                                                        next
                                                                ();
                                                }
                                        }
                                );
                }
        );
});
app.use(bodyParser.json());
app.get('/', function(req, res) {
        res.send(
                "hello"
        );
})
app.post('/', function(req, res) {
        var data = {
                "version": "1.0",
                "response": {
                        "outputSpeech": {
                                "type": "PlainText",
                                "text": "Please work!"
                        },
                        "shouldEndSession": true
                },
                "reprompt": {
                        "outputSpeech": {
                                "type": "PlainText",
                                "text": "Can I help you with anything else?"
                        }
                },
                "sessionAttributes": {}
        }
        var info = req.body
                .request;
        //handle different types of requests
        if (info.type ==
                'LaunchRequest'
        ) {
                data.response
                        .outputSpeech
                        .text =
                        "Hey! Ask me for a number fact."
                data.response
                        .shouldEndSession =
                        false;
                res.send(
                        data
                );
        }
        else if (info
                .type ==
                'SessionEndedRequest'
        ) {}
        else if (
                info.type ==
                'IntentRequest'
        ) {
                if (
                        info
                        .intent
                        .name ==
                        "AMAZON.HelpIntent"
                ) {
                        console
                                .log(
                                        "help intent"
                                );
                        data
                                .response
                                .outputSpeech
                                .text =
                                "You can ask me for a random number fact."
                        data
                                .response
                                .shouldEndSession =
                                false;
                        res
                                .send(
                                        data
                                );
                }
                else if (
                        info
                        .intent
                        .name ==
                        "AMAZON.CancelIntent" ||
                        info
                        .intent
                        .name ==
                        "AMAZON.StopIntent"
                ) {
                        console
                                .log(
                                        "cancel intent"
                                );
                        data
                                .response
                                .outputSpeech
                                .text =
                                "Good-bye."
                        data
                                .response
                                .shouldEndSession =
                                true;
                        res
                                .send(
                                        data
                                );
                }
                else if (
                        info
                        .intent
                        .name =
                        "GetFact"
                ) {
                        var
                                randnum =
                                Math
                                .floor(
                                        Math
                                        .random() *
                                        10
                                ) +
                                2;
                        request
                                (
                                        "http://numbersapi.com/" +
                                        randnum,
                                        function(
                                                error,
                                                response,
                                                body
                                        ) {
                                                if (!
                                                        error &&
                                                        response
                                                        .statusCode ==
                                                        200
                                                ) {
                                                        var
                                                                fact =
                                                                body;
                                                        data
                                                                .response
                                                                .outputSpeech
                                                                .text =
                                                                fact
                                                        data
                                                                .response
                                                                .shouldEndSession =
                                                                true;
                                                        res
                                                                .send(
                                                                        data
                                                                );
                                                }
                                                else {
                                                        data
                                                                .response
                                                                .outputSpeech
                                                                .text =
                                                                fact
                                                        data
                                                                .response
                                                                .shouldEndSession =
                                                                true;
                                                        res
                                                                .send(
                                                                        "whoops! there was a problem getting the fact."
                                                                );
                                                }
                                        }
                                );
                }
        }
})
var port = process.env.PORT || 3000;
app.listen(port, function() {
        console.log(
                'test'
        );
});

Credits

Ram Vaidya

Ram Vaidya

1 project • 0 followers

Comments