DEV Community

Rafael Triantafillidis
Rafael Triantafillidis

Posted on

No time to sleep. Ding! It's time to work.

[NOTE: I speak really bad English so bear with me for about 3 mins, or just look at the code(which is far more understandable in my opinion)).

What I built

Hello everyone,

I build an alarm job using several great npm packages and of course the Twilio API. The purpose of this alarm is to wake me up on a schedule via an SMS. The user can adjust his schedule via a json file(so it's super easy) and then magic happens. The app creates a cron job, that triggers on the date times of the json.

TADaaa
Alt Text

Link to Code

The code is open source and is located at https://github.com/alator21/Ding-time-to-work. It is licenced with the Apache 2.0 licence.

How I built it (what's the stack? did I run into issues or discover something new along the way?)

My stack contains:

  • Node.js
  • Typescript

It all started today when i randomly saw the twilio hackathon announcement.It caught my interest immediately. I knew that i could create something interesting with such amazing service as Twilio.I am fammiliar with typescript and Node.js so i was certain what stack i would use. Then i remembered that i have trouble waking up so that was my idea.

The first problem i encountered was, the timing. I hadn't use a cron job again so it was kinda unclear. Then i found the awesome npm package node-scedule(https://www.npmjs.com/package/node-schedule). It simplified my work instantly.It has an very easy and predictable API, allowing you to create easy cron jobs.

After i found the core function of the program, i had to design the classes.

Classes:

  1. Moment. This class represents a single moment we want the alarm to ring.
  2. Schedule. This class represents the whole schedule of days and times when the alarm must ring.
  3. Alarm. This class get as an input, a schedule, and alarms on when its time.

The rest was easy. I created a json file(schedule.json) and i parse it. Then i had to convert days names to number(0,6). There was a catch there. Javascript's Date function has a getDate() that return the index of the name.To my surprise the index of Monday was 1 and 0. The index 0 is Sunday. Other than that the rest was was pretty straighforward.

Instruction on how to run the program, are on the github(README).

Thanks for reading

Additional Resources/Info

https://www.npmjs.com/package/node-schedule
https://pm2.keymetrics.io/

Top comments (0)