-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (56 loc) · 1.62 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
const crypto = require('crypto');
const request = require('request-promise');
const { Plugin } = require('jovo-framework');
class AlexaNotifications extends Plugin {
constructor(options) {
super(options);
this.uri = 'https://api.amazonalexa.com/v2/notifications';
this.expiresAfterSeconds = options && options.expiresAfterSeconds || 60;
}
init() {
this.app.on('request', jovo => {
const platformType = jovo.getType();
if (platformType !== 'AlexaSkill') return;
const {request} = jovo.requestObj;
if (request.type === 'Messaging.MessageReceived') {
this.token = jovo.requestObj.context.System.apiAccessToken;
const body = this.createNotificationBody(request.message);
return this.sendNotification(body);
}
});
}
createNotificationBody({displayInfo, spokenInfo}) {
if (!spokenInfo.text && !spokenInfo.ssml) {
throw new Error('Notification has no spoken text');
}
return {
displayInfo: {
content: [displayInfo]
},
referenceId: crypto.randomBytes(16).toString("hex"),
expiryTime: this.setExpiryTime(),
spokenInfo: {
content: [spokenInfo]
}
};
}
sendNotification(body) {
return request({
uri: this.uri,
method: 'POST',
headers: { 'Authorization': `Bearer ${this.token}` },
body,
json: true
})
.catch(err => {
console.log('USER NOTIFICATION SEND ERR', err.message);
return [];
});
}
setExpiryTime() {
const expiry = new Date();
expiry.setTime(expiry.getTime() + (this.expiresAfterSeconds*1000));
return expiry.toISOString();
}
}
module.exports = AlexaNotifications;