Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 'use strict';
- var https = require('https');
- /**
- * URL prefix to download history content from Wikipedia
- */
- var urlPrefix = 'https://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&explaintext=&exsectionformat=plain&redirects=&titles=';
- /**
- * Variable defining number of events to be read at one time
- */
- var paginationSize = 3;
- /**
- * Variable defining the length of the delimiter between events
- */
- var delimiterSize = 2;
- /**
- * Gets a poster prepares the speech to reply to the user.
- */
- function handleFirstEventRequest(intent, session, response) {
- var daySlot = intent.slots.day;
- var repromptText = "With History Buff, you can get historical events for any day of the year. For example, you could say today, or August thirtieth. Now, which day do you want?";
- var monthNames = ["January", "February", "March", "April", "May", "June",
- "July", "August", "September", "October", "November", "December"
- ];
- var sessionAttributes = {};
- // Read the first 3 events, then set the count to 3
- sessionAttributes.index = paginationSize;
- var date = "";
- // If the user provides a date, then use that, otherwise use today
- // The date is in server time, not in the user's time zone. So "today" for the user may actually be tomorrow
- if (daySlot && daySlot.value) {
- date = new Date(daySlot.value);
- } else {
- date = new Date();
- }
- var prefixContent = "For " + monthNames[date.getMonth()] + " " + date.getDate() + " ";
- var cardContent = "For " + monthNames[date.getMonth()] + " " + date.getDate() + " ";
- var cardTitle = "Events on " + monthNames[date.getMonth()] + " " + date.getDate();
- getJsonEventsFromWikipedia(monthNames[date.getMonth()], date.getDate(), function (events) {
- var speechText = "",
- i;
- sessionAttributes.text = events;
- session.attributes = sessionAttributes;
- if (events.length == 0) {
- speechText = "There is a problem connecting to Wikipedia at this time. Please try again later.";
- cardContent = speechText;
- response.tell(speechText);
- } else {
- for (i = 0; i < paginationSize; i++) {
- cardContent = cardContent + events[i];
- speechText = speechText + events[i];
- }
- speechText = speechText + "Wanna go deeper in history?";
- var speechOutput = prefixContent + speechText;
- var repromptOutput = repromptText;
- response.askWithCard(speechOutput, cardTitle, cardContent);
- }
- });
- }
- /**
- * Gets a poster prepares the speech to reply to the user.
- */
- function handleNextEventRequest(intent, session, response) {
- var cardTitle = "More events on this day in history",
- sessionAttributes = session.attributes,
- result = sessionAttributes.text,
- speechText = "",
- cardContent = "",
- repromptText = "Do you want to know more about what happened on this date?",
- i;
- if (!result) {
- speechText = "With History Buff, you can get historical events for any day of the year. For example, you could say today, or August thirtieth. Now, which day do you want?";
- cardContent = speechText;
- } else if (sessionAttributes.index >= result.length) {
- speechText = "There are no more events for this date. Try another date by saying get events for august thirtieth.";
- cardContent = "There are no more events for this date. Try another date by saying, get events for august thirtieth.";
- } else {
- for (i = 0; i < paginationSize; i++) {
- if (sessionAttributes.index>= result.length) {
- break;
- }
- speechText = speechText + result[sessionAttributes.index];
- cardContent = cardContent + result[sessionAttributes.index];
- sessionAttributes.index++;
- }
- if (sessionAttributes.index < result.length) {
- speechText = speechText + " Wanna go deeper in history?";
- cardContent = cardContent + " Wanna go deeper in history?";
- }
- }
- var speechOutput = speechText;
- var repromptOutput = repromptText;
- response.askWithCard(speechOutput, repromptOutput, cardTitle, cardContent);
- }
- function getJsonEventsFromWikipedia(day, date, eventCallback) {
- var url = urlPrefix + day + '_' + date;
- console.log("yippee requestId: " + url);
- https.get(url, function(res) {
- var body = '';
- res.on('data', function (chunk) {
- body += chunk;
- });
- res.on('end', function () {
- var stringResult = parseJson(body);
- eventCallback(stringResult);
- });
- }).on('error', function (e) {
- console.log("Got error: ", e);
- });
- }
- function parseJson(inputText) {
- // sizeOf (/nEvents/n) is 10
- var text = inputText.substring(inputText.indexOf("\\nEvents\\n")+10, inputText.indexOf("\\n\\n\\nBirths")),
- retArr = [],
- retString = "",
- endIndex,
- startIndex = 0;
- if (text.length == 0) {
- return retArr;
- }
- while(true) {
- endIndex = text.indexOf("\\n", startIndex+delimiterSize);
- var eventText = (endIndex == -1 ? text.substring(startIndex) : text.substring(startIndex, endIndex));
- // replace dashes returned in text from Wikipedia's API
- eventText = eventText.replace(/\\u2013\s*/g, '');
- // add comma after year so Alexa pauses before continuing with the sentence
- eventText = eventText.replace(/(^\d+)/,'$1,');
- eventText = 'In ' + eventText;
- startIndex = endIndex+delimiterSize;
- retArr.push(eventText);
- if (endIndex == -1) {
- break;
- }
- }
- if (retString != "") {
- retArr.push(retString);
- }
- retArr.reverse();
- return retArr;
- }
- module.exports = handleFirstEventRequest;
- module.exports = handleNextEventRequest;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement