Swapnil Agarwal
Published © GPL3+

Star Date

This skill returns a stardate, which is a fictional system of time measurement developed for the television and film series Star Trek.

BeginnerShowcase (no instructions)1 hour566
Star Date

Things used in this project

Story

Read more

Code

StarDate

JavaScript
Deploy to a Lambda function, triggered by Alexa Skills Kit.
'use strict';

const Alexa = require('alexa-sdk');

const APP_ID = 'amzn1.ask.skill.e26e400d-1271-4e97-bf05-e9d1d7980ff3';

const SKILL_NAME = 'Star Date';
const STAR_DATE_MESSAGE = 'The current star date is: ';
const HELP_MESSAGE = 'Do you want to know the current star date?';
const STOP_MESSAGE = 'Goodbye!';

const handlers = {
    'LaunchRequest': function () {
        this.emit('GetStarDate');
    },
    'GetStarDate': function () {
        var date = new Date();

        var year = date.getUTCFullYear();
        var month = date.getUTCMonth() + 1;
        var day = date.getUTCDate();
        var seconds = date.getUTCSeconds() + (60 * date.getUTCMinutes()) + (60 * 60 * date.getUTCHours());
        
        var stardate1 = year;
        var stardate2 = ("0"+month).slice(-2) + ("0" + day).slice(-2);
        var stardate3 = Math.floor(seconds%86400/86400*100000);
        var stardate = "<say-as interpret-as='digits'>"+stardate1+"</say-as>"
                     + "<break/>"
                     + "<say-as interpret-as='digits'>"+stardate2+"</say-as>"
                     + "<break/>"
                     + "dot"
                     + "<break/>"
                     + "<say-as interpret-as='digits'>"+stardate3+"</say-as>";
        
        const speechOutput = STAR_DATE_MESSAGE + stardate;
        
        this.emit(':tellWithCard', speechOutput, SKILL_NAME, STAR_DATE_MESSAGE + stardate1 + stardate2 + '.' + stardate3);
    },
    'AMAZON.HelpIntent': function () {
        const speechOutput = HELP_MESSAGE;
        const reprompt = HELP_MESSAGE;
        this.emit(':ask', speechOutput, reprompt);
    },
    'AMAZON.CancelIntent': function () {
        this.emit(':tell', STOP_MESSAGE);
    },
    'AMAZON.StopIntent': function () {
        this.emit(':tell', STOP_MESSAGE);
    },
    'SessionEndedRequest': function () {
        this.emit(':tell', STOP_MESSAGE);
    },
};

exports.handler = (event, context) => {
    const alexa = Alexa.handler(event, context);
    alexa.APP_ID = APP_ID;
    alexa.registerHandlers(handlers);
    alexa.execute();
};

Credits

Swapnil Agarwal

Swapnil Agarwal

0 projects • 0 followers

Comments