-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMagicBotCommand.php
More file actions
60 lines (50 loc) · 1.63 KB
/
Copy pathMagicBotCommand.php
File metadata and controls
60 lines (50 loc) · 1.63 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
<?php
/**
* Class MagicBotCommand
* This class randomnly answers using Magic_8-Ball
*
* usage:
* ask sentence
*
*
* @author: Karez Bartolo
* @date: 15/11/2017
*/
class MagicBotCommand extends BotCommand {
public function __construct($sender, $user) {
parent::__construct("ASK", $sender, $user);
}
protected function executeCommand($parameter) {
$MAGIC_URL = "https://8ball.delegator.com/magic/JSON/";
if (!empty($parameter)) {
$response = $this->curl_invoke($MAGIC_URL.$parameter);
$talkback = json_decode($response, true);
$answer = $talkback["magic"]["answer"];
$type = $talkback["magic"]["type"];
if ($type == "Affirmative") {
$emoji = ":)";
} elseif ($type = "Contrary") {
$emoji = ":(";
} else {
$emoji = ":|";
}
$this->send("Hey ".$this->user->getFirstName().", ".lcfirst($answer)." ".$emoji);
} else {
$this->send("Maybe you forgot your pants today? Ask a question! ".$this->user->getFirstName()." :poop:");
}
}
protected function curl_invoke($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// Handle failed query
if ($httpcode != 200) {
$this->send("Sorry! My spirits are out and about: ".$httpcode);
die();
}
return $response;
}
}