Created
January 4, 2017 09:28
-
-
Save Westlad/d6c72a0e6597e9723dcc974018e9eae0 to your computer and use it in GitHub Desktop.
PHP script to receive JSON from Amazon Echo and run a simple on/off script (with thanks to Ghostbit for the original code https://gist.github.com/ghostbitmeta/824474ceea327da629c7)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| $EchoJArray = json_decode(file_get_contents('php://input')); | |
| $RequestType = $EchoJArray->request->type; | |
| $JsonOut = GetJsonMessageResponse($RequestType,$EchoJArray); | |
| $size = strlen($JsonOut); | |
| header('Content-Type: application/json'); | |
| header("Content-length: $size"); | |
| echo $JsonOut; | |
| function GetJsonMessageResponse($RequestMessageType,$EchoJArray){ | |
| // Variable to confirm everything was successful; | |
| $pass = True; | |
| // ******************** Get state ********************; | |
| $action = $EchoJArray->request->intent->slots->power->value; | |
| if(empty($action)) { | |
| $action = "off"; | |
| } | |
| // ******************** Execute ********************; | |
| if($pass){ | |
| $cmd = "/home/pi/alexa/heater.py"; | |
| switch ($action) { | |
| case 'on': | |
| $output=shell_exec("$cmd -n"); | |
| break; | |
| case 'off': | |
| $status = shell_exec("$cmd -f"); | |
| break; | |
| } | |
| } | |
| // ******************** Responses ********************; | |
| if ($pass and $action=="off") { | |
| $text_title = "Turning shack heater off"; | |
| $text_response = "The shack heater is now off"; | |
| $voice_response = "The shack heater is now off"; | |
| $voice_reprompt = ""; | |
| } | |
| if ($pass and $action=="on") { | |
| $text_title = "Turning shack heater on"; | |
| $text_response = "The shack heater is now on"; | |
| $voice_response = "The shack heater is now on"; | |
| $voice_reprompt = ""; | |
| } | |
| // ******************** JSON reply built below ********************; | |
| $RequestId = $EchoJArray->request->requestId; | |
| $ReturnValue = ""; | |
| if( $RequestMessageType == "LaunchRequest" ){ | |
| $ReturnValue= ' | |
| { | |
| "version": "1.0", | |
| "sessionAttributes": { | |
| "countActionList": { | |
| "read": true, | |
| "category": true, | |
| "currentTask": "none", | |
| "currentStep": 0 | |
| } | |
| }, | |
| "response": { | |
| "outputSpeech": { | |
| "type": "PlainText", | |
| "text": "Done." | |
| } | |
| }, | |
| "card": { | |
| "type": "Simple", | |
| "title": "' . $text_title . '", | |
| "content": "' . $text_response . '" | |
| } | |
| }'; | |
| } | |
| if( $RequestMessageType == "SessionEndedRequest" ) | |
| { | |
| $ReturnValue = '{ | |
| "type": "SessionEndedRequest", | |
| "requestId": "$RequestId", | |
| "timestamp": "' . date("c") . '", | |
| "reason": "USER_INITIATED " | |
| } | |
| '; | |
| } | |
| if( $RequestMessageType == "IntentRequest" ){ | |
| $ReturnValue= ' | |
| { | |
| "version": "1.0", | |
| "sessionAttributes": { | |
| "countActionList": { | |
| "read": true, | |
| "category": true, | |
| "currentTask": "none", | |
| "currentStep": 0 | |
| } | |
| }, | |
| "response": { | |
| "outputSpeech": { | |
| "type": "PlainText", | |
| "text": "' . $voice_response . '" | |
| }, | |
| "card": { | |
| "type": "Simple", | |
| "title": "' . $text_title . '", | |
| "content": "' . $text_response . '" | |
| }, | |
| "reprompt": { | |
| "outputSpeech": { | |
| "type": "PlainText", | |
| "text": "' . $voice_reprompt . '" | |
| } | |
| }, | |
| "shouldEndSession": true | |
| } | |
| }'; | |
| } | |
| return $ReturnValue; | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment