-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
executable file
·122 lines (107 loc) · 4.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* App ID for the skill
*/
var APP_ID = "YOUR_ALEXA_SKILL_ID_HERE";
var AlexaSkill = require('./AlexaSkill');
var parser = require('rss-parser');
var RssReader = function () {
AlexaSkill.call(this, APP_ID);
};
// Extend AlexaSkill
RssReader.prototype = Object.create(AlexaSkill.prototype);
RssReader.prototype.constructor = RssReader;
RssReader.prototype.eventHandlers.onSessionStarted = function (sessionStartedRequest, session) {
console.log("RssReader onSessionStarted requestId: " + sessionStartedRequest.requestId
+ ", sessionId: " + session.sessionId);
// any initialization logic goes here
};
RssReader.prototype.eventHandlers.onLaunch = function (launchRequest, session, response) {
console.log("RssReader onLaunch requestId: " + launchRequest.requestId + ", sessionId: " + session.sessionId);
var speechOutput = "Welcome, What news would you like to hear?";
var repromptText = "What news would you like to hear?";
response.ask(speechOutput, repromptText);
};
RssReader.prototype.eventHandlers.onSessionEnded = function (sessionEndedRequest, session) {
console.log("RssReader onSessionEnded requestId: " + sessionEndedRequest.requestId
+ ", sessionId: " + session.sessionId);
// any cleanup logic goes here
};
RssReader.prototype.intentHandlers = {
// register custom intent handlers
"GetBBCNews": function (intent, session, response) {
var url;
if(intent.slots && intent.slots.FeedName)
{
switch (intent.slots.FeedName.value.toLowerCase()) {
case "world":
url = "http://feeds.bbci.co.uk/news/world/rss.xml?edition=uk";
break;
case "front page":
case "uk":
case "main":
url = "http://feeds.bbci.co.uk/news/rss.xml?edition=uk";
break;
case "technology":
case "tech":
url = "http://feeds.bbci.co.uk/news/technology/rss.xml?edition=uk";
break;
case "local":
case "northern ireland":
url = "http://feeds.bbci.co.uk/news/northern_ireland/rss.xml?edition=uk";
break;
case "business":
url = "http://feeds.bbci.co.uk/news/business/rss.xml?edition=uk";
break;
case "politics":
url = "http://feeds.bbci.co.uk/news/politics/rss.xml?edition=uk";
break;
case "health":
url = "http://feeds.bbci.co.uk/news/health/rss.xml?edition=uk";
break;
case "education":
url = "http://feeds.bbci.co.uk/news/education/rss.xml?edition=uk";
break;
case "science":
url = "http://feeds.bbci.co.uk/news/science_and_environment/rss.xml?edition=uk";
break;
case "entertainment":
url = "http://feeds.bbci.co.uk/news/entertainment_and_arts/rss.xml?edition=uk";
break;
default:
response.tellWithCard("Unable to understand " + intent.slots.FeedName.value, "RSS Headlines", "Couldn't understand " + intent.slots.FeedName.value);
return;
}
}
else
{
response.tellWithCard("Unable to understand that request", "RSS Headlines", "Couldn't understand the request");
return;
}
parser.parseURL(url, function(err,parsed){
var output = "" + parsed.feed.title + ". <break time=\"0.8s\"/> ";
var i = 0;
parsed.feed.entries.forEach(function(entry){
if(i <= 4)
{
console.log(entry.title);
output = output + entry.title + ". <break time=\"0.6s\"/> "
i++;
}
})
var ssmlOutput = {
speech: '<speak>' + output + '</speak>',
type: AlexaSkill.speechOutputType.SSML
};
response.tellWithCard(ssmlOutput, "RSS Headlines", "Reading headlines from: " + parsed.feed.title);
});
},
"AMAZON.HelpIntent": function (intent, session, response) {
response.ask("You can say what feed you want to hear", "You can say what feed you want to hear");
}
};
// Create the handler that responds to the Alexa Request.
exports.handler = function (event, context) {
// Create an instance of the RssReader skill.
var rssReader = new RssReader();
rssReader.execute(event, context);
};