Asteroid App: Project Submission
Tags: twiliohackathon, javascript, node
Published: 2020-04-17 10:57:51

[Comment]: # (All of this is placeholder text. Use this format or any other format of your choosing to best describe your project.)

[Reminder]: # (Make sure you've submitted the Twilio CodeExchange agreement: https://ahoy.twilio.com/code-exchange-community)

[Important]: # (By making a submission, you agree to the competition's terms: https://www.twilio.com/legal/twilio-dev-hackathon-terms)

## 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](https://api.nasa.gov/index.html#browseAPI).

If the API is unable to parse the message, it will issue a request to [Cat Facts API](https://catfact.ninja/) 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](https://i.ibb.co/hfpFCDD/screen1.png)

2. A request including the start and end dates of a period:
    ![Request for a specific period](https://i.ibb.co/bJFmJGm/screen2.png)

3. A bad request. Something that the API can't understand:
    ![Bad request](https://i.ibb.co/JvXXYq0/screen3.png)
    <figcaption>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.</figcaption>

## 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](https://github.com/SavagePixie/asteroid-app).

## 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 `if`s. But it was a mess.

So I remembered [Professor Frisby Introduces Composable Functional JavaScript](https://egghead.io/courses/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 `either`s.

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

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

## 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.