Adding Alexa to Your Mendix App for More Custom IoT Features

This blog continues on the same app from a previous blog, on the Internet Button. You can read part one here.

In my previous post, I started building an app to help me record how I spend my time during the day. The first thing I built utilized The Internet Button to track my time. With the push of the button, I knew when I started and ended working on a specific task. Now that I’ve got this information stored in my Mendix app, I need to access it – enter Alexa!

Alexa lets me access this information without having to open a browser and go into my Mendix app. Since I need to enter my time into another window, not having to alternate tabs to view and enter information is great. This blog will guide you through the process of building out two pieces of functionality – one for Alexa to read your time entries back to you one by one, and another to email a summary of the day.

Setting up Alexa

The first task is setting up Alexa, which is a relatively straightforward process. If you have an Amazon account, you can enable the development portion. You can create a new Amazon account here. Once you’re logged in, navigate to “Alexa” and click “Get Started” for the Alexa Skills Kit:

Then select “Add a New Skill”. From here, create a “Custom Interaction Model” in English. Use Time Management for the “Name” and “Invocation Name”. There’s no need to enable any of the Global Fields, as you won’t be using them.

From here we build the Intents, an action that fulfills a user’s spoken request. You can follow their documentation here. This will be part of the structure of the JSON request, and you’ll define the intents that you’ll need in your Mendix app (more on the Mendix side later). Create three Intents: Email, List, and Next. Email will send you an email summary of your day. List and Next will allow you to have each time entry individually read to you – technically they’ll do the same thing (read the next time entry in the list), but having two commands makes the conversation with Alexa a bit easier. You can start by saying ‘Alexa, ask Time Management to list my time entries for today’ and then simply say ‘next entry’ to get the next one, rather than having to say, ‘list my time’ again.

The next page is Configuration – this is where you’ll define how to communicate with your Mendix app. Instead of going through AWS Lambda, you’re using a direct HTTPS connection. This requires a URL for the REST service of your app. Since it’s just you using this app, you’re not going to set up user accounts in your app (if you want to do this, you’ll need to build out an OAuth connection).

Since you’re using a direct HTTPS connection, on the next page you must assert that you have an active SSL certificate. Since your app is hosted in the Mendix Cloud, you automatically have this.

Now you’re at the point where you can start testing! On the Test page, you can use their Service Simulator to see sample requests and responses – note that you must complete the Mendix side of the service to get a valid response since it is hitting that service.

Connecting Alexa to Mendix

Now that you’ve got the service working on the Alexa side, let’s walk through the service on the Mendix side. Since Alexa supports REST services, you can take the sample request and response generated in the AWS environment and use that to create the corresponding JSON for Mendix. There’s a lot of information in these requests, but there are two pieces that are most important for this purpose, the Intent and the SessionID. The Intent is the command – which will be either Email, List, or Next. The SessionID is used to keep track of the Alexa that is accessing the data. This way, if you happen to be using two separate Alexa devices for some reason, you wouldn’t miss any items in your list functionality.

This example requests an Email summary of your time for sessionID SessionId.6f60d01c-3582-4b75-a00a-a5d3562a06e9. The first thing you do with your request is some required Alexa security (I won’t go too deep into that, but you can read about it here). From there, you’ll map the intent to the intended piece of logic:

The email summary sub-flow is fairly simple, so I won’t go into that too much. It simply generates an email that contains all the data and emails it:

The List activity is a bit more interesting:

Now you can have Alexa read each time entry aloud to you while you’re entering your time into the other system – much quicker than bouncing between browsers, calendars, and notebooks!

I’ve updated the Mendix app in GitHub, so you can take a closer look at how Alexa and the Internet Button work together. If you want to get started on your own Mendix app, I recommend creating a free account and starting with the online Introduction Course.