-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathGifBot.php
More file actions
70 lines (56 loc) · 1.47 KB
/
GifBot.php
File metadata and controls
70 lines (56 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
/**
* Number of GIFs to reply with during #gifbomb. Slack will only open
* up to 3 images automatically.
*/
define('GIFBOMB_COUNT', 3);
// ----- END OF CONFIGURATION ----- //
require_once 'GifBot/Gifable.php';
require_once 'GifBot/Giphy.php';
require_once 'GifBot/Gif.php';
$gif = new GifBot\Giphy;
// Get request
$trigger = trim($_POST['trigger_word']);
$term = trim(substr($_POST['text'], strlen($trigger) + 1));
// Respond with random GIF if no search term is provided
if ($term == '') {
sendResponse($gif->random()->getUrl());
}
// Search
$gifs = $gif->search($term);
$count = count($gifs);
// Build reponse
$response = '';
if ($count > 0) {
if ($trigger == '#bomb') {
shuffle($gifs);
$pullCount = min($count, GIFBOMB_COUNT);
while ($pullCount > 0) {
$index = rand(0, $count - 1);
$response .= $gifs[$index]->getUrl() . "\n";
// Prevent duplicates
unset($gifs[$index]);
$gifs = array_values($gifs);
$pullCount--;
}
} else {
$response = $gifs[rand(0, $count - 1)]->getUrl();
}
} else {
$response = "No image found for '" . $term . "'. Enjoy this random GIF instead. " . $gif->random()->getUrl();
}
// Respond
sendResponse($response);
exit;
/**
* Send JSON Response
*
* @param $response
*/
function sendResponse($response)
{
header('Content-Type: application/json');
die(json_encode(array(
'text' => $response
)));
}