DEV Community

SavagePixie
SavagePixie

Posted on

Asteroid App: Project Submission

What I built

For the Twilio Hackathon, I have built an automated WhatsApp account to which you can ask information about the closest asteroid to Earth at a particular date.

When a user sends a WhatsApp message, the API endpoint will try and parse it. If it is asking about asteroid information, it'll check for dates. If there are no dates present, it'll assume today. Otherwise, it'll parse the dates and use them as start and end of the period for which to issue the request to NASA's NeoWs API.

If the API is unable to parse the message, it will issue a request to Cat Facts API and respond with an apology and a cat fact.

If the parsing process is successful, it'll issue a request to NeoWs, glean the data returned and send it in a human-readable form.

Category Submission: Interesting Integrations

Demo

Here are some images of responses to different messages:

  1. A simple request asking about the closest asteroid to Earth. Since there is no date, today is assumed:
    Simple request

  2. A request including the start and end dates of a period:
    Request for a specific period

  3. A bad request. Something that the API can't understand:
    Bad request

    I apologise for the typo in the reply. I didn't realise it was there until the image was cropped and uploaded and didn't feel like going through the process again.

Link to Code

Asteroid App's code, together with its README and set up instructions is available under a MIT license in this git repository.

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

I originally intended to build this project with Elixir. But it proved to be more than I could chew. So I decided to go with JavaScript and build it on Node.

You can read about some of the issues and notes of the process in previous posts in this series. There I documented part of the journey and most of the issues I ran into.

Something new that I discovered along the way was the beauty of Either. Creating the request parser proved to be a bit of a challenge. Especially when I wanted to allow for custom date requests. In the beginning, I had simply coded it with a bunch of ifs. But it was a mess.

So I remembered Professor Frisby Introduces Composable Functional JavaScript, which I had been watching last week (if you don't know what an either is, by the way, I strongly recommend watching the first five videos. The explanation is simply brilliant). So I decided to try a different approach using eithers.

The result was simply beautiful. Now, instead of a bunch of nested ifs, the parser is this simple pipeline of transformations:

checkKeyWords(str.toLowerCase())
    .map(matchDate)
    .chain(parseDate)
    .map(buildObject)
    .fold(
        () => ({ result: 'notOk' }),
        x => ({
            result: 'ok',
            payload: x,
        })
    )
Enter fullscreen mode Exit fullscreen mode

Additional Resources/Info

I guess I'll use this section to say that building this project was a lot of fun. I got to learn how to integrate a Twilio service in my API and I experimented with functional libraries and types. The result is a pretty simple app, but I'm quite happy with the result. It does what I set out to accomplish.

Top comments (2)

Collapse
 
avalander profile image
Avalander

Great job! Congratulations on finishing the project!

Collapse
 
savagepixie profile image
SavagePixie

Thanks a lot!