-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
109 lines (97 loc) · 2.56 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
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
const twilio = require('twilio');
const PLUGIN_ID = 'signalk-twilio-notifications';
const PLUGIN_NAME = 'Signalk twilio notifications';
var unsubscribes = [];
module.exports = function(app) {
var plugin = {};
plugin.id = PLUGIN_ID;
plugin.name = PLUGIN_NAME;
plugin.description = 'A plugin to send sms notifications when an event occurs';
plugin.start = function(options, restartPlugin) {
app.debug('Plugin started');
plugin.options = options;
let accountSid = options.account.Sid;
let authToken = options.account.token;
let from = options.account.from;
let client = twilio(accountSid, authToken);
options.notifications.forEach(option => listen(option, client, from, options.name));
app.setProviderStatus('Running');
};
function listen(option, client, from, name) {
let _notify = function(event) {
option.recipients.forEach(recipient => {
client.messages
.create({
body: `Alert from ${name}: ${option.message}`,
from: from,
to: recipient
})
.then(message => console.log(message.sid));
});
};
app.on(option.event, _notify);
unsubscribes.push(() => {
app.removeListener(option.event, _notify);
});
}
plugin.stop = function() {
// Here we put logic we need when the plugin stops
app.debug('Plugin stopped');
unsubscribes.forEach(f => f());
app.setProviderStatus('Stopped');
};
plugin.schema = {
title: PLUGIN_NAME,
type: 'object',
properties: {
name: {
type: 'string',
title: 'sender name'
},
account: {
type: 'object',
required: ['Sid', 'token', 'from'],
properties: {
Sid: {
type: 'string',
title: 'Account Sid'
},
token: {
type: 'string',
title: 'Token'
},
from: {
type: 'string',
title: 'Sender'
}
}
},
notifications: {
type: 'array',
title: 'notifications',
items: {
type: 'object',
properties: {
event: {
type: 'string',
title: 'event'
},
message: {
type: 'string',
title: 'message'
},
recipients: {
type: 'array',
title: 'Recipients',
items: {
type: 'string',
title: 'phone number'
}
}
}
}
}
}
};
return plugin;
};