Skip to content

Instantly share code, notes, and snippets.

@alphanumeric0101
Last active September 22, 2017 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alphanumeric0101/e6d81be19eb201c80bb3f0b4de12f136 to your computer and use it in GitHub Desktop.
Save alphanumeric0101/e6d81be19eb201c80bb3f0b4de12f136 to your computer and use it in GitHub Desktop.
sending texts via twilio to taskers when a new shift is posted
var textClient = require('twilio')(process.env.TWILIO_ACCOUNT_SID, process.env.TWILIO_AUTH_TOKEN)
var sendText = textClient.messages.create({
to: phone,
from: taskiPhone,
body: messageBody
}, function (err, message) {
if (err) {
debug('Twilio error!')
console.log('texting error: ' + messageBody.length + ' chars')
console.log(err)
} else {
console.log(`sent a text to: ${phone}: ${messageBody}`)
}
})
this.alertTaskers = function (req, event) {
let sentTexts = []
Promise.all(event.shifts.map((s, i, a) => { // s is the new shift
let shift = Object.assign(s, {
venueName: event.venueName || null,
time: moment(s.startDate).format('ha'),
endTime: moment(s.endDate).format('ha'),
date: moment(s.startDate).format('dddd MMMM Do'),
company: event.company
})
return req.db.jobs.find({date: s.date}, {fields: {'jobid': 1, '_id': 0}})
.then(sameDayShifts => {
let conflictingShifts = sameDayShifts.map(cs => cs.jobid)
return req.db.users.find({
skills: s.position,
status: 'Accepted',
account_type: 'worker',
'notificationSettings.phone_job_match': true,
matched_jobs: { $nin: conflictingShifts }
}, { fields: { 'distance': 1, 'email': 1, 'location': 1, 'phone_number': 1, 'name': 1, 'firstName': 1, 'userid': 1 } })
.then(users => {
if (users.length) {
return Promise.all(users.map(u => {
if (u.distance && u.location && event.location) {
if (calculateDistance(event, u) <= u.distance && u.phone_number.length > 3 && !sentTexts.includes(u.phone_number)) {
sentTexts.push(u.phone_number)
sendText(u.phone_number, 30, u, shift, null)
return { email: u.email, alert: 'text sent' }
} else { return { email: u.email, alert: 'distance was wrong or duplicate' } }
} else { return { email: u.email, alert: 'missing fields in userdoc' } }
}))
} else { return { email: null, alert: 'no available users found' } }
})
})
}))
}
this.shiftStartReminder = function (job, database, done) {
let event = job.attrs.data.event
database.events.findOne({eventid: event.eventid})
.then(e => {
console.log(`reminding an event: ${e.eventName}`)
return Promise.all(e.all_accepted_users.map(userid => { return database.users.findOne({userid: userid}) }))
.then(usersArray => {
return Promise.all(usersArray.map(user => {
let msgType = e.all_confirmed_users.includes(user.userid) ? 70 : 33
sendText(user.phone_number, msgType, user, event, event.keyword)
sendEmail(14, user, null, event, event.keyword)
})).then(res => done())
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment