Skip to content

Instantly share code, notes, and snippets.

@navsing
Last active April 15, 2020 00:58
Show Gist options
  • Save navsing/fea0d1179e516becc0c865c958677c2c to your computer and use it in GitHub Desktop.
Save navsing/fea0d1179e516becc0c865c958677c2c to your computer and use it in GitHub Desktop.
const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest();
const mysql = require('mysql');
const config = require('./config');
const pool = mysql.createPool({
connectionLimit: 10,
host: config.dbhost,
user: config.dbuser,
password: config.dbpass,
database: config.dbname
});
pool.getConnection( ( err, connection ) => {
//Use the connection
connection.query('SELECT name from birthday where DATE_FORMAT(birthday, '%m-%d') = DATE_FORMAT(now(), '%m-%d')', ( error, results, fields ) => {
//When done with the connection, release it
connection.release();
//Handle error after the release
if ( error ) throw error;
//Get the list of promises (async requests) for the HTTP requests
let promises = postMessage(results);
//Once all the promises have resolved, kill the process
Promise.all(promises).then((results) => {
process.exit();
});
});
});
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