Skip to content

Instantly share code, notes, and snippets.

@aakashlpin
Last active May 28, 2020 09:11
Show Gist options
  • Save aakashlpin/408ddaed59072531e3a806e00930e6eb to your computer and use it in GitHub Desktop.
Save aakashlpin/408ddaed59072531e3a806e00930e6eb to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
var inquirer = require('inquirer');
var fetch = require('isomorphic-unfetch');
var EMAILAPI_HOST = "https://emailapi.io";
console.log('πŸ‘‹πŸ½ Enter your details below to get details on Saturday Noon Livestream with Aakash.');
inquirer
.prompt([
{
type: 'input',
name: 'name',
message: 'Hey there! Enter your name:',
default: 'John Doe'
},
{
type: 'input',
name: 'email',
message: 'And now your Email id:',
default: '[email protected]'
},
])
.then(answers => {
fetch(`${EMAILAPI_HOST}/api/saturday-hacks/register`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(answers)
}).then(async r => {
return {
answers,
response: await r.json()
};
}).then(({answers, response}) => {
console.log(`\nYour registration is still pending. Check your mailbox for an email from us. \nIn the email, you'll find instructions on what to do next.\n\n`);
let attempt = 0;
const timer = setInterval(() => {
attempt = attempt + 1;
console.log(`[Attempt #${attempt}] waiting for your action..`);
fetch(`${EMAILAPI_HOST}/api/saturday-hacks/verify-email`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
email: answers.email,
})
}).then(async r => {
const data = await r.json();
if (data.status_code === 'RECEIVED') {
clearInterval(timer);
console.log('βœ… Email received!\n\n')
console.log(`Hi ${answers.name},\n\nThank you for verifying your email id! You've now successfully registered for Saturday Noon Livestream! πŸŽ‰ \n\nYou'll receive details about the live-stream by Friday each week. See you soon.\n\nCheers!\n- Aakash`);
}
}).catch(e => {
console.log(e);
})
}, 7000);
})
})
.catch(error => {
if(error.isTtyError) {
// Prompt couldn't be rendered in the current environment
} else {
// Something else when wrong
}
});
{
"name": "snl",
"version": "0.0.2",
"bin": "./index.js",
"dependencies": {
"inquirer": "^7.1.0",
"isomorphic-unfetch": "^3.0.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment