Zeltta logo image
Wed Oct 21 2020

How to Automate Email Responses to Typeforms with NodeJS



Typeform.com is a fantastic surveying tool. It lets you create powerful user-friendly forms that you can share via links or embed in web pages. I've personally used Typeforms in the past to conduct user feedback surveys and market research. One of the most powerful ways to use Typeform is as a lead generation tool. For example, asking website visitors to fill out a custom survey on your website in return for a customised evaluation, free ebook or guide. This allows you to generate and qualify leads while helping people understand their problem better and teaching how you can help. Currently, a client is using Typeforms to provide an easy way to trial and participate in one of their self-development programs. The tricky part for them, however, is sending the customised responses based on the survey results. They have a fantastic set of templates but it's starting to take up way too much manual time to send each survey participant their customised response.

What we're doing

To fix the problem, we've decided to set up a script that runs every 5 minutes and requests form submissions that were completed in the preceding 5 minutes. It then takes each submitted set of answers, combines them back with the original questions, prints them out into a templated email and sends it to the survey participant along with any attachments (if appropriate). It also sends a copy of the email to an admin email address to keep track of results.

What we're using

šŸ’» Typescript A type-safe "upgrade" of the Javascript programming language. Developed by Microsoft as an open-source project and has nothing to do with Typeform.com. (However, for simplicity, I've converted all code displayed here to plain old Javascript for easier reading) āš™ļø Node.js An open-source, server-side javascript runtime šŸ§° Cron An open-source library dedicated to scheduling recurring tasks. āœ‰ļø Twilio Sendgrid An email automation service built to enable easy programmatic emailing. ā›© Heroku by Salesforce A "serverless" app hosting platform that enables programmers to build and launch applications faster by focusing on code rather than web hosting infrastructure. šŸ“ Typeform.com As you know by now, an online service for making to people-friendly forms and surveys.

Step 1: Set up Node App

For more information on how to do this check out the links below.



Step 1: List of forms to automate

First, I made a list with every form I wanted to automate answers from, with ids, custom content and attachments if applicable. Find a form's id by looking at the ID code in its URL, for example, with admin1234.typeform.com/to/j7w2a6ZV, the form's id is j7w2a6ZV Note the use of <br/> to create line breaks in the final HTML email.

Step 2: Set up Cron

Set up an automatically recurring function. Cron is incredibly powerful, learn more about it by checking out the links below.



Step 3: Set up Repeating Script

Inside the Cron Job I have a loop that goes through the list of forms and performs three functions for each form. Function #1 Handles the fetching of Typeform answers. Function #2 Handles attachments Function #3 Sends the emails

Step 4: Set up the Typeform Fetching Function

I used the following function to fetch and convert Typeform responses into a list representing emails to send. #1 Get form submissions #2 Get form fields #3 Loop through submissions #4 Loop through fields and insert them into an array. #5 If a field has an answer, find and insert it along with the field. Notes: - I'm using Axios, a great Javascript library for performing API requests. - Typeform requires an auth token with each API request that you can find under "Personal tokens" in your Typeform account settings. - Yes I am making a 5 level deep, for-loop of death ā˜ ļø

Step 5: Set up emailing

My "sendEmail" function is just a lazy wrapper of SendGrid's Node Library, although I used a custom function called "makeHTMLTemplate" to form the actual HTML email.

Step 6: Create the HTML Email

The last step is to create the email. Someone has yet to build the best templating engine for formatting HTML emails* so I just do it with giant ugly strings; *Or I haven't bothered looking hard enough - I have removed all styling or this would look absolutely disgusting. - You can find great HTML email templates by searching on GitHub.

The result:

The resulting auto-responding emails look like this, with the Typeform answers nicely formatted, custom messages and attachments (not shown in pic).

That's it! Hope you found this interesting and or useful!!

Click an icon to share this article with someone who would like it!
Zeltta logo image