<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>alex Archives - gj</title>
	<atom:link href="https://blog.gaiterjones.com/tag/alex/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.gaiterjones.com/tag/alex/</link>
	<description>gaiterjones</description>
	<lastBuildDate>Tue, 24 Apr 2018 09:13:28 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.4.3</generator>
	<item>
		<title>Amazon Alexa PHP Prompt and Response Example (Hello World Part 2)</title>
		<link>https://blog.gaiterjones.com/amazon-alexa-php-prompt-and-response-example/</link>
					<comments>https://blog.gaiterjones.com/amazon-alexa-php-prompt-and-response-example/#comments</comments>
		
		<dc:creator><![CDATA[PAJ]]></dc:creator>
		<pubDate>Sat, 15 Apr 2017 12:43:10 +0000</pubDate>
				<category><![CDATA[Alexa]]></category>
		<category><![CDATA[Amazon]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[alex]]></category>
		<category><![CDATA[hello world]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[prompt]]></category>
		<category><![CDATA[response]]></category>
		<category><![CDATA[session]]></category>
		<guid isPermaLink="false">http://blog.gaiterjones.com/?p=1562</guid>

					<description><![CDATA[NOTE &#8211; this blog entry was written for the old version of the Alexa Skills Kit Developer Console &#8211; whilst the PHP server code still works some info and screen...<a class="more-link" href="https://blog.gaiterjones.com/amazon-alexa-php-prompt-and-response-example/" title="Continue reading">Continue reading</a>]]></description>
										<content:encoded><![CDATA[<p><strong>NOTE &#8211; this blog entry was written for the old version of the Alexa Skills Kit Developer Console &#8211; whilst the PHP server code still works some info and screen shots of the developer console are now obsolete!</strong></p>
<p>In <a href="https://blog.gaiterjones.com/amazon-alexa-php-hello-world-example/">part one of the Alexa PHP Hello World example</a> I created a very basic skill and a PHP app to show how to handle Amazon Alexa IntentRequest data and provide a response. In part two I will extend the code to allow us to create interactive prompt and responses.</p>
<p><em>If you are new to Alex skills and PHP read <a href="https://blog.gaiterjones.com/amazon-alexa-php-hello-world-example/">part one</a> of this Hello World example first.</em></p>
<p>To demonstrate prompt and responses we will ask Alexa for a &#8220;clever quote&#8221;, Alexa will give us the quote and ask if we want another. If the user replies <em>yes</em>, Alexa reads another quote and prompts again until all the available quotes have been used.</p>
<figure id="attachment_1574" aria-describedby="caption-attachment-1574" style="width: 400px" class="wp-caption aligncenter"><img fetchpriority="high" decoding="async" class="wp-image-1574 size-full" src="https://blog.gaiterjones.com/wp-content/uploads/2017/04/alexa-clever-quotes-1.jpg" alt="" width="400" height="473" /><figcaption id="caption-attachment-1574" class="wp-caption-text">Clever Quotes</figcaption></figure>
<h1>Alexa Prompt and Response</h1>
<p>To allow this prompt and response interaction we simply tell our skill to keep the session open when we require a response, and we store the data we need for the session in the session attributes array.</p>
<p>First let&#8217;s modify the Hello World skill interaction model :</p>
<p>&nbsp;</p>
<pre class="brush: plain; title: ; notranslate">
{
  &quot;intents&quot;: &#x5B;
    {
      &quot;slots&quot;: &#x5B;
        {
          &quot;name&quot;: &quot;command&quot;,
          &quot;type&quot;: &quot;COMMANDOBJECTS&quot;
        },
        {
          &quot;name&quot;: &quot;prompt&quot;,
          &quot;type&quot;: &quot;PROMPTOBJECTS&quot;
        }
      ],
      &quot;intent&quot;: &quot;HelloWorld&quot;
    },
    {
      &quot;intent&quot;: &quot;HelpIntent&quot;
    }
  ]
}
</pre>
<p>Now we have command and prompt slots, with command objects and prompt objects to catch our prompt and response utterances. I have also added two new utterances</p>
<p><em>HelloWorld for {command}</em><br />
<em>HelloWorld {prompt}</em></p>
<h1>&#8220;Clever&#8221; quotes</h1>
<p>The clever quotes data is an array of sentences stored in <em>Data::cleverQuotes(). </em>We load the data, and then create a list of random numbers we will use as array keys to get random quotes from the array. The list of random array keys will be stored in the session data.</p>
<pre class="brush: php; title: ; notranslate">
// clever quotes data
//
$_cleverQuotesData=Data::cleverQuotes();
$_cleverQuotesArrayKeys= range(0, (count($_cleverQuotesData)-1));
shuffle($_cleverQuotesArrayKeys);
</pre>
<p>&nbsp;</p>
<ul>
<li>alexa ask hello world for clever quotes</li>
</ul>
<p>In the HelloWorld php intent class I have added some new code to parse the user request for clever quotes. The response includes the first &#8220;clever quote&#8221; fetched from the array using the first of our random array key numbers and the prompt to the user &#8211; &#8220;<em>would you like some more clever quotes</em>&#8220;. We also set <em>endsession</em> to false to keep the session open and save the random key data and prompt count data we need for the next session.</p>
<p>&nbsp;</p>
<pre class="brush: php; title: ; notranslate">
// response
//
return array(
	'intent' =&gt; array(
		'response' =&gt; 'Ok, here comes your first clever quote : '. $_cleverQuotesData&#x5B;$_cleverQuotesArrayKeys&#x5B;0]]. ' Would you like some more clever quotes?',
		'card' =&gt; array(
			'title' =&gt; $_target,
			'text' =&gt; $_now-&gt;format('H:i:s'). ' '. $_cleverQuotesData&#x5B;$_cleverQuotesArrayKeys&#x5B;0]],
			'image' =&gt; false
		),											
		'target' =&gt; $_target,
		'status' =&gt; false,
		'sessionattributes' =&gt; array('object' =&gt; 'clever quotes','target' =&gt; $_target,'prompt' =&gt; true, 'count' =&gt; 1,'data' =&gt; $_cleverQuotesArrayKeys),
		'endsession' =&gt; false
	)
);
</pre>
<p>With the session still open the user has about 8 seconds to reply and we will see the response in the prompt slot. In the same way we parse the command slot data we parse the response slots for the <em>yes</em> or <em>no</em> response from the user.</p>
<p>If the user responded with yes, we extract the data we need from the session to provide the next &#8220;clever quote&#8221; and then provide the exact same response and session data including an incremented prompt count variable.</p>
<p>If there are no more quotes available we respond appropriately and end the session. Similarly if the user responds with no, we respond and end the session by setting <em>endsession</em> to true.</p>
<h1>Alexa Session Data</h1>
<p>If you check the logs generated by the php script you will see the session data:</p>
<p>&nbsp;</p>
<pre class="brush: plain; title: ; notranslate">
    &#x5B;session] &gt; Array
        (
            &#x5B;new] &gt; 
            &#x5B;sessionId] &gt; SessionId.1-2-3
            &#x5B;application] &gt; Array
                (
                    &#x5B;applicationId] &gt; amzn1.ask.skill.1-2-3
                )

            &#x5B;attributes] &gt; Array
                (
                    &#x5B;data] &gt; Array
                        (
                            &#x5B;0] &gt; 0
                            &#x5B;1] &gt; 1
                            &#x5B;2] &gt; 5
                            &#x5B;3] &gt; 3
                            &#x5B;4] &gt; 7
                            &#x5B;5] &gt; 4
                            &#x5B;6] &gt; 10
                            &#x5B;7] &gt; 6
                            &#x5B;8] &gt; 2
                            &#x5B;9] &gt; 8
                            &#x5B;10] &gt; 9
                        )

                    &#x5B;count] &gt; 1
                    &#x5B;prompt] &gt; 1
                    &#x5B;object] &gt; clever quotes
                    &#x5B;target] &gt; clever quotes
                )

            &#x5B;user] &gt; Array
                (
                    &#x5B;userId] &gt; amzn1.ask.account.123
                )
</pre>
<h1>Custom Help and Launch intent</h1>
<p>Alexa has some default built in intents such as STOP, HELP and LAUNCH.</p>
<p>When you say &#8220;Alex open skillname&#8221; the LAUNCH intent is used. When you say HELP in a skill, the HELP intent is used. When you submit your skill Amazon will check these intents for functionality. To create your own custom LAUNCH and HELP intent responses you must create or edit the example file in AmazonDev/Alexa/Launch and AmazonDev/Alexa/Help.</p>
<p>The filenames should be as follows</p>
<p><em>launch123.php<br />
help123.php</em></p>
<p>Where 123 is replaced with the last digits from your skill id, i.e. if your skill id is <em>amzn1.ask.skill.111-222-333-444-555</em> then the launch and intent filenames should be</p>
<p><em>launch555.php<br />
help555.php</em></p>
<p>You should also change the class name of the launch and help classes to the same as the filename (without .php). Now you can customise the launch and help intent for you skill by editing the responses in the help and launch methods.</p>
<pre class="brush: php; title: ; notranslate">
	public static function help($_alexaRequest=false)
	{
		$_response=&quot;Hello, I am happy to Help! To start this skill please say 'Alexa, open Hello World'&quot;;
		$_card=false;
		$_endSession=false;
		$_sessionAttributes=false;
		$_outputSSML=false;

		return array(
			'response' =&gt; $_response,
			'card' =&gt; $_card,
			'endsession' =&gt; $_endSession,
			'sessionattributes' =&gt; $_sessionAttributes,
			'outputssml' =&gt; $_outputSSML
		);

	}
</pre>
<p>You can see here that it is relatively simple to include prompts and responses in your Alexa skill by controlling and storing data in the skill session.</p>
<p>The example &#8220;clever quotes&#8221; intent show here is published in the Alexa skill store as &#8220;Clever Stuff&#8221;.</p>
<p>The updated code for this Hello World and Clever Quotes example is <a href="https://github.com/gaiterjones/amazon-alexa-php-hello-world-example">available from github here</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://blog.gaiterjones.com/amazon-alexa-php-prompt-and-response-example/feed/</wfw:commentRss>
			<slash:comments>35</slash:comments>
		
		
			</item>
	</channel>
</rss>
