Invoking OpenFaaS serverless functions from Siri
We figured out how to trigger OpenFaaS functions from Siri and interact with Docker/Kubernetes via the new Shortcuts app in iOS 12. Serverless Siri!
With the advent of iOS 12, the Workflow app has been replaced with Shortcuts which is the new app for creating custom shortcuts. In iOS 12, they can also be triggered by custom phrases in Siri.
I wanted to see what Shortcuts is capable of doing so I challenged myself:
Create a shortcut which allows the user to select a function from an OpenFaaS instance and run it with custom input
I wasn’t too sure where to start, but opted to go with retrieving the list of functions from an instance first. This is fairly easy as OpenFaaS exposes gateway:8080/system/functions
for getting a list of functions and their statuses.
The next step was to figure out how to do basic HTTP authentication since newer versions of OpenFaaS enable this by default. This was surprisingly trivial as Shortcuts allows us to set headers on HTTP calls and even Base64 encode strings. Then all I had to do was save that into a variable for use in the request headers. Nice!
Doing the HTTP call was fairly straightforward too, all you need to do is use the “Get Contents of URL” action (Apple named some of these actions a bit strangely).
This will retrieve the list of functions as JSON, however we need to be able to display a list of them for the user to choose from. This was the hardest part, but luckily Shortcuts includes a few regex actions so we can use those.
Amazingly, you can just hook up the “Get Group from Matched Text” action straight to the “Choose from List” one and it works perfectly:
After that, it was plain sailing. I just added a prompt for the request body, plus another HTTP call and injected the appropriate variables. Lastly, I wired the output up to the “Quick Look” action as it is the most effective at showing the output from functions.
Here’s a quick demo:
If you want to have a play with this yourself, all you need to do is install the shortcut. Then just edit it in the Shortcuts app to use your username/password and gateway and give it a go.
Serverless Siri
You can even allow Shortcuts to be triggered from Siri in the Shortcut’s settings under “Siri Phrase”.
Other samples
Here are few other sample Shortcuts Javier Revillas and I have compiled:
- Whalesay - run an ad-hoc
whalesay
container over SSH (requires Docker to be installed on the host) - Server Status - login to a server over SSH and perform some status commands (uptime, memory, CPU, disk etc). Ensure you set the host, username and password correctly in the shortcut.
- List Docker containers - login to a server via SSH and list the containers that are running
Next steps
If you’ve made some shortcuts of your own, please do send us a link so we can put them on the list.
As always, head over to openfaas/faas on GitHub and give it a 🌟 - as I was writing this, we reached the 11.5k mark!
Make sure to tweet @developius and @javirevillas to show us what you’ve got working, we’d love to see it!