-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsms.js
37 lines (32 loc) · 1.2 KB
/
sms.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
let FROM_PHONE_NUMBER = process.env.RADAR_TWILIO_FROM_PHONE_NUMBER,
TO_PHONE_NUMBER = process.env.RADAR_TWILIO_TO_PHONE_NUMBER,
accountSid = process.env.RADAR_TWILIO_ACCOUNTSID,
authToken = process.env.RADAR_TWILIO_AUTHTOKEN,
mediaUrl = `${process.env.RADAR_HTTP_PROTO}${process.env.RADAR_HTTP_HOST}/${process.env.RADAR_IMAGE_PUBLIC_PATH}/image-`,
_twilio = require('twilio')(accountSid, authToken),
_redis = require('redis');
let redis = {
client: _redis.createClient()
};
redis.client.on('message', function (channel, message) {
console.log('SMS: client channel ' + channel + ': ' + message);
notify(message);
});
redis.client.on('ready', function () {
redis.client.subscribe('motion-channel');
});
function notify (message) {
console.log('message: ', message);
let filename = message.split('|')[3];
_twilio.messages.post({
to: TO_PHONE_NUMBER,
from: FROM_PHONE_NUMBER,
body: `${message} \n${mediaUrl}${filename}.jpg`,
// mediaUrl: `${mediaUrl}${fileName}` // MMS, but expensive
}, function (err, message) {
if (err) {
return console.error('Cannot send SMS', err);
}
console.log(message);
});
}