-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
162 lines (131 loc) · 6.17 KB
/
index.js
File metadata and controls
162 lines (131 loc) · 6.17 KB
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
var Alexa = require('alexa-sdk');
var FB = require('facebook-node');
var util = require('util');
// Messages used for Alexa to tell the user
var repeatWelcomeMessage = "you should be able to read your feed using this skill.";
var welcomeMessage = "Welcome to the summary social alexa skill, " + repeatWelcomeMessage;
var stopSkillMessage = "Ok, see you next time!";
var helpText = "You can say things like read my feed, or summarize my social media, what would you like to do?";
var tryLaterText = "Please try again later."
var noAccessToken = "There was a problem getting the correct token for this skill, have you linked your account from the Alexa store?" + tryLaterText;
var months =["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var accessToken = "";
// Create a new session handler
var Handler = {
'NewSession': function () {
// Access token is pass through from the session information
accessToken = this.event.session.user.accessToken;
// If we have an access token we can continue.
if (accessToken) {
FB.setAccessToken(accessToken);
this.emit(':ask', welcomeMessage, repeatWelcomeMessage);
}
else {
// If we dont have an access token, we close down the skill. This should be handled better for a real skill.
this.emit(':tell', noAccessToken, tryLaterText);
}
},
// Read fb feed handler
'readFeedIntent': function () {
var alexa = this;
// Again check if we have an access token
if (accessToken) {
// Call into FB module and get my feed
// FB.api("/me/feed?fields=from,created_time,message,message_tags,story,comments,likes,reactions", function (response) {
var name = "";
FB.api("/me", 'get', {fields: 'name'}, function(response){
if (response && !response.error) {
name = response.name;
}
});
FB.api("/me/feed", 'get', {fields: 'message,from,created_time,likes,comments,status_type,application'}, function (response) {
if (response && !response.error) {
// If we have data
if (response.data) {
var output = "";
var max = 3;
// Take the top three posts and parse them to be read out by Alexa.
for (var i = 0; i < response.data.length; i++) {
if (i < max) {
var data = response.data[i];
var timestamp = data.created_time;
var date = new Date(timestamp);
var day = date.getDate();
var month = months[date.getMonth()];
var year = date.getFullYear();
var type = data.status_type;
type = type.split('_').join(' ');
var from = data.from.name;
if (from === name) from = "you";
var message = "";
if (data.message) message = data.message;
var likes = 0;
if (data.likes) likes = data.likes.data.length;
var comments = 0;
if (data.comments) comments = data.comments.data.length;
if (data.application && data.application.name === "Instagram")
type = "Instagram post";
output += "Post " + (i + 1) + ": ";
output += type + " from " + from + " on " + month + " " + day + ": " + year +
": with " + likes + " likes and " + comments + " comments: " + message + ". ";
}
}
alexa.emit(':ask', output, output);
} else {
// REPORT PROBLEM WITH PARSING DATA
}
} else {
// Handle errors here.
console.log(response.error);
}
});
} else {
this.emit(':tell', noAccessToken, tryLaterText);
}
},
// Write a post to Facebook feed handler.
'writePostIntent': function () {
var alexa = this;
// Chack if we have access tokens.
if (accessToken) {
FB.api("/me/feed", "POST",
{
// Message to be posted
"message": "This is Alexa, I can now access a whole new world of information, good luck!"
}, function (response) {
if (response && !response.error) {
// Alexa output for successful post
alexa.emit(':tell', "Post successfull");
} else {
console.log(response.error);
// Output for Alexa, when there is an error.
alexa.emit(':ask', "There was an error posting to your feed, please try again");
}
});
}else{
this.emit(':tell', noAccessToken, tryLaterText);
}
},
'AMAZON.CancelIntent': function () {
// Triggered wheen user asks Alexa top cancel interaction
this.emit(':tell', stopSkillMessage);
},
'AMAZON.StopIntent': function () {
// Triggered wheen user asks Alexa top stop interaction
this.emit(':tell', stopSkillMessage);
},
// Triggered wheen user asks Alexa for help
'AMAZON.HelpIntent': function () {
this.emit(':ask', helpText, helpText);
},
// Triggered when no intent matches Alexa request
'Unhandled': function () {
this.emit(':ask', helpText, helpText);
}
};
// Add handlers.
exports.handler = function (event, context, callback) {
var alexa = Alexa.handler(event, context);
alexa.registerHandlers(Handler);
alexa.execute();
};