What Can I Do With Alexa at Work
If y'all are an SAP Employee, delight follow united states of america on Jam.
Since a long time…I had the idea of making Unity3D and Alexa work together…nonetheless…other project kept me away for really doing it…so…a couple of days agone…a conversation with a friend made me remember that I actually really wanted to do this…and then I did 🙂
At first…I wasn't exactly sure how to do information technology…simply then slowly the main thought came into my mind…what if Unity read a webservice that gets updated by Alexa? When the right command is parsed, and so Unity will create the object and bug is solved…seems like shooting fish in a barrel? Well…it actually is…
First things starting time…we need to create a small NodeJS webserver on Heroku …then…nosotros need to install the Heroku Toolbelt …
Now…create a binder chosen node_alexa and inside create the following files…
bundle.json
{ "dependencies": { "express": "iv.13.3" }, "engines": { "node": "0.12.7" } }
procfile
web: node index.js
index.js
var express = require('express') ,app = limited() ,last_value; app.gear up('port', (process.env.PORT || 5000)); app.get('/', function (req, res) { if(req.query.command == ""){ res.send("{ \"command\":\"" + last_value + "\"}"); }else{ if(req.query.control == "empty"){ last_value = ""; res.transport("{}"); }else{ res.send("{ \"command\":\"" + req.query.command + "\"}"); last_value = req.query.command; } } }) app.heed(app.become('port'), function () { panel.log("Node app is running on port', app.get('port')"); })
One time you have that…log into your Heroku Toolbelt and write the post-obit…
Heroku Toolbelt
cd node_alexa git init . git add . git commit -m "Init" heroku apps:create "yourappname" git button heroku chief heroku ps:calibration = web 0 heroku ps:calibration = web 1
Your webservice is ready to rock 🙂 You should exist able to detect by going to "http://yourappname.herokuapp.com/"
Now…this elementary NodeJS powered webservice will serve as a uncomplicated Echo server…significant…whatever you blazon volition be returned equally a json response…of course…if you lot type "empty" so the response volition be a empty json…then the principal idea here is that we tin keep the concluding entered value…if you laissez passer a control it volition be called once more when you don't laissez passer any commands at all…so by calling it one time…nosotros can cal it multiple times without disrupting its value…
Side by side in line…will be to create our Unity app…
Create a new app and phone call it "WebService" or something like that…project name doesn't matter as well much…
If the Hierarchy window select "Main Camera" and alter the "Tranform" details similar this…
Now, create a new "3D Object" -> "Cube" and proper noun it "Platform" with the post-obit "Transform" details…
After that, we might need to create four wall that will go around the platform…so create four "3D Object" -> "Cube" and proper name them "Wall 1", "Wall ii", "Wall 3" and "Wall 4″…
When everything is ready, your workspace should look similar this…
Go to the project tab and create a new folder chosen "plugins" and and so create a new C# file chosen "SimpleJSON"…inside copy the source code from here …this will allow us to use SimpleJSON to parse the JSON…
Now…create some other folder called "Script" and inside create a new C# file called "MetaCoding"…or whatever yous like…
MetaCoding.cs
using UnityEngine; using System.Collections; using System.Internet; using Organisation.IO; using SimpleJSON; public grade MetaCoding : MonoBehaviour { int counter = 1; IEnumerator DownloadWebService() { while (true) { Www w = new Www("http://yourapp.herokuapp.com/?command"); yield return w; print("Waiting for webservice\due north"); yield return new WaitForSeconds(1f); print("Received webservice\n"); ExtractCommand(due west.text); print("Extracted data"); WWW y = new WWW("http://yourapp.herokuapp.com/?command=empty"); yield return y; print("Cleaned webservice"); yield return new WaitForSeconds(5); } } void ExtractCommand(string json) { var jsonstring = JSON.Parse(json); cord command = jsonstring["command"]; impress(command); if (command == nothing) { return; } string[] commands_array = command.Split(" "[0]); if(commands_array.Length < iii) { return; } if (commands_array[0] == "create") { CreateObject(commands_array[ane], commands_array[2]); } } void CreateObject(string color, string shape) { string proper name = "NewObject_" + counter; counter += ane; GameObject NewObject = new GameObject(proper noun); switch (shape) { case "cube": NewObject = GameObject.CreatePrimitive(PrimitiveType.Cube); intermission; case "sphere": NewObject = GameObject.CreatePrimitive(PrimitiveType.Sphere); break; case "cylinder": NewObject = GameObject.CreatePrimitive(PrimitiveType.Cylinder); pause; case "capsule": NewObject = GameObject.CreatePrimitive(PrimitiveType.Capsule); break; } NewObject.transform.position = new Vector3(0, 5, 0); NewObject.AddComponent<Rigidbody>(); switch (color) { case "cherry-red": NewObject.GetComponent<Renderer>().cloth.color = Colour.cherry-red; interruption; case "yellow": NewObject.GetComponent<Renderer>().cloth.color = Color.xanthous; break; case "green": NewObject.GetComponent<Renderer>().textile.color = Colour.green; break; case "blue": NewObject.GetComponent<Renderer>().material.colour = Color.blue; break; example "black": NewObject.GetComponent<Renderer>().material.color = Color.black; interruption; case "white": NewObject.GetComponent<Renderer>().material.color = Color.white; break; } } // Use this for initialization void Start () { impress("Started webservice import...\north"); StartCoroutine(DownloadWebService()); } // Update is called once per frame void Update () { } }
Once you have the lawmaking…but attach the script to the Main Photographic camera…
The basic concept for this script is pretty simple…We're creating "DownloadWebService" as an IEnumerator method and then we can call it as a Coroutine…and that allow u.s. to have a slumber equally we want to give some time between calls…
This method will fetch our Heroku WebService looking for a "create" command…once it has it…it will parse the JSON response and divide in 3…so nosotros tin can have…"create", "blue" and "sphere"…this will phone call CreateObject which will then create a blue sphere…after nosotros have done that…the coroutine will go along equally simply send a new control to our WebService to make clean the output…to make this work nicely…we want to requite 5 seconds after we clean the webservice before trying to encounter if there'due south another "create" call…
And this call be will be washed by our Alexa skill…and so basically when proverb "create blue sphere" on Alexa…she volition be send the command to the WebService…update the bulletin and our Unity app will take hold of it…do its piece of work…and clean upwardly the Webservice…the expect for Alexa to provide the side by side command…
So…to kind of wrap up…we need to create our Alexa skill…
First, we're going to create a Lambda function…and then log inhere…
Of form…I take everything already setup…so I'm going to create a dummy role just to show the steps…
Click on "Create Lambda Function" and you volition exist presented with this…
There's a bunch of course…so type in "Color" in the filter box…
Choose "alexa-skills-kit-color-expert"
Go out this as it is and printing "Next"
Choose a proper noun and a clarification…
Choose an existing role if you have it already…otherwise merely create a lambda_basic_execution…then raise upwardly Timeout to 10 seconds and leave everything else as it is…printing "Next"…a confirmation window will announced…and then just press "Create function"…
Yous will be presented with a screen where you can upload your source lawmaking (which volition be doing later on) and an ARN number…which we need for the next stride…
The following function deals with create the Alexa skill…then delight follow forth…and log in here …
Choose "Alexa Skills Kit"…and create a new skill…
Choose a name for you skill and the about important…cull an "Invocation Name"…which is what you're going to use tell Alexa to open you lot awarding…something like…"Alexa, open Sandbox"…click next…
On the Interaction Model tab we have two windows…fill up this on "Intent Schema"…
Intent Schema
{ "intents": [ { "intent": "GetUnityIntent", "slots": [ { "name": "color", "blazon": "LITERAL" }, { "name": "shape", "type": "LITERAL" } ] }, { "intent": "HelpIntent", "slots": [] } ] }
This are basically the parameters that we tin use when asking Alexa to do something…
And make full this on "Sample Utterances"…
Sample Utterances
GetUnityIntent create {red|color} {sphere|shape} GetUnityIntent create {yellow|color} {sphere|shape} GetUnityIntent create {green|color} {sphere|shape} GetUnityIntent create {blueish|color} {sphere|shape} GetUnityIntent create {black|color} {sphere|shape} GetUnityIntent create {white|color} {sphere|shape} GetUnityIntent create {red|color} {cube|shape} GetUnityIntent create {yellow|colour} {cube|shape} GetUnityIntent create {green|color} {cube|shape} GetUnityIntent create {bluish|colour} {cube|shape} GetUnityIntent create {black|color} {cube|shape} GetUnityIntent create {white|color} {cube|shape} GetUnityIntent create {red|colour} {cylinder|shape} GetUnityIntent create {xanthous|color} {cylinder|shape} GetUnityIntent create {green|color} {cylinder|shape} GetUnityIntent create {blue|colour} {cylinder|shape} GetUnityIntent create {black|color} {cylinder|shape} GetUnityIntent create {white|color} {cylinder|shape} GetUnityIntent create {carmine|color} {capsule|shape} GetUnityIntent create {yellowish|color} {capsule|shape} GetUnityIntent create {green|color} {sheathing|shape} GetUnityIntent create {bluish|color} {capsule|shape} GetUnityIntent create {black|color} {capsule|shape} GetUnityIntent create {white|colour} {capsule|shape} GetUnityIntent {thank you|color}
This are all the commands that Alexa can understand…and yes…nosotros could have used "Custom Slot Types" to brand the code shorter…but…I have had the bug of not working pretty well with more than one slot…simply hit next…
Here, choose AWS Lambda ARN…and choice either North America or Europe depending on your physical location…the on the text box…simply copy and paste the ARN that you received from your Lambda role…
This volition ship you to the "Test" tab…but we don't want to and actually we can't use that yet…and so get back to the "Skill Data" tab and you lot will find that a new field has appeared…
And that should be "Application Id"…copy this number and let's motion on to the last step…
Create a folder called "Unity" and within a folder called "src"…inside that folder copy this file "AlexaSkills.js"
We're going to use the "request" module of NodeJS…so install information technology locally on the Unity binder like this…
sudo npm install --prefix=~/Unity/src request
This will create a node_module folder with the asking module on it…
And so, create a new file chosen "index.js"
index.js
var request = require("asking") , AlexaSkill = crave('./AlexaSkill') , APP_ID = 'yourappid'; var fault = function (err, response, body) { console.log('ERROR [%south]', err); }; var getJsonFromUnity = function(color, shape, callback){ var control = "create " + colour + " " + shape; if(color == "thanks"){ callback("give thanks you"); } else{ var options = { method: 'Go', url: 'http://yourapp.herokuapp.com/', qs: { control: command }, headers: { 'postman-token': '230914f7-c478-4f13-32fd-e6593d8db4d1', 'cache-command': 'no-cache' } }; var error_log = ""; asking(options, function (error, response, torso) { if (!fault) { error_log = color + " " + shape; }else{ error_log = "There was a fault"; } callback(error_log); }); } } var handleUnityRequest = office(intent, session, response){ getJsonFromUnity(intent.slots.color.value,intent.slots.shape.value, office(data){ if(information != "thanks"){ var text = 'The ' + data + ' has been created'; var reprompt = 'Which shape would you like?'; response.inquire(text, reprompt); }else{ response.tell("You're welcome"); } }); }; var Unity = role(){ AlexaSkill.call(this, APP_ID); }; Unity.prototype = Object.create(AlexaSkill.image); Unity.prototype.constructor = Unity; Unity.prototype.eventHandlers.onSessionStarted = function(sessionStartedRequest, session){ console.log("onSessionStarted requestId: " + sessionStartedRequest.requestId + ", sessionId: " + session.sessionId); }; Unity.image.eventHandlers.onLaunch = function(launchRequest, session, response){ // This is when they launch the skill but don't specify what they want. var output = 'Welcome to Unity. Create whatsoever color shape past proverb create and providing a color and a shape'; var reprompt = 'Which shape would you like?'; response.ask(output, reprompt); panel.log("onLaunch requestId: " + launchRequest.requestId + ", sessionId: " + session.sessionId); }; Unity.prototype.intentHandlers = { GetUnityIntent: office(intent, session, response){ handleUnityRequest(intent, session, response); }, HelpIntent: office(intent, session, response){ var speechOutput = 'Create a new colored shape. Which shape would you lot like?'; response.inquire(speechOutput); } }; exports.handler = function(event, context) { var skill = new Unity(); skill.execute(upshot, context); };
This code is very simple…because it mostly a template…yous simply copy it…change a couple of things and you're ready to get…
Basically when you say "Alexa, open Unity"…she will listen for your requests…so you tin say "create light-green cube"…and so volition call our Heroku WebService and the expect for another control…if you lot doesn't speak to her again…she will prompt you to say something…if you say "Thanks" she will politely deactivate herself…
And that's pretty much information technology…once Alexa send the command to the WebServer…our Unity App will read and act accordingly…creating whatever shape and color you lot requested…nice, huh?
But of form…yous don't believe, don't you? It tin't be that simple…well…yes and no…information technology's simple…but I took all the hurting point and provide you with the nice and clean ready of instructions…
So…here'south how it looks like when you run the Unity app…
And hither the activity video…
Hope you like information technology…and stay tuned…because for me this was only a proof of concept…the real thing volition become my next full time project…
– Your d-shop team
Source: https://blogs.sap.com/2016/12/01/unity3d-and-alexa-working-together/
0 Response to "What Can I Do With Alexa at Work"
Post a Comment