Amazon Echo Hello World with PHP endpoint

Discussion in 'Amazon Echo Development' started by Jeff C, Apr 21, 2015.

  1. Chad Doebelin

    Chad Doebelin New Member

    Messages:
    17
    Likes Received:
    3
    Trophy Points:
    3
    Gender:
    Male
    Avinash you update the intents and utterances on the edit app menu on Amazon Developer Console -> Apps & Services -> Alexa -> edit button for the app -> Interaction Model
    See Attachment #1

    you have to switch back and forth between the lambda console.

    For those of you that are having problems with the Alexa Colors example, You guys need to edit the lambda function. and make sure you find and replace all instances of Color with City in avinash's case. you were probably missing the one in line 151
    Code:
    var favoriteColorSlot = intent.slots.Color;
    
     

    Attached Files:

    Last edited: Jun 23, 2015
  2. Chad Doebelin

    Chad Doebelin New Member

    Messages:
    17
    Likes Received:
    3
    Trophy Points:
    3
    Gender:
    Male
    Also, I got sick and tired of this node.js on lambda bullshit and bit the bullet and set up a webserver and got the SSL working.
    I went ahead and got a signed certificate and domain name from namecheap, because I'm a student I got the stuff free with the github educational pack.

    I was not able to communicate properly with alexa until my site finally passed from symantec's SSL checker.
    https://ssltools.websecurity.symantec.com/checker/views/certCheck.jsp

    I had to edit my
    /etc/httpd/conf.d/flex-443.conf directives

    Code:
    <VirtualHost *:443>
            ServerName doebelin.me
            ServerAlias [URL='http://www.doebelin.me']www.doebelin.me[/URL]
            DocumentRoot /var/www/html
            ScriptAlias /cgi-bin/ /var/flexshare/shares/doebelin.me/cgi-bin/
            ErrorLog /var/log/httpd/error_log
            CustomLog /var/log/httpd/access_log combined
            SSLEngine on
            SSLCertificateKeyFile /etc/pki/tls/private/my-self-signed.key
            SSLCertificateFile /etc/pki/tls/certs/my-CA-issued-certificate.crt
            SSLCertificateChainFile /etc/pki/tls/certs/bundle-containing-root-and-issuing authority.crt
    
            # No weak export crypto allowed
            SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL
    #SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:!EXP:+eNULL
            SSLProtocol all -SSLv2 -SSLv3
            SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown downgrade-1.0 force-response-1.0
    </VirtualHost>
    
    in your case, with a self signed certificate you probably only need to have the SSLCertificateKeyFile pointing to the key you generated and the SSLCertificateFile pointing to the certificate you signed.

    then restart httpd
     
    Last edited: Jun 23, 2015
  3. iWombat

    iWombat New Member

    Messages:
    6
    Likes Received:
    2
    Trophy Points:
    3
    Gender:
    Male
    Ah, that was my problem. I forgot the other half was edited via the Alexa Appls page.
     
    Chad Doebelin likes this.
  4. Avinash

    Avinash New Member

    Messages:
    19
    Likes Received:
    1
    Trophy Points:
    3
    Hey Chad, I am updating intents and utterances in Interaction Model only but getting issues as below:

    MyColorIsIntent my color is {dark brown|Temp}
    MyColorIsIntent my color is {green|Temp}
    MyColorIsIntent my favorite color is {red|Temp}
    MyColorIsIntent my favorite color is {navy blue|Temp}
    WhatsMyColorIntent whats my color
    WhatsMyColorIntent what is my color
    WhatsMyColorIntent say my color

    When I give input to my echo with these utterances slot values(dark brown, green, red or navy blue) then echo gives me proper response but when I give input like 'yellow', 'light orange' or any other words which are not present in utterances slots values then it doesn't give me response.

    Also I want to fetch the location of my echo device. Anyone knows about this? I know I had previously asked this question but still if anyone knows???

    Here is the code I am using:
    Code:
    <?php
    $fp = fopen("response.csv","a");
    $data = file_get_contents("php://input");
    $jsonData = json_decode($data);
    
    header('Content-Type: application/json;charset=UTF-8');
    
    $response = "";
    
    if($jsonData->request->type === "LaunchRequest")
    {
        fwrite($fp, "This is LaunchRequest\n");
      
        $response = '{
            "version" : "1.0",
            "response" : {
                "outputSpeech" : {
                "type" : "PlainText",
                "text" : "Say something!"
                },
                "shouldEndSession" : false
               }
            }';
        fwrite($fp, $data."\tresponse: $response\n");
    } else if($jsonData->request->type === "IntentRequest") {
        fwrite($fp, "IntentRequest\n");
        //$yourName = $jsonData->request->intent->slots->Name->value;
        $userNameIntentName = $jsonData->request->intent->name;
        $temp = '';
        if($userNameIntentName === "MyColorIsIntent")
        {
            $temp= $jsonData->request->intent->slots->Temp->value;
            $response = "{
                \"version\" : \"1.0\",
                \"response\" : {
                    \"outputSpeech\" : {
                    \"type\" : \"PlainText\",
                    \"text\" : \"You called MyColorIsIntent and you said $temp\"
                    },
                    \"shouldEndSession\" : false
                   }
                }";
            fwrite($fp, "Intent name: $userNameIntentName\t".$data."\tresponse: $response\n");
        }
        if($userNameIntentName === "WhatsMyColorIntent")
        {
            $response = "{
                \"version\" : \"1.0\",
                \"response\" : {
                    \"outputSpeech\" : {
                    \"type\" : \"PlainText\",
                    \"text\" : \"You called WhatsMyColorIntent\"
                    },
                    \"shouldEndSession\" : false
                   }
                }";
            fwrite($fp, "Intent name: $userNameIntentName\t".$data."\tresponse: $response\n");
        }
    }
    
    header('Content-Length: ' . strlen($response));
    echo $response;
    ?>
     
  5. Chad Doebelin

    Chad Doebelin New Member

    Messages:
    17
    Likes Received:
    3
    Trophy Points:
    3
    Gender:
    Male
    Avinash, I ran your code and set my endpoint to it. It recognizes when I say as input "My color is yellow" and "My color is light orange" "My color is fuscia" and "My color is poop"
    that works fine, are you forgetting to say the intent "my color is" before saying "yellow" ?

    I am getting an error response if it timeouts and doesnt respond to the session end request. I havent figured out how that works yet.

    Also, I just don't know what to do to find the geo location, I can't think of any way to do this. I watched my apache logs when the echo makes a request and the IP address for the endpoint originates in maryland, no where near me.
     
  6. Avinash

    Avinash New Member

    Messages:
    19
    Likes Received:
    1
    Trophy Points:
    3
    Thanks Buddy, This worked at my place too but only in my hello world app but not in my actual application where I am changing names of Intents and Slots.
    Here is my code + Intents + Utterances.

    Code:
    {
    "intents": [
        {
          "intent": "MyCityIntent",
          "slots": [
            {
              "name": "Temp",
              "type": "LITERAL"
            }
          ]
        },
        {
          "intent": "SearchArtist",
          "slots": [
        {
              "name": "Artist",
              "type": "LITERAL"
            }
          ]
        }
      ]
    }
    
    MyCityIntent  my city is {London|Temp}
    MyCityIntent  my city is {New York|Temp}
    
    SearchArtist is {lady gaga|Artist} coming to town
    SearchArtist is {akon|Artist} coming to town
    SearchArtist my artist is {lady gaga|Artist}
    SearchArtist my artist is {akon|Artist}
    Code:
    <?php
    $fp = fopen("response.csv","a");
    $data = file_get_contents("php://input");
    $jsonData = json_decode($data);
    if($jsonData->session->new === "true"){
        //echo "New Session Started Here.";
    }
    
    $APP_ID = "appId";
    $REQUESTED_APP_ID = $jsonData->session->application->applicationId;
    
    //App Validation
    if($APP_ID !== $REQUESTED_APP_ID)
    {
        die("Invalid Request!");
    }
    
    header('Content-Type: application/json;charset=UTF-8');
    
    $response = "";
    if($jsonData->request->type === "LaunchRequest")
    {
        fwrite($fp, "This is LaunchRequest\n");
        $response = '{
            "version" : "1.0",
            "response" : {
                "outputSpeech" : {
                "type" : "PlainText",
                "text" : "WelCome, Say Something!"
                },
                "shouldEndSession" : false
               }
            }';
    } else if ($jsonData->request->type === "IntentRequest") {
        $userCityIntentName = $jsonData->request->intent->name;
        fwrite($fp, "This is IntentRequest $userCityIntentName\n");
      
        $cityName = '';
        $artistName = '';
        if($userCityIntentName == "MyCityIntent")
        {
            $cityName = $jsonData->request->intent->slots->Temp->value;      
            $response = "{
                \"version\" : \"1.0\",
                \"response\" : {
                    \"outputSpeech\" : {
                    \"type\" : \"PlainText\",
                    \"text\" : \"MyCityIntent, Your city is: $cityName.\"
                    },
                    \"shouldEndSession\" : false
                    }
                }";
            fwrite($fp, "data: $data\tIntent Name: $userCityIntentName\tUser City: $cityName\tResponse: $response\n");
        }
        if($userCityIntentName == "SearchArtist"){
            $artistName = $jsonData->request->intent->slots->Artist->value;
            $response = "{
                \"version\" : \"1.0\",
                \"response\" : {
                        \"outputSpeech\" : {
                        \"type\" : \"PlainText\",
                        \"text\" : \"SearchArtist, Your artist: $artistName.\"
                        },
                    \"shouldEndSession\" : false
                    }
                }";
            fwrite($fp, "data: $data\tIntent Name: $userCityIntentName\tUser Artist: $artistName\tResponse: $response\n");
        }
    }
    
    header('Content-Length: ' . strlen($response));
    echo $response;
    ?>
    Just Changed Intent and Utterances names, resulting in echo not recognizing inputs.
     
    Last edited: Jun 25, 2015

Share This Page