Skip to content

Instantly share code, notes, and snippets.

@juntao
Created January 26, 2016 19:16
Show Gist options
  • Select an option

  • Save juntao/8fd35e7b978690cf14cf to your computer and use it in GitHub Desktop.

Select an option

Save juntao/8fd35e7b978690cf14cf to your computer and use it in GitHub Desktop.
<?php
if ($_REQUEST["user_name"] == "slackbot") {
exit;
}
$cache_file = $_REQUEST["token"] . '.txt';
if (!file_exists($cache_file)) {
// This is a new conversation. Start with search
$json_str = file_get_contents("http://www.omdbapi.com/?s=" . urlencode($_REQUEST["text"]) . "&r=json");
file_put_contents($cache_file, $json_str);
$movies_list_str = "";
$json = json_decode($json_str, true);
for($i=0; $i<count($json['Search']); $i++) {
$movies_list_str = $movies_list_str . ($i+1) . " " . $json['Search'][$i]['Title'] . "(" . $json['Search'][$i]['Year'] . ")\n";
}
?>
{
"text": "I have found the following movies matching your search. Please respond with a number to learn more about a movie.\n <?= $movies_list_str ?>",
"mrkdwn": true
}
<?php
} else {
$cache_content = file_get_contents($cache_file);
if (strlen($cache_content) < 10) {
// this is IMDB ID
$command = strtolower($_REQUEST["text"]);
if ($command == "detail") {
$imdb_url = "Follow this link for detail: http://www.imdb.com/title/" . $cache_content;
} elseif ($command == "showtime" || $command == "showtimes") {
$imdb_url = "Follow this link for showtimes: http://www.imdb.com/showtimes/title/" . $cache_content;
} elseif ($command == "cast") {
$imdb_url = "Follow this link for full cast and crew: http://www.imdb.com/title/" . $cache_content . "/fullcredits";
} elseif ($command == "trivia") {
$imdb_url = "Follow this link for trivia: http://www.imdb.com/title/" . $cache_content . "/trivia";
} elseif ($command == "quotes") {
$imdb_url = "Follow this link for quotes: http://www.imdb.com/title/" . $cache_content . "/quotes";
} elseif ($command == "done") {
?>
{
"text": "Bye <?= $_REQUEST["user_name"] ?>. See you next time!",
"mrkdwn": true
}
<?php
unlink($cache_file);
exit;
} else {
?>
{
"text": "Sorry, I did not understand. Please respond with one of the following: _detail_, _showtime_, _cast_, _trivia_, _quotes_ or _done_",
"mrkdwn": true
}
<?php
exit;
}
?>
{
"text": "<?= $imdb_url ?>",
"mrkdwn": true
}
<?php
} else {
// This is number selector on JSON array
$json = json_decode($cache_content, true);
$num = intval($_REQUEST["text"]);
if ($num < 1 || $num > count($json['Search'])) {
?>
{
"text": "Sorry, I did not understand. Please respond with a number in the list",
"mrkdwn": true
}
<?php
exit;
} else {
$movie = $json['Search'][$num - 1];
?>
{
"text": "You selected *<?= $movie['Title'] ?>* (<?= $movie['Year'] ?>) \nPlease respond with one of the following: _detail_, _showtime_, _cast_, _trivia_, _quotes_ or _done_ for more",
"mrkdwn": true
}
<?php
file_put_contents($cache_file, $movie['imdbID']);
exit;
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment