{"id":551,"date":"2018-05-17T13:45:05","date_gmt":"2018-05-17T11:45:05","guid":{"rendered":"https:\/\/www.welaunch.io\/en\/?p=551"},"modified":"2020-06-20T12:28:43","modified_gmt":"2020-06-20T10:28:43","slug":"create-an-alexa-skill-with-php-tutorial","status":"publish","type":"post","link":"https:\/\/www.welaunch.io\/en\/2018\/05\/create-an-alexa-skill-with-php-tutorial\/","title":{"rendered":"Create an Alexa Skill with PHP &#8211; Tutorial"},"content":{"rendered":"<p><strong>Since my last tutorial some things have changed at Amazon&#8217;s Alexa Developer Platform. The interface has been reworked and instead of the old system, where intents had to be created manually, there is now a skill builder.<\/strong><\/p>\n<p>Therefore I would like to give you here a small update of my old Alexa PHP Tutorials.<strong>The Skill Overview<\/strong><\/p>\n<p>We&#8217;ll be leaving soon, too. I simply assume that you have an Alexa Developer account and therefore jump straight into the new dashboard. Via the menu item &#8220;Your Alexa Consoles&#8221; (top right) &gt; Skills you get to your Skill Overview.<\/p>\n<p><a href=\"https:\/\/developer.amazon.com\/alexa\/console\/ask\" target=\"_blank\" rel=\"noopener noreferrer\">To your Alexa Skill Overview<\/a><strong>Create a Skill<\/strong><\/p>\n<p>Under &#8220;Create Skill&#8221; we can create our first own Amazon Alexa skill. After you have clicked the button you have to assign a skill name in the first step. In my example, Daniel.<\/p>\n<p>Then we have to select the model for the skill. We want to start small and not directly build a Smart Home or Video Skill. That&#8217;s why we choose Custom here.<\/p>\n<h2>Skill Settings<\/h2>\n<h3>Skill Dashboard<\/h3>\n<p>That&#8217;s it: Now you get into the dashboard of your skill. In the upper bar you will find the following points:<\/p>\n<ul>\n<li>Daniel =&gt; name of your skill<\/li>\n<li>Build =&gt; Here we create the commands<\/li>\n<li>Test =&gt; So we can check the functions later<\/li>\n<li>Launch =&gt; If you want to publish your skill in the Amazon Store<\/li>\n<li>Measure =&gt; usage behavior etc.<\/li>\n<\/ul>\n<p>Under Build in the left sidebar is the most important one &#8211; the Skill calls and settings.<\/p>\n<h3>Invocation Name<\/h3>\n<p>In the right bar you will find a good checklist with things to do. Here we also start and click on &#8220;1st Invocation Name&#8221;. The invocation name is the &#8220;start expression&#8221; for Alexa to address your skill e.g. via &#8220;Alexa, ask INVOCATION NAME&#8221;. I use daniel as an example &#8211; important: no capitalization is allowed here.<\/p>\n<h3>Creating an Intent<\/h3>\n<p>After setting the Invocation name we can create our first skill intent. An intent is a command that runs on your server when one of the associated uterances is detected. It doesn&#8217;t sound complicated.Here is an example:<\/p>\n<ul>\n<li>Intent = hello<\/li>\n<li>Utterances\n<ul>\n<li>hi<\/li>\n<li>hello<\/li>\n<li>moin<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>If you now ask your skill &#8220;hi&#8221;, the intent &#8220;hello&#8221; will be executed on your server. What then comes back from this intent depends on your programming.<\/p>\n<h3>Skill Utterances &amp; Slots<\/h3>\n<p>Ok so we have now created an intent with &#8220;hello&#8221;. Now we create 3 utterances as described above: hello, hi and moin.<\/p>\n<p>I would also like a slot (or variable) to be transferred to my server. I add a slot &#8220;name&#8221; with the slot type &#8220;AMAZON.DE_FIRST_NAME&#8221;. Of course I have to add these to the Utterances with the help of a brace.<\/p>\n<p>Now our first intent is ready.<\/p>\n<h3>Set an Endpoint<\/h3>\n<p>Now we can set an endpoint. The endpoint is the destination where your skill requests should be sent. In my case this is https:\/\/alexa.welaunch.io. It is important that the endpoint has an encrypted connection (https).<\/p>\n<p><strong>Select HTTPs, enter the address and select &#8220;My development endpoint has a certificate from a trusted certificate authority&#8221;. Then back to the interaction model and click on &#8220;Build Click&#8221;.<\/strong><\/p>\n<h2>The PHP-Part<\/h2>\n<p>Now we can finally get to the PHP topic. Actually, not much changes here in comparison to my old tutorial, but I will explain it again briefly. As a logger I use monologue to check why some questions were not answered. You can also see which intents or utterances are asked, but not supported by your skill. For example, when I say &#8220;hiderido&#8221; &#8211; this uterance is missing.<\/p>\n<p>Composer File for Monolog:<\/p>\n<pre>{\r\n    \"name\": \"db-dzine\/alexaphp\",\r\n    \"description\": \"PHP Class for Alexa\",\r\n    \"type\": \"library\",\r\n    \"authors\": ,\r\n    \"minimum-stability\": \"dev\",\r\n    \"require\": {\r\n        \"monolog\/monolog\": \"1.x-dev\",\r\n    }\r\n}<\/pre>\n<p>After that run\u00a0<code>composer install<\/code> in your folder. Then you can create an index.php and load autload &amp; monolog:<\/p>\n<pre>&lt;?php \/\/ Load Monolog Vendor require_once __DIR__ . '\/vendor\/autoload.php'; \/\/ Autoload files using Composer autoload use Monolog\\Logger; use Monolog\\Handler\\StreamHandler; \/\/ Save Requests on debug log define('DEBUG', true); define('DEBUG_FILE', __DIR__ . '\/logs\/log.log'); $logger = new Logger('Alexa'); $logger-&gt;pushHandler( new StreamHandler( DEBUG_FILE, Logger::INFO ) );<\/pre>\n<p>Um requests von eurem Skill zu catchen:<\/p>\n<pre>\/\/ Get Alexa Request\r\n$jsonRequest = file_get_contents('php:\/\/input');\r\n\r\n\/\/ Decode the Request\r\n$data        = json_decode($jsonRequest, true);\r\n\r\n\/\/ Abort when Empty\r\nif( empty($data) || (!isset($data) ) ) {\r\n\tdie('Bad Request');\r\n}<\/pre>\n<p>The following variables can be read from the data array. There&#8217;s more in here, of course, but these are the most important:<\/p>\n<pre>$intent = !empty($data) ? $data : 'default';\r\n$intentData = !empty($data) ? $data : 'default';\r\n$sessionId =  !empty($data) ? $data : 'default'; \r\n\r\n$logger-&gt;info( var_export($data, true));\r\n$logger-&gt;info( $intentData );\r\n$logger-&gt;info( $sessionId);<\/pre>\n<p>Now we can use the $intent variable to find out which intent was addressed. I return a nice hello via a switch statement in case of the &#8220;hello&#8221;-intent. And if a name was passed with it, I will also return it. Return the whole as JSON encoded string to Alexa.<\/p>\n<pre>switch ($intent) {\r\n\r\n\t\/\/ Hallo Intent f\u00fcr Utterances hi, hallo, moin\r\n\tcase 'hallo':\r\n\t\t\/\/ Hallo Intentdata abfragen\r\n\t\t$name = !empty($intentData) ? $intentData : '';\r\n\r\n\t\t\/\/ save $name in data file - here also mysql can be used\r\n\t\t\/\/ later you can use session id to get data like name\r\n\t\tif(!empty($name)) {\r\n\t\t\t$dataToSave = array($sessionId =&gt; array(\r\n\t\t\t\t'name' =&gt; $name\r\n\t\t\t));\r\n\t\t\t$fp = fopen('data.json', 'w');\r\n\t\t\tfwrite($fp, json_encode($dataToSave));\r\n\t\t\tfclose($fp);\r\n\t\t}\r\n\r\n\t\t$responseArray = ,\r\n\t\t          'shouldEndSession' =&gt; false\r\n\t\t    ]\r\n\t\t];\r\n\t\tbreak;\r\n\r\n\t\/\/ Default\r\n\tdefault:\r\n\t\t$responseArray = ,\r\n\t\t          'shouldEndSession' =&gt; true\r\n\t\t    ]\r\n\t\t];\r\n\t\tbreak;\r\n}\r\n\r\nheader ( 'Content-Type: application\/json' );\r\necho json_encode ( $responseArray );\r\ndie();<\/pre>\n<h2>Skill testing<\/h2>\n<p>We can test our skill with a &#8220;Question daniel hello klaus&#8221;. Sounds stupid, but ask daniel to start the skill (invocation name) and hello (intent) is executed.<\/p>\n<p>You can also use your Alexa app on your smartphone to test the skill.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since my last tutorial some things have changed at Amazon&#8217;s Alexa Developer Platform. The interface has been reworked and instead of the old system, where intents had to be created manually, there is now a skill builder. Therefore I would like to give you here a small update of my old Alexa PHP Tutorials.The Skill&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[18,1,5],"tags":[],"class_list":["post-551","post","type-post","status-publish","format-standard","hentry","category-alexa","category-general","category-php"],"_links":{"self":[{"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/posts\/551","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/comments?post=551"}],"version-history":[{"count":7,"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/posts\/551\/revisions"}],"predecessor-version":[{"id":4339,"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/posts\/551\/revisions\/4339"}],"wp:attachment":[{"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/media?parent=551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/categories?post=551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.welaunch.io\/en\/wp-json\/wp\/v2\/tags?post=551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}