jeopardy example

Discussion in 'Amazon Echo Development' started by Chad Doebelin, Jun 25, 2015.

  1. Chad Doebelin

    Chad Doebelin New Member

    Messages:
    17
    Likes Received:
    3
    Trophy Points:
    3
    Gender:
    Male
    Made this quick jeopardy app

    This works well enough, it needs some polishing and fixing up, but the wife and kids are using it between commercial breaks on TV.



    Code for the Intent Schema
    Code:
    {
      "intents": [
        {
          "intent": "WhatIsIntent",
          "slots": [
            {
              "name": "Ans",
              "type": "LITERAL"
            }
          ]
        }
      ]
    }
    
    Code for the Sample Utterances
    Code:
    WhatIsIntent what is {metalurgy|Ans}
    WhatIsIntent what is {hamburger Hill|Ans}
    WhatIsIntent who is {james belushi|Ans}
    WhatIsIntent who are the {teenange mutant ninja turtles|Ans}
    WhatIsIntent what are the {new kids on the block|Ans}
    
    Code for the endpoint
    Code:
    <?php
    
    function speechOut($words) {
            header('Content-type: application/json');
    
            $open = '{"version": "1.0","sessionAttributes":"","response":{"outputSpeech":{"type": "PlainText","text": "';
            $close = '"},"shouldEndSession": false}}';
            $end = '"},"shouldEndSession": false}}';
    
            echo $open;
             echo ($words);
            echo $close;
    }
    
    function endSession($words) {
            header('Content-type: application/json');
    
            $open = '{"version": "1.0","sessionAttributes":"","response":{"outputSpeech":{"type": "PlainText","text": "';
            $close = '"},"shouldEndSession": true}}';
    
            echo $open;
             echo ($words);
            echo $close;
    }
    
    
    //$value = $data[value];
    
    $header = json_decode(file_get_contents('php://input'), TRUE);
    $requestName = $header['request'][intent][name];
    $requestType = $header['request'][type];
    $text = print_r($header,true);
    file_put_contents('from.txt',$text);
    if($requestType == 'LaunchRequest')
    {
            // new session!
    $data = json_decode(file_get_contents('http://jservice.io/api/random'), TRUE);
    $text = print_r($data,true);
    
    $ver = $data[0][question];
    $ans = $data[0][answer];
    file_put_contents('lastanswer.txt',$ans);
    speechOut($ver);
    }
    if($requestType == 'SessionEndedRequest')
    {
    $ans = file_get_contents('lastanswer.txt');
    speechout($ans);
    }
    if($requestType == 'IntentRequest')
    {
    $ans = file_get_contents('lastanswer.txt');
    if ($ans == $header['request'][intent][slots][Ans][value])
    {
    //speechOut('That was correct');
    endSession('That was correct');
    }
    else{
      $spch = "I'm sorry, but ";
      $spch .= $header['request'][intent][slots][Ans][value];
      $spch .= " was incorrect. The correct answer was :";
      $spch .= $ans;
    //  speechOut($header['request'][intent][slots][Ans][value]);
    endSession($spch);
    //speechOut($spch);
     }
    }
    speechOut(' ');
    ?>
    
     
    Avinash likes this.
  2. Avinash

    Avinash New Member

    Messages:
    19
    Likes Received:
    1
    Trophy Points:
    3
    Hi Chad, This is really an awesome example for me. I used this example at my endpoint, works well till launch request but after that it goes in SessionEndedRequest. The reason might be same here as I use you intents & utterances.
    It speaks question from your API but after that session directly ends with no ans.
    I tried replaced below line of code:
    Code:
    speechout("Your answer is: ".$ans);
    from SessionEndedRequest section of your code.
     
  3. Avinash

    Avinash New Member

    Messages:
    19
    Likes Received:
    1
    Trophy Points:
    3
    Hi Chad,
    I think you application is working perfectly as per the code. Nothing wrong! Plz ignore my comments.
     

Share This Page