-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
154 lines (140 loc) · 4.8 KB
/
Copy pathindex.js
File metadata and controls
154 lines (140 loc) · 4.8 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
'use strict';
const Alexa = require('alexa-sdk')
const request = require('express-request-sign/make-signed-request')
exports.handler = function(event, context, callback) {
const baseUrl = process.env.CONTROL_SERVER_URL + '/'
const apiKey = process.env.API_KEY
const alexa = Alexa.handler(event, context)
const RETRY_RESPONSE = 'Sorry, please could you re-phrase that?'
const handlers = {
LaunchRequest: function () {
this.emit(':ask', 'What would you like Plex to do?', RETRY_RESPONSE);
},
OnDeckIntent: function () {
perfomBasicAction('ondeck', null, (error, response) => {
if (error) return
if (!response.success) {
console.log('ERROR getting on deck', response)
return this.emit(':tell', 'Sorry, this information is currently not available')
}
const speech = 'Currently available: ' + response.onDeckItems.join(', ')
this.emit(':tell', speech)
})
},
IplayerWhatsOnIntent: function () {
perfomBasicAction('channels', null, (error, response) => {
if (error) return
if (!response.success) {
console.log('ERROR getting channels', response)
return this.emit(':tell', 'Sorry, this information is currently not available')
}
const speech = 'Currently playing: ' + response.channels.join(', ')
this.emit(':tell', speech)
})
},
Unhandled: function () {
this.emit(':ask', RETRY_RESPONSE, RETRY_RESPONSE);
}
}
const basicHandlers = [
{ intent: 'AMAZON.PauseIntent', action: 'pause', message: 'OK, pausing' },
{ intent: 'AMAZON.ResumeIntent', action: 'resume', message: 'OK, resuming' },
{ intent: 'AMAZON.StopIntent', action: 'stop', message: 'OK, stopping' },
]
function createHandler(action, message) {
return function () {
let client = null
const slots = this.event.request.intent.slots
if (slots && slots.zone && slots.zone.value) {
client = slots.zone.value
}
perfomBasicAction(action.toLowerCase(), client, (error, response) => {
if (error) return
if (!response.success && response.client) {
return this.emit(':tell', 'Sorry, there is no client with the name: ' + response.client)
}
this.emit(':tell', message)
})
}
}
basicHandlers.forEach((h) => {
handlers[h.intent] = createHandler(h.action, h.message)
})
handlers.ContinueMediaIntent = continueMediaIntent
handlers.MovieIntent = movieIntent
handlers.IplayerChannelIntent = IplayerChannelIntent
alexa.registerHandlers(handlers)
alexa.execute()
function continueMediaIntent() {
playMedia.call(this, 'continue', 'media')
}
function movieIntent() {
playMedia.call(this, 'movie', 'media')
}
function IplayerChannelIntent() {
playMedia.call(this, 'iPlayer', 'channel')
}
function playMedia(action, titleSlotName) {
let name = null
let client = null
const slots = this.event.request.intent.slots
if (slots && slots.zone && slots.zone.value) {
client = slots.zone.value
}
if (slots && slots[titleSlotName] && slots[titleSlotName].value) {
name = slots[titleSlotName].value
}
performPlayAction.call(this, { name, client, type: action }, (error, response) => {
if (error) return
this.emit(':tell', 'Playing ' + response.result.title + ' in the ' + response.result.client)
})
}
function perfomBasicAction(action, clientName, cb) {
const options = {
url: baseUrl + action,
method: 'GET',
apiKey: apiKey
}
if (clientName) {
options.url += '?client=' + clientName
}
request(options, (error, res, body) => {
if (error) {
console.log('ERROR when trying to ' + action + ':', error)
this.emit(':tell', 'Sorry, something went wrong')
return cb(error)
}
body = JSON.parse(body)
cb(null, body)
})
}
function performPlayAction(data, cb) {
console.log('Performing Play Action', data)
const opts = {
url: baseUrl + 'play',
body: data,
json: true,
apiKey: apiKey,
method: 'POST'
}
request(opts, (error, res, body) => {
if (error) {
console.log('ERROR when trying to play:', error)
this.emit(':tell', 'Sorry, something went wrong')
return cb(error)
}
console.log('Play Action Response', body)
if (!body.success && body.error) {
console.log('ERROR when trying to play:', body.error)
return this.emit(':tell', 'Sorry, something went wrong')
}
if (!body.success && body.name) {
return this.emit(':tell', 'Sorry, could not find ' + body.name)
}
if (!body.success && body.client) {
return this.emit(':tell', 'Sorry, there is no client with the name: ' + body.client)
}
cb(null, body)
})
}
}