Wednesday, 14 April, 2021 UTC


Summary

Get ready for National Haiku Poetry Day on April 17th and celebrate National Poetry Month all April! A haiku is a three-line poem where the first and third lines contain five syllables while the second line contains two. 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.

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 
Create your new project by running the following commands:
twilio serverless:init haiku-sms cd haiku-sms npm install [email protected] superagent 
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'); const superagent = require('superagent'); function makeLine (numSyllables, wordPool) { 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(); if(inbMsg.slice(-1) == "s") { inbMsg = inbMsg.slice(0,-1); } superagent.get(`https://api.datamuse.com/words`) .query({rel_jja: inbMsg, max: 100}) //query words related to inbound SMS .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`); return callback(null, twiml); } let haiku = `${makeLine(5, res.body)} \n${makeLine(7, res.body)}\n${makeLine(5, res.body)}`; 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 haiku-sms and run twilio serverless:deploy in your terminal. You should see an output similar to this at the bottom of your terminal:
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:
Click the Save button at the bottom and text anything to your Twilio phone number for a response like this one:

What's Next for Haikus and Code

How will you celebrate National Haiku Poetry Day on April 17th? Make a haiku yourself, maybe generate one with machine learning, or use one that was generated for you in this blog 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!