forked from cakebake/node-red-contrib-alexa-remote-cakebaked
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathalexa-remote-other.js
84 lines (68 loc) · 4.48 KB
/
alexa-remote-other.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
const tools = require('../lib/common.js');
module.exports = function (RED) {
function AlexaRemoteOther(input) {
RED.nodes.createNode(this, input);
tools.assign(this, ['config', 'outputs'], input);
tools.assignNode(RED, this, ['account'], input);
if(!tools.nodeSetup(this, input, true)) return;
this.on('input', function (msg) {
const send = tools.nodeGetSendCb(this, msg);
const error = tools.nodeGetErrorCb(this);
if(this.account.state.code !== 'READY') return error('account not initialised');
this.status({ shape: 'dot', fill: 'grey', text: 'sending' });
const alexa = this.account.alexa;
const config = tools.nodeEvaluateProperties(RED, this, msg, this.config);
if(!tools.matches(config, { option: '' })) return error(`invalid input: "${JSON.stringify(config)}"`);
const {option, value} = config;
switch(option) {
case 'get': {
if(!tools.matches(value, { what: '' })) return error(`invalid input: "${JSON.stringify(config)}"`);
switch(value.what) {
case 'accounts': return alexa.getAccountPromise ().then(send).catch(error);
case 'contacts': return alexa.getContactsPromise ().then(send).catch(error);
case 'conversations': return alexa.getConversationsPromise ().then(o => o.conversations).then(send).catch(error);
case 'automationRoutines': return alexa.getAutomationRoutinesPromise ().then(send).catch(error);
case 'musicProviders': return alexa.getMusicProvidersPromise ().then(send).catch(error);
case 'homeGroup': return alexa.getHomeGroupPromise ().then(send).catch(error);
case 'notifications': return alexa.getNotificationsPromise ().then(o => o.notifications).then(send).catch(error);
case 'skills': return alexa.getSkillsExt ().then(send).catch(error);
case 'list':
if(!tools.matches(value, { list: '' })) return error(`invalid input: "${JSON.stringify(config)}"`);
return alexa.getListExt(value.list).then(o => o.values).then(send).catch(error);
case 'activities':
if(!tools.matches(value, { count: 10, offset: 1 })) return error(`invalid input: "${JSON.stringify(config)}"`);
return alexa.getCustomerHistoryRecordsPromise({ size: value.count, offset: value.offset }).then(send).catch(error);
case 'cards':
if(!tools.matches(value, { count: 10 })) return error(`invalid input: "${JSON.stringify(config)}"`);
return alexa.getCardsPromise(value.count, '%t').then(o => o.cards).then(send).catch(error);
default:
return error(`invalid input: "${JSON.stringify(config)}"`);
}
}
case 'addListItem':
if(!tools.matches(value, { list: '', text: '' })) return error(`invalid input: "${JSON.stringify(config)}"`);
return alexa.addListItemExt(value.list, value.text).then(send).catch(error);
case 'addNotification':
if(!tools.matches(value, { type: '', label: '', time: undefined, device: '', status: '', sound: undefined })) return error(`invalid input: "${JSON.stringify(config)}"`);
return alexa.createNotificationExt(value.device, value.type, value.label, value.time, value.status.toUpperCase(), value.sound).then(send).catch(error);
case 'changeNotification':
if(!tools.matches(value, { notification: undefined, label: '', time: undefined, status: '', sound: undefined })) return error(`invalid input: "${JSON.stringify(config)}"`);
return alexa.changeNotificationExt(value.notification, value.label, value.time, value.status.toUpperCase(), value.sound).then(send).catch(error);
case 'removeNotification':
if(!tools.matches(value, { notification: undefined })) return error(`invalid input: "${JSON.stringify(config)}"`);
return alexa.deleteNotificationExt(value.notification).then(send).catch(error);
case 'sendTextMessage':
if(!tools.matches(value, { conversation: '', text: '' })) return error(`invalid input: "${JSON.stringify(config)}"`);
return alexa.sendTextMessagePromise(value.conversation, value.text).then(send).catch(error);
case 'deleteConversation':
if(!tools.matches(value, { conversation: '' })) return error(`invalid input: "${JSON.stringify(config)}"`);
return alexa.deleteConversationPromise(value.conversation).then(send).catch(error);
case 'checkAuthentication':
return alexa.checkAuthenticationExt().then(send).catch(error);
default:
return error(`invalid input: "${JSON.stringify(config)}"`);
}
});
}
RED.nodes.registerType("alexa-remote-other", AlexaRemoteOther);
};