Build a SMS Haiku Generator with Twilio Serverless and Datamuse
Time to read: 2 minutes
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 seven. 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
- A Twilio account - sign up for a free one here and receive an extra $10 if you upgrade through this link
- A Twilio phone number with SMS capabilities - configure one here
- 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:
Create your new project by running the following commands:
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:
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:
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!
- Twitter: @lizziepika
- GitHub: elizabethsiegle
- Email: lsiegle@twilio.com
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.