-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUniversityBotCommand.php
More file actions
79 lines (65 loc) · 2.35 KB
/
Copy pathUniversityBotCommand.php
File metadata and controls
79 lines (65 loc) · 2.35 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
71
72
73
74
75
76
77
78
79
<?php
/**
* Class UniversityBotCommand
* This class returns the official website of the given university.
*
* Usage:
* UNIVERSITY <search>
*
* @author: Ace Mangalino
* @date: 17/11/2017
*/
class UniversityBotCommand extends BotCommand {
public function __construct($sender, $user) {
parent::__construct("UNIVERSITY", $sender, $user);
}
protected function executeCommand($parameter) {
/**$this->send($this->command." command is not yet implemented, ".$this->user->getFirstName().". Parameter passed: ".$parameter);*/
if (trim($parameter) == "") {
$this->sendTextWithHelp("Sorry ".$this->user->getFirstName().", please type the keyword UNIVERSITY university name to search the website of the school, e.g. UNIVERSITY Manila");
return;
}
$university = $this->queryUniversity($parameter);
if (empty($university)) {
$this->send("No university found, ".$this->user->getFirstName().". Please make sure the university exists.");
}
else {
/* $answer = ["attachment"=>[
"type"=>"template",
"payload"=>[
"template_type"=>"button",
"elements"=>[
"buttons"=>[
[
"type"=>"web_url",
"url"=>$university['web_pages'][0],
"title"=>"Visit Website"
],
]
]
]
]]; */
$responseTemplate["attachment"]["payload"]["elements"][] = [
"buttons"=>[
[
"type"=>'web_url',
"url"=>$university['web_pages'][0],
"title"=>'Visit Website'
]
]
];
//$this->send($university['name'].chr(10)."The website address: ".$university['web_pages'][0] . $responseTemplate);
$this->send($university['name'].chr(10)."The website address: ".$university['web_pages'][0]);
}
}
function queryUniversity($name) {
//return json_decode(file_get_contents('http://universities.hipolabs.com/search?name='.urlencode($name)), true);
$universities = json_decode(file_get_contents('http://universities.hipolabs.com/search?name='.urlencode($name)), true);
// extract university name given in $name by the user.
foreach ($universities as &$university) {
//if ($university['name'] == $name) {
return $university;
//}
}
}
}