Last active
March 27, 2020 03:02
-
-
Save mattzuba/8b0a3b6734b126861c13cbd930d9ed0f to your computer and use it in GitHub Desktop.
/covid19 Slack Slash command in PHP using Johns Hopkins data
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 | |
/** | |
* Create a new slack command such as /covid19. Put this script in a web accessible location | |
* and point your slash command at it. Update the $slack_token below with the token for your | |
* command. Accepts a none or one arguments - with no arguments provides US data; one argument | |
* can be the name of a state to get data for. Not much in the way of error checking, was quick | |
* and dirty. | |
*/ | |
$slack_token = '<token>'; // Put your slash command token here | |
if (filter_input(INPUT_POST, 'token') !== $slack_token) { | |
http_response_code(400); | |
die('bad bad bad'); | |
} | |
$user_name = filter_input(INPUT_POST, 'user_name'); | |
$state = filter_input(INPUT_POST, 'text'); | |
if (empty($state = trim($state))) { | |
$state = null; | |
} | |
$today = date_create(null, new DateTimeZone('America/Los_Angeles'))->format('m-d-Y'); | |
$yesterday = date_create('yesterday', new DateTimeZone('America/Los_Angeles'))->format('m-d-Y'); | |
$files = [ | |
'county_report_yesterday' => "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/$yesterday.csv", | |
'county_report_today' => "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/$today.csv", | |
]; | |
$temp = sys_get_temp_dir(); | |
foreach ($files as $file => $url) { | |
$filePath = $temp . DIRECTORY_SEPARATOR . $file . '.csv'; | |
if (!file_exists($filePath) || filemtime($filePath) < time() - 3600) { | |
file_put_contents($filePath, file_get_contents($url), LOCK_EX); | |
} | |
$$file = new SplFileObject($filePath); | |
$$file->setFlags(SplFileObject::READ_CSV); | |
} | |
function get_stats(SplFileObject $file, ?string $state) { | |
$state_confirmed = 0; | |
$state_deaths = 0; | |
$state_recovered = 0; | |
$us_confirmed = 0; | |
$us_deaths = 0; | |
$us_recovered = 0; | |
foreach ($file as $line) { | |
if (count($line) < 5) { | |
continue; | |
} | |
if ($state !== null && strcasecmp($state, $line[2]) === 0) { | |
$state_confirmed += $line[7]; | |
$state_deaths += $line[8]; | |
$state_recovered += $line[9]; | |
} | |
if (strcasecmp('US', $line[3]) === 0) { | |
$us_confirmed += $line[7]; | |
$us_deaths += $line[8]; | |
$us_recovered += $line[9]; | |
} | |
} | |
return compact('state_confirmed', 'state_deaths', 'state_recovered', 'us_confirmed', 'us_deaths', 'us_recovered'); | |
} | |
$template = 'According to data from <https://coronavirus.jhu.edu/map.html|Johns Hopkins>, I have infected $infected people (a $infected_increase% increase over yesterday). $recovered people have recovered and sadly $deaths people have died.'; | |
$today_stats = get_stats($county_report_today, $state); | |
$yesterday_stats = get_stats($county_report_yesterday, $state); | |
$text = "<@$user_name> asked for my stats"; | |
if ($state !== null) { | |
$text .= ' for ' . ucwords($state) . ': '; | |
$text .= strtr($template, [ | |
'$infected' => $today_stats['state_confirmed'], | |
'$infected_increase' => round(($today_stats['state_confirmed'] * 100 / $yesterday_stats['state_confirmed']) - 100, 2), | |
'$recovered' => $today_stats['state_recovered'], | |
'$deaths' => $today_stats['state_deaths'] | |
]); | |
} else { | |
$text .= ' for the United States: '; | |
$text .= strtr($template, [ | |
'$infected' => $today_stats['us_confirmed'], | |
'$infected_increase' => round(($today_stats['us_confirmed'] * 100 / $yesterday_stats['us_confirmed']) - 100, 2), | |
'$recovered' => $today_stats['us_recovered'], | |
'$deaths' => $today_stats['us_deaths'] | |
]); | |
} | |
$response = [ | |
'response_type' => 'in_channel', | |
'text' => $text, | |
'unfurl_links' => false, | |
'unfurl_media' => false, | |
]; | |
header('Content-Type: application/json'); | |
echo json_encode($response); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment