{"id":803,"date":"2021-10-27T12:29:24","date_gmt":"2021-10-27T11:29:24","guid":{"rendered":"http:\/\/simkin.org\/wordpress\/?p=803"},"modified":"2022-06-13T11:09:39","modified_gmt":"2022-06-13T10:09:39","slug":"get-alexa-to-announce-tfl-rail-status-via-voice-monkey","status":"publish","type":"post","link":"http:\/\/simkin.org\/wordpress\/?p=803","title":{"rendered":"Get Alexa to announce TFL Rail (Elizabeth Line) Status via Voice Monkey"},"content":{"rendered":"\n<p>The nice people at TFL publish a <a href=\"https:\/\/api.tfl.gov.uk\/line\/mode\/tflrail\/status\">free API to get status information<\/a>. I had a few minutes spare today and decided to use it to push the info through Alexa. There are lots of clever ways to do this but I wanted to just throw something together in PHP and crontab as this doesn&#8217;t need to be a robust, industrial-scale thing, just a little quick-and-dirty test.<\/p>\n\n\n\n<p>First thing was to get an account on VoiceMonkey and add the VoiceMonkey skill to Alexa. That should not need a walkthrough, so go do that, add a Monkey called (for example) &#8220;TFLRailStatus&#8221; and you will end up with a &#8220;virtual doorbell&#8221; in Alexa with that name.<\/p>\n\n\n\n<p>Now create a routine in Alexa based on the &#8220;TFLRailStatus&#8221; doorbell being the trigger (under smarthome) and set the action to announce using the Voice Monkey skill and choose your target Alexa device.<\/p>\n\n\n\n<p>Use the <a href=\"https:\/\/app.voicemonkey.io\/url\">dashboard<\/a> to get the URL required to call this Monkey as you then only need to change the actual text.<\/p>\n\n\n\n<p>Now we need something that can call the new Monkey and thus tell Alexa to say something. The crude method is to search for \u201cstatusSeverityDescription\u201d: and scrape the following text strings<\/p>\n\n\n\n<p><code>\"statusSeverityDescription\":\"Minor Delays\",\"reason\":\"Minor delays between Liverpool Street and Shenfield due to a reason. GOOD SERVICE on the rest of the line. \"<\/code><\/p>\n\n\n\n<p>I did it with a bit of horrid PHP. Please don&#8217;t judge me. Employ your own flair to make this better before you do anything serious with it, but it will be enough to test the monkey and push a message out of Alexa:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\nini_set('display_errors', 1);\nini_set('display_startup_errors', 1);\nerror_reporting(E_ALL);\n\n        $tflrail = file_get_contents('https:\/\/api.tfl.gov.uk\/line\/mode\/elizabeth-line\/status');\n        $findme  = 'statusSeverityDescription';\n\n        $posStart = strpos($tflrail, $findme);\n        $posStart = $posStart + strlen($findme) + 3;\n        $posEnd = strpos($tflrail, '\"',$posStart);\n        $spacer = strpos($tflrail, '\"', $posEnd) + 3;\n        $posDescStart = strpos($tflrail, '\"', $spacer) + 3;\n        $posDescEnd = strpos($tflrail, '\",\"', $posDescStart+2);\n\n        $cmd = 'curl \"https:\/\/api.voicemonkey.io\/trigger?access_token=123458&amp;secret_token=678910&amp;monkey=tflrailstatus&amp;announcement=TFL%20Rail%20reports%20';\n\n        if (substr($tflrail,$posStart,$posEnd-$posStart)=='Good Service')\n                {\n                $cmd = $cmd.'A%20Good%20Service.\"';\n        } else {\n                 $cmd = $cmd.str_replace(\" \",\"%20\",substr($tflrail,$posDescStart,$posDescEnd-$posDescStart)).'\"';\n        }\n        $output = shell_exec($cmd);\n\n?>\n<\/code><\/pre>\n\n\n\n<p>What this does is extract the header and description and if it is a good service, says so, otherwise it posts the delay description using the Voice Monkey URL. You could obviously use a better technique to extract the text but as I said above, I&#8217;m just chucking out what I get.<\/p>\n\n\n\n<p>Point your web browser as this new PHP page and Alexa will tell you how the service is.<\/p>\n\n\n\n<p>Add a crontab entry to curl that page and Alexa can give you your morning TFL briefing. All done with very little code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo crontab -e\n\n# m h  dom mon dow   command\n15 7 * * 2,3,4 curl http:\/\/127.0.0.1\/tflrailstatus.php\n<\/code><\/pre>\n\n\n\n<p>** UPDATE &#8211; From May 24th 2022, the mode name changed from &#8220;tflrail&#8221; to &#8220;elizabeth-line&#8221; and in truth the name &#8220;TFL Rail&#8221; no long applies, so amend your code appropriately. The code segment above has the new mode name in it so will work as is.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The nice people at TFL publish a free API to get status information. I had a few minutes spare today &hellip; <a class=\"more-link\" href=\"http:\/\/simkin.org\/wordpress\/?p=803\">More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[3,8],"tags":[364,363,362],"jetpack_featured_media_url":"","_links":{"self":[{"href":"http:\/\/simkin.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/803"}],"collection":[{"href":"http:\/\/simkin.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/simkin.org\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/simkin.org\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/simkin.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=803"}],"version-history":[{"count":9,"href":"http:\/\/simkin.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/803\/revisions"}],"predecessor-version":[{"id":824,"href":"http:\/\/simkin.org\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/803\/revisions\/824"}],"wp:attachment":[{"href":"http:\/\/simkin.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=803"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/simkin.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=803"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/simkin.org\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}