-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
234 lines (163 loc) · 6.62 KB
/
app.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
var alexa = require('alexa-app');
var app = new alexa.app('paradox150');
var striptags = require('striptags');
var config = require('./config/config');
var feedController = require('./controllers/feedController');
exports.handler = function(event, context) {
console.info("Request:", event.request);
if(event.request.intent) {
if(event.request.intent.slots) {
console.info('Slots:', event.request.intent.slots);
}
}
else{
console.log('here');
}
// Send requests to the alexa-app framework for routing
app.lambda()(event, context);
};
app.sessionEnded(function(request,response) {
// Clean up the user's server-side stuff, if necessary
response.clearSession();
// No response necessary
});
app.pre = function (request, response, type) {
if(process.env.ALEXA_APP_ID) {
if (request.sessionDetails.application.applicationId != 'amzn1.echo-sdk-ams.app.'+config.alexaAppId) {
// Fail silently
response.send();
}
}
};
// TODO: change this message before production release
app.error = function(error, request, response) {
response.say("Something went wrong the Costa Rica News app: ");
exitIntent(request,response);
};
app.launch(function (request, response){
response.say("Welcome to Costa Rica News... Say start to get the latest news from Costa Rica, or help to receive some guidance...");
response.shouldEndSession(false,"You can say: Start, or Help.");
});
function OnHelpIntent (request, response){
var message=["Costa Rica news reads the latest news from Costa Rica. To get your news feed, open Costa Rica news and say Start.",
"Costa Rica news will start reading the most recent group of news.",
"If there are more news, you will be asked if you want to hear them, you can say: yes, more, or okay, to keep listening",
"Say No, to make it stop" ,
"Costa Rica news will stop when it's done reading, or when you ask alexa to Stop.",
" Do you want to hear your news feed?"].join("<break time='500ms'/>");
response.say(message);
response.shouldEndSession(false, "You can say: yes, or no.");
};
app.intent(null, exitIntent)
app.intent('OnHelpIntent', {
"utterances":["help","what can I ask you",
"get help",
"what do you do",
"how can I use you",
"help me"]
},OnHelpIntent)
function OnAboutIntent (request, response){
var message=["Costa Rica news is an Amazon Echo Skill Created by Andres Meza in Costa Rica, for more info visit www.andresmeza.com.",
" Do you want to hear your news feed?"
].join("<break time='500ms'/>");
response.say(message);
response.card("Costa Rica news is an Amazon Echo Skill Created by Andres Meza in Costa Rica. For more info visit www.andresmeza.com.",'http://www.andresmeza.com','http://www.andresmeza.com');
response.shouldEndSession(false, "You can say: yes, or no.");
};
app.intent('OnAboutIntent', {
"utterances":["about","Who created this app", "Who created this skill"]
},OnAboutIntent)
app.intent('OnStartIntent', {
"utterances":["start"]
},OnStartIntent);
function OnStartIntent (request,response) {
console.log('sources:'+config.newsSources.length);
feedController.fetch(config.newsSources,function(postCollection) {
var sayCount=0;
var itemsToRemove=0;
var endReached=false;
var message='';
var links = "Pura vida!!\r\nThe following are your news headings and the links to follow up: \r\n\r\n";
// console.log(request.session);
if (request && request.sessionAttributes && request.sessionAttributes.number) {
for(var c=0; c<request.sessionAttributes.number; c++){
postCollection.reverse().pop();
}
console.log('num: '+request.sessionAttributes.number+ 'post:'+postCollection);
itemsToRemove+=request.sessionAttributes.number;
}
postCollection.forEach(function (item) {
var toSay ='...'+ format(item.title) + '... ' + format(item.summary, item.title)+' <break time="1000ms"/> ';
if ((sayCount+toSay.length)>=7900) {
if (!endReached) {
message+='do you want to hear more ?';
console.log(message);
response.session('number',itemsToRemove);
response.say(message);
response.card('Costa Rica News by Andres Meza', links, 'http://google.com');
response.shouldEndSession(false,"You can say: yes, or no.");
response.send();
endReached=true;
}
}
else {
itemsToRemove=itemsToRemove+1;
sayCount+=toSay.length;
message+=toSay;
links+= item.title+'\r\n'+item.link+'\r\n\r\n';
}
});
if (!endReached){
console.log(message);
response.session('number',itemsToRemove);
response.say(message);
response.card('Costa Rica News by Andres Meza', links, 'http://google.com');
response.say("That's the end of your news, thank you!");
response.shouldEndSession(true);
response.send();
endReached=true;
}
});
return false; // This is how you tell alexa-app that this intent is async.
}
app.intent('OnYesIntent', {
"utterances":["yes","more","okay","ok"]
},OnStartIntent);
app.intent('OnNoIntent', {
"utterances":["no","leave",
"quit",
"bye",
"good bye",
"stop",
"cancel",
"enough",
"please stop"]
},exitIntent);
function exitIntent(request,response) {
response.clearSession(); // or: response.clearSession('key') to clear a single value
response.say("Thank you for using Costa Rica News, Good bye");
response.send();
};
//remove some extra data from my sources
function format(string, title){
if (string) {
string = string.replace('–', '-');
string = string.replace('The post', '');
string = striptags(string);
string = string.replace('appeared first on Costa Rica Star News', '')
if (title) {
string = string.substr(0, string.lastIndexOf(title));
if (string.indexOf('. ') > -1)
string = string.substr(0, string.lastIndexOf(". ") + 1);
}
}
else
{
string="";
}
return string;
}
// Output the schema
console.log( "\n\nSCHEMA:\n\n"+app.schema()+"\n\n" );
// Output sample utterances
console.log( "\n\nUTTERANCES:\n\n"+app.utterances()+"\n\n" );