Build a SMS Haiku Generator with Twilio Serverless and Datamuse
Tags: serverless, javascript, webdev, node
Published: 2021-04-15 22:10:18

---
title: Build a SMS Haiku Generator with Twilio Serverless and Datamuse
published: true
description: Learn to build a haiku generator using Twilio Serverless Toolkit, Twilio SMS, and the Datamuse API in JavaScript to celebrate National Haiku Day and National Poetry Month!
tags: serverless, javascript, webdev, node
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/45l2p6h2dg5kkd5317ed.png
---
This blog post was written for [Twilio](https://www.twilio.com/) and [originally published on the Twilio blog](https://www.twilio.com/blog/sms-haiku-generator).

Get ready for National Haiku Poetry Day on April 17th and celebrate National Poetry Month all April! A haiku is a 3-line poem where the 1st and 3rd lines contain 5 syllables while the second line contains 7. This tutorial will go over how to build a haiku generator via SMS using the Twilio Serverless Toolkit and the Datamuse API. <strong>Text a word like "code" to +14243392702 to receive a haiku relating to that word, as seen below.</strong>
![example code as inb msg](https://twilio-cms-prod.s3.amazonaws.com/images/CwXq9jG7edy_PtD9FZitAaEOcblLeUVoiVwcJRmJVhAyR.width-1600.png)

### Prerequisites
1. A Twilio account - [sign up for a free one here and receive an extra $10 if you upgrade through this link](http://www.twilio.com/referral/iHsJ5D)
2. A Twilio phone number with SMS capabilities - [configure one here](https://support.twilio.com/hc/en-us/articles/223135247-How-to-Search-for-and-Buy-a-Twilio-Phone-Number-from-Console)
3. Node.js installed - [download it here](https://nodejs.org/en/download/)

### Make and Test the Function Locally
Let’s write a function that generates a haiku according to SMS input. The [Serverless Toolkit](https://www.twilio.com/docs/labs/serverless-toolkit) developed by my teammate [Dominik](https://twitter.com/DKundel/) helps us debug our [Twilio Function](https://www.twilio.com/docs/runtime/functions) with no hassle. For more details on installation and project structure, check out the [docs on how to develop and debug Twilio Functions locally](https://www.twilio.com/docs/labs/serverless-toolkit/getting-started).

The best way to work with the Serverless Toolkit is through the Twilio CLI. If you don't have the Twilio CLI installed yet, run the following commands to install it and the Serverless Toolkit:
```bash
npm install twilio-cli -g
twilio login
twilio plugins:install @twilio-labs/plugin-serverless
```
Create your new project titled <em>haiku-sms</em> by running the following commands:
```bash
twilio serverless:init haiku-sms 
cd haiku-sms
npm install syllable@3.3.0 superagent
```
Navigate into the <em>functions folder</em>. There are some files there called <em>hello-world.js</em> and <em>private-message.js</em>. Feel free to ignore or delete them, and then make a new file called <em>haiku.js</em> containing the following code to generate a haiku according to an input text message using the Datamuse API which simplifies searching words and phrases for developers and the [Syllable npm module](https://www.npmjs.com/package/syllable) for checking syllable counts in Node.js:
```javascript
const syllLib = require('syllable'); //check syllables of words
const superagent = require('superagent'); //make HTTP request to the Datamuse API
function makeLine (numSyllables, wordPool) { //make line with words given a number (haiku lines are 5, 7, 5) and possible words relating to the input word
  if (numSyllables == 0 || numSyllables != parseInt(numSyllables, 10)) {
    numSyllables = 5;
  }
  let line = '';
  let totalNumSyllables = 0;
  while (totalNumSyllables < numSyllables) {
    line += ' ' + wordPool[Math.floor(Math.random() * wordPool.length)].word;
    totalNumSyllables = syllLib(line);
    if (totalNumSyllables > numSyllables) {
      line = '';
      totalNumSyllables = 0;
    }
  }
  return line.trim();
};
exports.handler = async function(context, event, callback) {
  let twiml = new Twilio.twiml.MessagingResponse();
  let inbMsg = event.Body.toLowerCase().trim(); //get inbound word
  if(inbMsg.slice(-1) == "s") { //remove "s" presuming that means the word is plural because Datamuse returns more words relating to singular than plural words
    inbMsg = inbMsg.slice(0,-1);
  }
  superagent.get(`https://api.datamuse.com/words`) //hit the Datamuse API
  .query({rel_jja: inbMsg, max: 100}) //query words related to inbound SMS word
  .end((err, res) => {
    if(res.body.length == 0) { //Datamuse doesn't have any related words
      twiml.message(`Oh no I'm sorry \nYour haiku is out to eat \nTry a different word`); //haiku if no words related to input SMS
      return callback(null, twiml);
    }
    let haiku = `${makeLine(5, res.body)} \n${makeLine(7, res.body)}\n${makeLine(5, res.body)}`; //generate haiku by calling the makeLine function from above 3x with the input word and correct syllables for each line
    twiml.message(haiku);
    return callback(null, twiml);  
  });
}
```

### Configure the Twilio Function with a Twilio Phone Number
To open up our app to the web with a public-facing URL, navigate back up to the parent folder <em>haiku-sms</em> and run `twilio serverless:deploy` in your terminal. You should see an output similar to this at the bottom of your terminal:
![function URLs](https://twilio-cms-prod.s3.amazonaws.com/images/UXsMIORdEiYrb2Jwepfjri64n1mG43VNkeUTinFdNaLIw.width-1600.png)
You could copy the Function URL with the <em>/haiku</em> endpoint corresponding to your app to configure a Twilio number with, but you don't need to--it should appear in a dropdown in the console.

If you haven't done so already, [search for and purchase a Twilio phone number from the console](https://support.twilio.com/hc/en-us/articles/223135247-How-to-Search-for-and-Buy-a-Twilio-Phone-Number-from-Console). Make sure that the phone number you choose is set to the same country or region of your personal number to avoid international fees when you pick up calls from the number.

Select an [Active Number](https://www.twilio.com/console/phone-numbers/incoming) that you would like to host the haiku generator on. Scroll down to the <strong>Messaging</strong> section and configure the phone number by pasting the newly created webhook you just deployed.

Set the section <em>A message comes in</em> to "Function". For <em>Service</em>, find the Twilio Function you just created, also known as "haiku-sms". Select "dev-environment" for <em>Environment</em> and set the <em>Function Path</em> to "/haiku" as seen below:
![configure phone number](https://twilio-cms-prod.s3.amazonaws.com/images/ZvA1Vt2lQFrFwP2GsVlpBcyPzo2OXpUuh7dANVWvCWQAy.width-1600.png)
Click the <strong>Save</strong> button at the bottom and text anything to your Twilio phone number for a response like this one:
![pokemon sms to haiku](https://twilio-cms-prod.s3.amazonaws.com/images/rvQPxql0UQI9NoALc__iDuICthMl2t5KvV-krivNH3Ey9.width-1600.png)
### What's Next for Haikus and Code
![i am a poet gif](https://twilio-cms-prod.s3.amazonaws.com/original_images/poetgif.gif)
How will you celebrate National Haiku Poetry Day on April 17th, and National Poetry Month all month long? Make a haiku yourself, maybe generate one with machine learning, or use one that was generated for you in this post.

The [complete code can be found here on GitHub](https://github.com/elizabethsiegle/haiku-sms). There's so much you can do with the Twilio Serverless Toolkit--let me know online what you're building!

- Twitter: [@lizziepika](http://twitter.com/lizziepika)
- GitHub: [elizabethsiegle](http://github.com/elizabethsiegle)
- Email: lsiegle@twilio.com