Amazon Alexa PHP Prompt and Response Example (Hello World Part 2)

created April 15, 2017, last updated May 14, 2017.

.

In part one of the Alexa PHP Hello World example 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.

If you are new to Alex skills and PHP read part one of this Hello World example first.

To demonstrate prompt and responses we will ask Alexa for a “clever quote”, Alexa will give us the quote and ask if we want another. If the user replies yes, Alexa reads another quote and prompts again until all the available quotes have been used.

Clever Quotes

Alexa Prompt and Response

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.

First let’s modify the Hello World skill interaction model :

 

{
  "intents": [
    {
      "slots": [
        {
          "name": "command",
          "type": "COMMANDOBJECTS"
        },
        {
          "name": "prompt",
          "type": "PROMPTOBJECTS"
        }
      ],
      "intent": "HelloWorld"
    },
    {
      "intent": "HelpIntent"
    }
  ]
}

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

HelloWorld for {command}
HelloWorld {prompt}

“Clever” quotes

The clever quotes data is an array of sentences stored in Data::cleverQuotes(). 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.

// clever quotes data
//
$_cleverQuotesData=Data::cleverQuotes();
$_cleverQuotesArrayKeys= range(0, (count($_cleverQuotesData)-1));
shuffle($_cleverQuotesArrayKeys);

 

  • alexa ask hello world for clever quotes

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 “clever quote” fetched from the array using the first of our random array key numbers and the prompt to the user – “would you like some more clever quotes“. We also set endsession to false to keep the session open and save the random key data and prompt count data we need for the next session.

 

// response
//
return array(
	'intent' => array(
		'response' => 'Ok, here comes your first clever quote : '. $_cleverQuotesData[$_cleverQuotesArrayKeys[0]]. ' Would you like some more clever quotes?',
		'card' => array(
			'title' => $_target,
			'text' => $_now->format('H:i:s'). ' '. $_cleverQuotesData[$_cleverQuotesArrayKeys[0]],
			'image' => false
		),											
		'target' => $_target,
		'status' => false,
		'sessionattributes' => array('object' => 'clever quotes','target' => $_target,'prompt' => true, 'count' => 1,'data' => $_cleverQuotesArrayKeys),
		'endsession' => false
	)
);

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 yes or no response from the user.

If the user responded with yes, we extract the data we need from the session to provide the next “clever quote” and then provide the exact same response and session data including an incremented prompt count variable.

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 endsession to true.

Alexa Session Data

If you check the logs generated by the php script you will see the session data:

 

    [session] > Array
        (
            [new] > 
            [sessionId] > SessionId.1-2-3
            [application] > Array
                (
                    [applicationId] > amzn1.ask.skill.1-2-3
                )

            [attributes] > Array
                (
                    [data] > Array
                        (
                            [0] > 0
                            [1] > 1
                            [2] > 5
                            [3] > 3
                            [4] > 7
                            [5] > 4
                            [6] > 10
                            [7] > 6
                            [8] > 2
                            [9] > 8
                            [10] > 9
                        )

                    [count] > 1
                    [prompt] > 1
                    [object] > clever quotes
                    [target] > clever quotes
                )

            [user] > Array
                (
                    [userId] > amzn1.ask.account.123
                )

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.

The example “clever quotes” intent show here is published in the Alexa skill store as “Clever Stuff”.

The updated code for this Hello World and Clever Quotes example is available from github here.

Comments

  1. Boris says:

    Thank you for you great work so far. Are there any plans to support the new Dialog model from amazon in the near future?
    https://developer.amazon.com/public/solutions/alexa/alexa-skills-kit/docs/dialog-interface-reference
    This could make the code part much leaner, when it comes to more complex implementations. I would highly appreciate to see this officially implemented in your library, so that I don’t have to start to “frickle around” with this 😉 Any feedback welcome.
    Beside that: Cool stuff, thank you for publishing!

    • PAJ says:

      The new skill builder tool looks good. The new dialog model is really just delegating some dialog tasks to the skill to ensure that all the required slots for the intent get filled before the intent request is sent. I will certainly start using the beta skill but I don’t think my server side code will change that much as we still need to parse the intent response and slot values to process the intent.

      • Johann says:

        Great work PAJ! i’vegot build a nice SDK.
        I agree completely with Boris.
        Could you take a look at the multiturn dialog at alexa?
        What are your next plans ?

        Greetings