-
Notifications
You must be signed in to change notification settings - Fork 0
/
Alexa_Skill_Anirudh_Menon.js
152 lines (129 loc) · 5.06 KB
/
Alexa_Skill_Anirudh_Menon.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
var https = require('https')
exports.handler = (event, context) => {
try {
if (event.session.new) {
// New Session
console.log("NEW SESSION")
}
switch (event.request.type) {
case "LaunchRequest":
// Launch Request .. Invocation Name: youtube data
console.log(`LAUNCH REQUEST`)
context.succeed(
generateResponse(
buildSpeechletResponse("This is Alexa skill for Liquid Studio test", true),
{}
)
)
break;
case "IntentRequest":
// Intent Request .. Total views, Total subscribers, Most viewed video
console.log(`INTENT REQUEST`)
switch(event.request.intent.name) {
case "GetSubscriberCount":
/*
Voice commands:
current subcriptions
number of subcriptions
number of subcribers
total subscribers
*/
//This youtube API endpoint gives channel information (my personal youtube channel in this case)
var endpoint = "https://www.googleapis.com/youtube/v3/channels?part=snippet,statistics&id=UCi_xD8D_Qn3LZoRU80NGM1A&key={PERSONAL_API_KEY}"
var body = ""
https.get(endpoint, (response) => {
response.on('data', (chunk) => { body += chunk })
response.on('end', () => {
var data = JSON.parse(body)
var title = data.items[0].snippet.title //Channel Name
var subscriberCount = data.items[0].statistics.subscriberCount //Get View count
context.succeed(
generateResponse(
buildSpeechletResponse(`Current subscriber count for ${title}'s channel is ${subscriberCount}`, true), //This is what Alexa reads you back
{}
)
)
})
})
break;
case "GetViewCount":
/*
Voice commands:
current views
total hits
total views
total clicks
*/
var endpoint = "https://www.googleapis.com/youtube/v3/channels?part=snippet,statistics&id=UCi_xD8D_Qn3LZoRU80NGM1A&key={PERSONAL_API_KEY}"
var body = ""
https.get(endpoint, (response) => {
response.on('data', (chunk) => { body += chunk })
response.on('end', () => {
var data = JSON.parse(body)
var title = data.items[0].snippet.title //Channel name
var viewCount = data.items[0].statistics.viewCount //Get count of total views on channel
context.succeed(
generateResponse(
buildSpeechletResponse(`Current view count for ${title}'s channel' is ${viewCount}`, true), //Alexa's outputSpeech
{}
)
)
})
})
break;
case "GetMostViewed":
/*
Voice commands:
most viewd vide
maximum viewed video
most popular video
*/
//This endpoint gives information about all the videos in youtube channel (my personal youtube channel in this case)
var endpoint = "https://www.googleapis.com/youtube/v3/search?part=snippet&forMine=true&maxResults=25&order=viewCount&type=video&key={PERSONAL_API_KEY}"
var body = ""
https.get(endpoint, (response) => {
response.on('data', (chunk) => { body += chunk })
response.on('end', () => {
var data = JSON.parse(body)
var title = data.items[0].snippet.title //Since it is ordered based on viewCount, first object in the items list will be the video with maximum views
context.succeed(
generateResponse(
buildSpeechletResponse(`Video with most views is ${title}`, true),
{}
)
)
})
})
break;
default:
throw "Invalid intent"
}
break;
case "SessionEndedRequest":
// Session Ended Request
console.log(`SESSION ENDED REQUEST`)
break;
default:
context.fail(`INVALID REQUEST TYPE: ${event.request.type}`)
}
} catch(error) { context.fail(`Exception: ${error}`) }
}
// Helpers
//Builds output text for Alexa to read back in Plain text format with EndSession flag true in all cases
buildSpeechletResponse = (outputText, shouldEndSession) => {
return {
outputSpeech: {
type: "PlainText",
text: outputText
},
shouldEndSession: shouldEndSession
}
}
//Creates a response object to be sent to amazon services which is then deciphered TTS and read back to user from above helper function
generateResponse = (speechletResponse, sessionAttributes) => {
return {
version: "1.0",
sessionAttributes: sessionAttributes,
response: speechletResponse
}
}