interactive scavenger hunt

How to Create an Interactive Scavenger Hunt with Nexmo’s SMS and Voice API

Published August 14, 2019 by Lauren Lee

Introduction

QR codes are everywhere these days: on the back of business cards, on flyers, or billboards—you name it. They make information quickly accessible for anyone with a smartphone and thus have become popularized in the past decade.

Today we are going to capitalize on that functionality to create an interactive scavenger hunt throughout your city!

🔎 A Scavenger Hunt App 🔎

I figure, what better way to get your friends and family up off the couch and out into the neighborhood than with an interactive scavenger hunt? And thus I thought it’d be fun to design and build something with Nexmo’s Voice and SMS APIs to do just that. This tutorial will cover how to create QR Codes to hide throughout your neighborhood and how to create an app with Express and Node.js to populate a corresponding SMS to send to a Nexmo phone number that will call the player back with a recorded clue! The hunt can continue up to however many clues YOU choose.

The Flow of the App:

  • User finds a QR code
  • Scans QR code with their smartphone
  • SMS is prepopulated to with a clue to be sent to a Nexmo phone number
  • User hits send
  • App initializes a phone call
  • Recording of the clue plays
  • User searches for that new clue and the game continues!

flow of app

Prerequisites

To work through this tutorial, you will need a Nexmo account. You can sign up now for free if you don’t already have an account.

But, if you want to skip to a working project you can remix the QR Code Reader App right away in Glitch.

Otherwise, in just a few steps you can create your own clues and app from scratch!

Record Your Scavenger Hunt Clues

Using your preferred recording device, record the clues for your scavenger hunt as .ogg files. Here’s an example of one of my clues:

(HINT: it leads to the Fremont Troll under a bridge here in Seattle 😆)

fremont troll

Create a Nexmo Account

If you haven’t done so already, create a Nexmo account for free, and as an added bonus, your account will be credited with 2 euros to begin using your new application. Head over to https://dashboard.nexmo.com/sign-up and go through the signup steps. Once you have finished you will be in your Nexmo dashboard.

Purchase a Nexmo Phone Number

From the Nexmo Dashboard, click on the Numbers menu item on the left-hand side.

Click the Buy Numbers option and you’ll be directed to a page where you can choose a country, features, type, and four digits you would like the number to have.

buy numbers dashboard
Select the country that you are currently in so the call is local. For features, select Voice and for type, either mobile or landline will work just fine.

Click Search to see a list of phone numbers available.

Select a number by clicking the orange Buy button, and clicking the orange Buy button again once you’re in the confirmation prompt.

You now own a Nexmo phone number. Your next step is to create an application in the Nexmo dashboard.

Create a Nexmo Voice Application

From the left-hand menu, click on the Voice menu item.

Select the Create an application option. You are directed to a page where you can set up a new Nexmo application.

Complete the form with the following:

Application name text field enter qr code scavenger hunt

Event URL text field enter your Glitch URL: https://[your Glitch URL].glitch.me/events

Answer URL text field enter your Glitch URL again: https://[your Glitch URL].glitch.me/answer

Once that is all in there, click the blue Create Application button.

Be sure to generate a public/private key pair and save it.

Link Everything Together

You now have a Nexmo number and a voice application, all that is left to do is link the two together.

Select the Numbers menu option in the left-hand menu and then click on Your numbers. You’ll be directed to a page listing the phone number you just purchased.

Click on the gear icon on the right-hand side of the page and a menu will pop up.
In the Inbound Webhook URL field enter your Glitch URL followed by /smsInbound: https://[your Glitch URL].glitch.me/smsInbound`

Select Application under the Forward to area

Select your new application from the drop-down list under the Application area.
Press the blue Ok button

inbound webhook dashboard

Your new Nexmo voice number is now linked to your new Nexmo application, and with that last step, you are ready to build your application!

Troubleshooting

On the Nexmo dashboard, under your name and the account settings, be sure to select POST-JSON as your default HTTP METHOD as so:

POST-JSON

Getting Started on Glitch

To begin, create a new project on Glitch, choosing the hello-express template.

In the package.json file, select the Add a package dropdown to search for and add the following dependencies: nexmo, qrcode, body-parser, and dotenv.

dependency list

At the top of your server.js file, let’s set up our Express server, require our dependencies, and embed our Nexmo credentials:

In the .env file, fill in those Nexmo specific credentials we just created in the Nexmo Dashboard:

Replace the API Key, API Secret, App ID, and your Nexmo Number. To include the Private Key, select the New File toggle in the top left corner and name it .data/private.key. In that file paste the private key you generated when you created your voice application. This file will become invisible within Glitch if you remix your code for security reasons.

Create Routes in server.js

Next, beneath those credentials, in addition to our default route and the listener, let’s create a few different routes:

The first functions we need to fill in are for /events and /answer which both will respond with the send() functionality if the status response is 200:

Then for the /playRecording route, you will dynamically direct the app to play the recordings of your scavenger hunt clues:

Lastly, for the /smsInbound route, create a switch statement that will handle which clue is found. You can name them whatever you like (I’d recommend getting a bit more creative than my boring numerical names 😂).

Then, still within the /smsInbound route, connect to the Nexmo Voice API with nexmo.calls.create() which will then call the /playRecording route and function to stream the correct clue. And finally send the function if the response is 200.

Generate QR Codes

Next, in your index.html file, let’s create the QR Codes. I used the qrcode-generator CDN to dynamically generate codes based on the amount of clues. This is what the <body /> of my index.html looks like:

Be sure to replace YOUR_NEXMO_NUMBER in the createQRCode() function with your Nexmo number that you send the SMS to.

The last thing we need to do is create a assets.js file and fill it in with logic to handle Glitch’s assets and clues so that we can use the CDN.

final app view frontend

Happy Hunting! 🕵🏼‍

Congrats! We have successfully generated QR codes onto the frontend of our webpage. You can take those and place them at their corresponding clue locations throughout your city or neighborhood!

Once a scavenger hunter finds a particular clue, they should be able to scan it with their smartphone and a prepopulated text message will be prepared and ready to send to your Nexmo phone number. Once that SMS is successfully sent, the scavenger hunter will receive a phone call from that same number with the audio recording of their next clue. The hunt can then continue!

Leave a Reply

Your email address will not be published.