Skip to content

Instantly share code, notes, and snippets.

@navsing
Created April 15, 2020 01:06
Show Gist options
  • Save navsing/9d06b0c70ff486b651b23fb956cf62f2 to your computer and use it in GitHub Desktop.
Save navsing/9d06b0c70ff486b651b23fb956cf62f2 to your computer and use it in GitHub Desktop.
const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest();
var fs = require('fs')
fs.readFile('birthdays', 'UTF-8', function ( err, contents ) {
var birthdays = contents.toString().split('\n');
const bdayPeople = birthdayToday(birthdays);
greetBirthday(bdayPeople);
})
function birthdayToday(birthdays) {
var results = []
birthdays.map( (item) => {
const birthday = item.split(',')
const name = birthdays[0]
const date = birthdays[1]
var newDate = new Date();
const todayDate = (newDate.getMonth()+1) + '/' + newDate.getDate();
if (date === todayDate) {
results.push(name);
}
})
return results;
}
function greetBirthday(bdayPeople) {
postMessage(bdayPeople)
}
postMessage = (results) => {
//Return a Promise object that resolves when the HTTP request is done
return results.map( (item ) => {
return new Promise ( (resolve, reject) => {
let http = new XMLHttpRequest();
let postHook = < PLATFORM_API_TO_POST_MESSAGE_TO >
content = { Content: "![birthday](bday-gif-link) \n" + "Happy Birthday " + firstName + "!!" }
http.open('POST', postHook, true);
//This is the callback method that occurs after the HTTP request finishes
http.onLoad = () => {
//once we finish the HTTP request, resolve the promise
resolve(http.responseText);
}
http.setRequestHeader('Content-Type', 'application/json;charset=UTF-8');
http.setRequestHeader('X-Auth-Token', '<You may not need this>')
http.send(JSON.stringify(content))
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment