Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.58 KB | None | 0 0
  1. <?php
  2.  
  3.     $jsonRequest = file_get_contents('php://input');
  4.     $data = json_decode($jsonRequest, true);
  5.  
  6.     file_put_contents('alexaInput.json', json_encode($data, JSON_PRETTY_PRINT));
  7.  
  8.     if (empty($data) || (!isset($data))) {
  9.         die('Bad Request');
  10.     }
  11.    
  12.     $intentType = !empty($data['request']['type']);
  13.     $intent = !empty($data['request']['intent']['name']) ? $data['request']['intent']['name'] : 'default';
  14.     $intentData = !empty($data['request']['intent']['slots']) ? $data['request']['intent']['slots'] : 'default';
  15.     $sessionId = !empty($data['session']['sessionId']) ? $data['session']['sessionId'] : 'default';
  16.  
  17.     file_put_contents('alexaInput.json', 'TIPOLOGIA INTENT : ' . $intentType, FILE_APPEND);
  18.  
  19.     if ($intentType == 'LaunchRequest'){
  20.         $responseArray = [
  21.             'version' => '1.0',
  22.             'response' => [
  23.                 'outputSpeech' => [
  24.                     'type' => 'PlainText',
  25.                     'text' => 'Ciao, stai entrando o uscendo?',
  26.                     'ssml' => null
  27.                 ],
  28.                 'reprompt' => [
  29.                     'outputSpeech' => [
  30.                         'type' => 'PlainText',
  31.                         'text' => 'Tempo di risposta esaurito',
  32.                         'ssml' => null
  33.                     ]
  34.                 ],
  35.                 'shouldEndSession' => false
  36.             ]
  37.         ];
  38.     } elseif ($intentType == 'IntentRequest'){
  39.         switch($intent) {
  40.             case 'AzioneIntent':
  41.                 $azione = !empty($intentData['name']['value']) ? $intentData['name']['value'] : '';
  42.                 if (!empty($azione)) {
  43.                     $responseArray = [
  44.                         'version' => '1.0',
  45.                         'response' => [
  46.                             'outputSpeech' => [
  47.                                 'type' => 'PlainText',
  48.                                 'text' => 'Ok, mi hai inviato : ' . $azione,
  49.                                 'ssml' => null
  50.                             ],
  51.                             'shouldEndSession' => false
  52.                         ]
  53.                     ];
  54.                 }
  55.             break;
  56.             case 'IDIntent':
  57.                 $id = !empty($intentData['name']['value']) ? $intentData['name']['value'] : '';
  58.                 if (!empty($id)){
  59.                     $responseArray = [
  60.                         'version' => '1.0',
  61.                         'response' => [
  62.                             'outputSpeech' => [
  63.                                 'type' => 'PlainText',
  64.                                 'text' => 'Ok, mi hai inviato il seguente identificativo : ' . $id,
  65.                                 'ssml' => null
  66.                             ],
  67.                             'shouldEndSession' => false
  68.                         ]
  69.                     ];
  70.                 }
  71.             break;
  72.         }
  73.     }
  74.  
  75.     /*
  76.     $responseArray = [
  77.         'version' => '1.0',
  78.         'response' => [
  79.             'outputSpeech' => [
  80.                 'type' => 'PlainText',
  81.                 'text' => 'Ciao, inviami il tuo identificativo',
  82.                 'ssml' => null
  83.             ],
  84.             'shouldEndSession' => false
  85.         ]
  86.     ];
  87.     */
  88.     header ('Content-Type: application/json');
  89.     file_put_contents('alexaOutput.json', json_encode($responseArray, JSON_PRETTY_PRINT));
  90.     echo json_encode($responseArray);
  91.     die();
  92.  
  93. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement