Amazon ALEXA Lists Exercise – Put’em on Paper…
4.3 Edge server PHP code
Your EDGE server must be able to run PHP code. I have a LAMP running latest PHP 7.0 and this tiny exercise script named edge_script_printer.php fits perfectly.
- Server is exposed at my dyndns via HOSTNAME:443 with a self signed certificate.
- Script is therefore available at https://$DYNDNSHOST$/edge_script_printer.php . Obviously, if you put this into another path, adapt the python glue code above.
- Script has the following GET or POST parameters:
- secret : same as above in Python script
- listName : UTF8 URL encoded name for the list, 1-32 characters
- listItems : URL encoded JSON list of items, e.g. [„tomatoes“, „mozarella“] becomes %5B%22tomatoes%22%2C%20%22mozarella%22%5D%C2%A0 . Note that on older PHP5 json implementations you cannot use an apostroph, but must use double quotes as shown here.
Aaaand here’s the EDGE server code, nothing fancy:
<?php // small obscurity secret checking... $localsecret = "1234567890__REPLACE__WITH__YOUR__SECRET__1234567890" // obviously, same as above in python script $secret = $_REQUEST["secret"]; if ($localsecret != $secret) { header("HTTP/1.1 403 Unauthorized"); exit(1); } $listName = urlencode($_REQUEST["listName"]); $listItems = json_decode($_REQUEST["listItems"]); // Remove comment to test and track issues // ob_flush(); // ob_start(); // var_dump($_REQUEST["listName"]); // var_dump($_REQUEST["listItems"]); // file_put_contents("edge_script_printer_dump.txt", ob_get_flush()."\r\n", FILE_APPEND); if (!is_array($listItems) || count($listItems)<1 || count($listItems)>30) { header("HTTP/1.1 400 Bad Request"); $error = array( "statusCode"=>400, "statusText"=>"Please provide at least 1 and at best 30 list item(s) in request parameter named 'listItems' in form of a JSON UTF8 list.", "listName"=>$_REQUEST["listName"], "listItems"=>$_REQUEST["listItems"] ); echo json_encode($error); exit(2); } $ch = curl_init("http://!!!!!!!!!INTERNAL_PRINT_SERVER_HOST_NAME_OR_IP!!!!!!!!!/internal_printer.php?listName=$listName&listItems=". urlencode($_REQUEST["listItems"])); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_TIMEOUT, 3); $data = curl_exec($ch); $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($code != 200) { header("HTTP/1.1 500 Internal Server Error"); exit(1); } curl_close($ch); header("HTTP/1.1 200 OK"); echo "List printed successfully."; ?>
Note that you’re required to change two things:
- line 3: use your own secret here from above (python script).
- line 28: replace with your raspberry pis internal, static IP address or host name.
The script just passes on listName and listItems as-is to the internal print server and waits for a reply.
4.4 Testing
As an intermediate step, we can test at this point by uncommenting the five lines in the PHP script above.
- Tell ALEXA to put tomatoes on your shopping list
- Ask ALEXA to start „list printer“, this should result in a listName=“shopping list“ and listItems=[„tomatoes“] dumped to the edge_script_printer_dump.txt file.
You can use Amazon CloudWatch to track any errors in the Python Script.
5 Kommentare zu “Amazon ALEXA Lists Exercise – Put’em on Paper…”
Hi,
great post.
Could please tell which thermal printer you have used?
The amazon link isnt‘ working anymore.
Thanks
hi, Thanks. The one I used isn’t available on Amazon anymore, but any USB receipt printer should work… Just look for what mike24’s ESC/POS library is supporting and buy one of those models… Best, Christoph
Thanks for the reply. This product list on github helps a lot.
One further question: Could you really delete the original shopping list with the skill? In your video you use the original „Einkaufsliste“ so you could also delete this one per API? In your code for the lambda it is „shopping list“ and not the original code snipped?
Danke und Gruß
Tom
hi, well, the Einkaufsliste is used by the ALEXA language model. it translates into „shopping list“ in the skill. Therefore the difference… Cheers.
Hello!
Nice work ! I try to integrate it to home assistant .
Your video link is dead could you update it please?
regards
Ben
Einen Kommentar hinterlassen