Last active
December 10, 2017 22:16
-
-
Save daverix/70d8dfa45d2b02c93f887ef241370250 to your computer and use it in GitHub Desktop.
lunch.php
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 | |
function loadDocument($url) { | |
$pageContent = file_get_contents($url); | |
$document = new DOMDocument(); | |
$document->loadHtml($pageContent); | |
return $document; | |
} | |
if($_SERVER['REQUEST_METHOD'] == 'POST') { | |
$restaurants['af'] = function($weekday) { | |
$document = loadDocument('http://www.butlercatering.se/print/11'); | |
$xpath = new DOMXpath($document); | |
$days = $xpath->query("//div[@class='field-day']"); | |
if(count($days) == 0) { | |
echo "Menu is empty or html is changed"; | |
return; | |
} | |
if($weekday > 5) { | |
echo "Menu not available for today"; | |
return; | |
} | |
$today = $days->item($weekday-1); | |
$nodes = $today->childNodes; | |
$menuText=""; | |
foreach($nodes as $node) { | |
if($node->nodeName != "h3") { | |
$menuText .= trim($node->nodeValue). "\n"; | |
} | |
} | |
header("Content-type: application/json"); | |
echo "{\"response_type\": \"in_channel\", \"text\": \"$menuText\"}"; | |
}; | |
$restaurants['mektagonen'] = function($weekday) { | |
$document = loadDocument('http://restaurangmektagonen.se/lunch'); | |
$xpath = new DOMXpath($document); | |
$days = $xpath->query("//p[@class='mat']"); | |
if(count($days) == 0) { | |
echo "Menu is empty or html is changed"; | |
return; | |
} | |
if($weekday > 5) { | |
echo "Menu not available for today"; | |
return; | |
} | |
$today = $days->item($weekday-1); | |
$menuText=""; | |
foreach($nodes as $node) { | |
if($node->nodeName != "br") { | |
$menuText .= trim($node->nodeValue). "\n"; | |
} | |
} | |
header("Content-type: application/json"); | |
echo "{\"response_type\": \"in_channel\", \"text\": \"$menuText\"}"; | |
}; | |
$text = $_POST['text']; | |
$key = strtolower($text); | |
if(array_key_exists($key, $restaurants)) { | |
$weekday = 1; //date('N'); | |
$restaurants[$key]($weekday); | |
} elseif($text == "restaurants") { | |
$restaurantsText = implode("\n", array_keys($restaurants)); | |
echo "Available restaurants:\n$restaurantsText\n"; | |
} else { | |
echo "$text is not a valid restaurant.\n\nType \"/lunchmenu restaurants\" for a list of configured restaurants."; | |
} | |
} else { | |
?> | |
<!Doctype html> | |
<html> | |
<title>Lunchbot</title> | |
<body> | |
<form action="/lunch.php" method="post"> | |
/lunchmenu | |
<input type="text" name="text" /> | |
<input type="submit" value="Send" /> | |
</form> | |
<body> | |
</html> | |
<?php | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment