const request = require("request");
const rp = require("request-promise");
const ical = require('node-ical');
const bdayIcal = await rp(process.env.ICAL_FILE_URL);
const events = ical.sync.parseICS(bdayIcal);
const today = new Date();
today.setHours(0,0,0,0);
const tomorrow = new Date(today);
tomorrow.setDate(today.getDate() + 1);
const nextDay = new Date(today);
nextDay.setDate(today.getDate() + 2);
const dayNum = today.getDay();
if (dayNum === 0 || dayNum === 6) {
return;
}
let birthdayMsg = ""
let birthdayEmojis = [
'cake',
'tada',
'sparkling',
'fast_parrot',
'fiesta_parrot',
'partying_face',
'amaze',
'cool-doge',
'floss_avocado',
'highfive',
'mushroom_dance',
'party_pikachu',
'yay-avocado',
'catjam',
'happy_dance',
'dogepls',
'dancing_doge',
'yay',
'charmander_dancing',
'yay_tomato'
]
const nScaliens = Object.values(events).length;
const prob = ((364.0/365.0)**nScaliens)*100;
let noBirthdayMsgs = [
"No birthdays today :flushed: let's go hire some more Scaliens!",
`No one was born today :sad-panda: Assuming ${nScaliens} Scaliens, that's a ${prob.toFixed(1)}% likely outcome. :nerd_face:`,
"Nothing to celebrate today :sad_parrot: Our parents must have had better things to do 39 weeks and some years ago :woman-shrugging: :man-shrugging:",
"Not a single Scalien was born on this day :wow:",
"A very happy unbirthday to us all! None of us have birthdays today... :crying-swag:",
"If you have a clever remark about no one having a birthday today, this is the time to share it. :think-up: (No, seriously, we'll add it to the list)",
"Today's not your birthday, but at least you're in good company :heart-scale: - It's no one's birthday today at Scale.",
"No birthdays today. Koan: A blank space on the tapestry of life. If no thread is woven this day, is the tapestry any less complete?"
];
let noBirthdayNames = [
"Charlotte Zhuang"
]
for (const event of Object.values(events)) {
if (event.summary) {
const name = event.summary.replace(' - Birthday', '');
if (noBirthdayNames.includes(name)) {
continue;
}
isBirthday = false;
if (dayNum > 0 && dayNum < 5) {
isBirthday = today.toISOString() == event.start.toISOString();
} else {
isBirthday = (
today.toISOString() === event.start.toISOString() ||
tomorrow.toISOString() === event.start.toISOString() ||
nextDay.toISOString() === event.start.toISOString()
)
}
if (isBirthday) {
const month = event.start.toLocaleString('default', { month: 'long' });
const day = event.start.getDate();
const emojiNum = Math.floor(Math.random() * birthdayEmojis.length);
birthdayMsg = birthdayMsg + `> *${name}* - ${month} ${day} :${birthdayEmojis[emojiNum]}:\n> \n`;
}
}
};
if (birthdayMsg) {
birthdayMsg = ':birthday: *A very happy birthday to the following Scaliens* :birthday:\n\n' + birthdayMsg
} else {
birthdayMsg = noBirthdayMsgs[today.getDate() % noBirthdayMsgs.length];
}
console.log(birthdayMsg);
await rp({
method: 'POST',
uri: process.env.BIRTHDAY_BOT_WEBHOOK_URL,
body: {
text: birthdayMsg
},
json: true
});