Last active
February 1, 2018 20:36
-
-
Save matthiasnys/6835012c846446cb4782af7a74980c86 to your computer and use it in GitHub Desktop.
The Birth Notifier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MIT License | |
// Copyright (c) 2018 Matthias Nys - B-NYS.com | |
// Permission is hereby granted, free of charge, to any person obtaining a copy | |
// of this software and associated documentation files (the "Software"), to deal | |
// in the Software without restriction, including without limitation the rights | |
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
// copies of the Software, and to permit persons to whom the Software is | |
// furnished to do so, subject to the following conditions: | |
// The above copyright notice and this permission notice shall be included in all | |
// copies or substantial portions of the Software. | |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
// SOFTWARE. | |
'use strict' | |
var GoogleSpreadsheet = require('google-spreadsheet') | |
var async = require('async') | |
var client = require('twilio')( | |
'TWILIO_ACCOUNT_ID', | |
'TWILIO_AUTH_TOKEN' | |
) | |
module.exports.sendMessage = function (event, context, callback) { | |
// spreadsheet key is the long id in the sheets URL | |
var doc = new GoogleSpreadsheet('THE_ID_OF_YOUR_SPREADSHEET') | |
var datasheet | |
var phonedatasheet | |
var lengte = '' | |
var gewicht = '' | |
var naam = '' | |
var datum = '' | |
var phonenumbers = [] | |
var message = '' | |
var website = '' | |
var twilioPhoneNumber = 'YOUR_TWILIO_PHONENUMBER' | |
async.series([ | |
function setAuth (step) { | |
var creds = { | |
// THE GOOGLE API CREDENTIALS | |
} | |
doc.useServiceAccountAuth(creds, step) | |
}, | |
function getInfoAndWorksheets (step) { | |
doc.getInfo(function (err, info) { | |
console.log('Loaded doc: ' + info.title + ' by ' + info.author.email) | |
info.worksheets.forEach(function (worksheet) { | |
if (worksheet.title === 'data') { | |
datasheet = worksheet | |
} else if (worksheet.title === 'phone-data-dummy') { | |
phonedatasheet = worksheet | |
} | |
}, this) | |
console.log('sheet 1: ' + datasheet.title + ' ' + datasheet.rowCount + 'x' + datasheet.colCount) | |
step() | |
}) | |
}, | |
function getData (step) { | |
datasheet.getRows({ | |
offset: 0, | |
limit: 1 | |
}, function (err, rows) { | |
console.log('Read ' + rows.length + ' rows') | |
// Only 1 row: | |
var row = rows[0] | |
lengte = row['lengte'] | |
naam = row['naam'] | |
gewicht = row['gewicht'] | |
datum = row['datum'] | |
website = row['website'] | |
console.log('Lengte: ' + lengte + ' naam: ' + naam + ' gewicht: ' + gewicht) | |
step() | |
}) | |
}, | |
function getSMSData (step) { | |
phonedatasheet.getRows({ | |
offset: 0, | |
limit: 200 | |
}, function (err, rows) { | |
console.log('Read ' + rows.length + ' rows') | |
// Only 1 row | |
rows.forEach(function (row) { | |
console.log('phone: ' + row.number) | |
phonenumbers.push(row.number) | |
}, this) | |
step() | |
}) | |
}, | |
function constructMessage (step) { | |
message = 'Matthias + Janneke = ' + naam + '. Onze kleine meid is geboren op ' + datum + '. Ze is ' + lengte + ' groot en weegt ' + gewicht + ' bloot. ' + website | |
console.log('Message: ' + message) | |
step() | |
}, | |
function sendMessage (step) { | |
async.forEach(phonenumbers, function (phonenumber, callback) { | |
console.log('send to: ' + phonenumber) | |
client.messages.create({ | |
from: twilioPhoneNumber, | |
to: phonenumber, | |
body: message | |
}, function (err, message) { | |
if (err) { | |
console.error(err.message) | |
} | |
callback() | |
}) | |
}, function (err) { | |
step() | |
}) | |
// step() | |
}, | |
function closeFeedback (step) { | |
console.log('finished sending!') | |
const response = { | |
statusCode: 200, | |
body: JSON.stringify({ | |
message: 'Finished Sending!', | |
input: event | |
}) | |
} | |
callback(null, response) | |
} | |
]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment