Sending SMS Templates with Jinja2 and Python

Published December 17, 2018 by Aaron Bassett

Need to send a message to many people at once? Want to personalise it so that it doesn’t seem so, well, impersonal? Can’t send a group message as you don’t want your mum seeing you sent the same heartfelt text to every one of your relations and not just her? We’re going to look at how to create an SMS client which supports bulk messages and templates for personalisation.

Getting Started

You’ll need a bit of experience with Python to run the CLI, and at least Python 3.6 as we’re going to be using f-strings.

  1. Clone the source from GitHub
  2. Install the dependencies into your virtual environment; I recommend using pipenv and installing the package in editing mode: pipenv install -e .
  3. Create the following environmental variables; NEXMO_KEY, NEXMO_SECRET, and MY_NUMBER the first two are available from your Nexmo Dashboard. MY_NUMBER is your mobile number in the international E.164 format.

Managing SMS Contacts

Creating a new contact via the CLI

Our application is going to need a basic CRM to hold our contacts. We’re going to use tinydb for our persistence layer as we don’t need anything too complicated and as a bonus, it stores the data as a JSON file, which makes backups and manual edits of the data straightforward.

As well as the contact’s name and telephone number we have a few multiple choice values we collect.

When we render the template for a message, we’ll supply a random value from these lists in the context, give the messages some variation.

Managing Our SMS Templates

Our templates are going to be rendered using Jinja2 giving us access to all of Jinja’s built-in filters and features. We also supply information about each contact in the context when rendering the template.

Creating a new template via the CLI

I’ve made liberal use of Jinja’s random filter so that there is some variation in my messages, even if I resend the same person the same message multiple times it should look as if I’ve taken the time to write it personally each time. For added authenticity, you can randomly add in the odd typo from time-to-time.

{{ greeting }} {{ diminutive }} {{ ["gah", "sorry", "I suck", "soz"]|random }}, I'm {{ ["running", "runnin", "runing"]|random }} about {{ range(5,25)|random }} {{ ["mins", "minutes", "mintes"]|random }} late. {{ valediction }}

Screenshot of phone with multiple SMS

The rest of our template management code looks very similar to the contacts, although we do have a crude drill-down/expand so that you can quickly view the template contents.

Sending Multiple SMS with Python

Sending multiple/bulk SMS via the CLI

With the Nexmo Python Client sending an SMS is a single function call; most of our SMS sending code is to allow the user to select which numbers are going to receive the message, and which template we should use.

I modified the code above to log the results of my Christmas message so you can see the sort of variation it produces across different contacts.

Multiple messages output from bulk SMS CLI in Python

Leave a Reply

Your email address will not be published.