BirthdayBot
@scale
code:
data:privatelast updated:1 month ago
today
Build integrations remarkably fast!
You're viewing a public workflow template.
Sign up to customize, add steps, modify code and more.
Join 800,000+ developers using the Pipedream platform
steps.
trigger
Cron Scheduler
Deploy to configure a custom schedule
This workflow runs on Pipedream's servers and is triggered on a custom schedule.
steps.
nodejs
auth
to use OAuth tokens and API keys in code via theauths object
code
Write any Node.jscodeand use anynpm package. You can alsoexport datafor use in later steps via return or this.key = 'value', pass input data to your code viaparams, and maintain state across executions with$checkpoint.
async (event, steps) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
}
111
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);

// What day is it again?
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);

// What day of the week is it? 0 = Sunday, 6 = Saturday
const dayNum = today.getDay();
if (dayNum === 0 || dayNum === 6) {
    return; // don't run on weekends, could also be set with the cron schedule...
}

// Birthday Message Base
let birthdayMsg = ""

// Birthday Emoji List
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.",
];

let noBirthdayNames = [
    "Charlotte Zhuang"
]

for (const event of Object.values(events)) {    
    if (event.summary) { // All of a sudden invites may not have a summary???
        const name = event.summary.replace(' - Birthday', ''); // Name of person

        if (noBirthdayNames.includes(name)) {
            continue;
        }

        isBirthday = false;
        
        if (dayNum > 0 && dayNum < 5) { // Monday - Thursday
            isBirthday = today.toISOString() == event.start.toISOString();
        } else { // It's Friday, because Sat / Sun are skipped above
            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' }); // April
            const day = event.start.getDate(); // 25
            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 // Automatically stringifies the body to JSON
});