It should be simple, the MET Office publishes weather warnings in an RSS feed, and Slack has an RSS app you can use to post feeds straight into channels.

But it’s not.

The MET Office feed contains all active weather warnings, and the whole file has a single timestamp for when it was updated (more like a noticeboard than a news feed), so doesn’t add one to the individual entries. The Slack App looks for individual entries updated in the past 10 minutes, and because that field is missing nothing gets posted to the channel.

I raised a support ticket with Slack, but as it doesn’t work like a traditional news feed they wouldn’t do anything to support it. I sent some feedback to the MET Office, asking if a timestamp could be added, but didn’t hear anything back from them.

So as I couldn’t find any other way to get this information in the way I wanted, I decided to build my own solution.

I added a new php page to an existing website that could be added to the Slack App, used it to read the MET Office feed, copy the file’s timestamp, and insert that into each item in the feed.

Now we have weather warnings being posted to a channel, everyone get’s the same information at the same time, and they have everything they need to discuss anything that needs to be done to prepare for potential disruption.

<?php
   //headers to control cache expiry and indicate the content is an RSS Feed
   header('Content-Type: application/atom+xml; charset=utf-8');
   header('Cache-Control: must-revalidate, max-age=30');
   // calc an offset of 30s
   $offset = 30;
   // calc the string in GMT not localtime and add the offset
   $expire = "Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
   //output the HTTP header
   Header($expire);

   //read the feed for the whole of the UK
   $alert_feed = file_get_contents('http://www.metoffice.gov.uk/public/data/PWSCache/WarningsRSS/Region/UK');

   if ($alert_feed) {
      //find the timestamp
      $datePosition = strpos($alert_feed, "<dc:date>");
      $dateValue = substr($alert_feed,$datePosition+9,20);

      //add a timestamp to each item
      $modifiedFeed = str_replace("<item>","<item>\n      <dc:date>".$dateValue."</dc:date>",$modifiedFeed);

      //publish the updated content
      echo $modifiedFeed;

   } else {
      echo "";
   }
?>

It’s not particularly sophisticated, but it’s been enough to solve the problem I had. It’d be great to see other ways folk have solved this problem.

Photo by Johannes Plenio from Pexels