DEV Community

Cover image for Build a SMS Haiku Generator with Twilio Serverless and Datamuse
Lizzie Siegle for Twilio

Posted on • Edited on

Build a SMS Haiku Generator with Twilio Serverless and Datamuse

This blog post was written for Twilio and originally published on the Twilio blog.

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. Text a word like "code" to +14243392702 to receive a haiku relating to that word, as seen below.
example code as inb msg

Prerequisites

  1. A Twilio account - sign up for a free one here and receive an extra $10 if you upgrade through this link
  2. A Twilio phone number with SMS capabilities - configure one here
  3. Node.js installed - download it here

Make and Test the Function Locally

Let’s write a function that generates a haiku according to SMS input. The Serverless Toolkit developed by my teammate Dominik helps us debug our Twilio Function with no hassle. For more details on installation and project structure, check out the docs on how to develop and debug Twilio Functions locally.

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:

npm install twilio-cli -g
twilio login
twilio plugins:install @twilio-labs/plugin-serverless
Enter fullscreen mode Exit fullscreen mode

Create your new project titled haiku-sms by running the following commands:

twilio serverless:init haiku-sms 
cd haiku-sms
npm install syllable@3.3.0 superagent
Enter fullscreen mode Exit fullscreen mode

Navigate into the functions folder. There are some files there called hello-world.js and private-message.js. Feel free to ignore or delete them, and then make a new file called haiku.js 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 for checking syllable counts in Node.js:

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);  
  });
}
Enter fullscreen mode Exit fullscreen mode

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 haiku-sms and run twilio serverless:deploy in your terminal. You should see an output similar to this at the bottom of your terminal:
function URLs
You could copy the Function URL with the /haiku 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. 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 that you would like to host the haiku generator on. Scroll down to the Messaging section and configure the phone number by pasting the newly created webhook you just deployed.

Set the section A message comes in to "Function". For Service, find the Twilio Function you just created, also known as "haiku-sms". Select "dev-environment" for Environment and set the Function Path to "/haiku" as seen below:
configure phone number
Click the Save button at the bottom and text anything to your Twilio phone number for a response like this one:
pokemon sms to haiku

What's Next for Haikus and Code

i am a poet 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. There's so much you can do with the Twilio Serverless Toolkit--let me know online what you're building!

Top comments (0)