-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeatherBotCommand.php
More file actions
158 lines (146 loc) · 5.89 KB
/
Copy pathWeatherBotCommand.php
File metadata and controls
158 lines (146 loc) · 5.89 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/**
* Class WeatherBotCommand
* This class returns the current weather condition and forecast for the given location.
*
* Usage:
* WEATHER <location>
*
* @author: Angelito Sardez, Jr.
* @date: 16/11/2017
*/
class WeatherBotCommand extends BotCommand
{
public function __construct($sender, $user)
{
parent::__construct("WEATHER", $sender, $user);
}
protected function executeCommand($parameter)
{
if (trim($parameter) == "") {
$this->sendTextWithHelp("Sorry ".$this->user->getFirstName().", you need to specify the location, whose weather condition and forecast you wanted to see. (e.g. WEATHER Manila PH)");
return;
}
$locations = $this->queryLocation($parameter);
$validLocationsCount = $this->getValidLocationsCount($locations);
if ($validLocationsCount == 0) {
$this->send("No weather condition and forecast found for the location \"".$parameter."\", ".$this->user->getFirstName().". Please make sure that the place exists or be more specific in providing the location. (e.g. WEATHER Singapore, Singapore)");
}
else {
if ($validLocationsCount > 1) {
$this->sendMultipleLocationsMessage($locations, $parameter);
}
else {
$zmwCode = $this->getZmwCode($locations);
$weatherCondition = $this->getWeatherCondition($zmwCode);
$this->sendWeatherCondition($zmwCode, $weatherCondition);
}
}
}
function queryLocation($location) {
return json_decode(file_get_contents('http://autocomplete.wunderground.com/aq?query='.urlencode($location)), true);
}
function getValidLocationsCount($locations) {
$count = 0;
if (!empty($locations['RESULTS'])) {
foreach ($locations['RESULTS'] as &$place) {
if (strtolower($place['tz']) != 'missing') {
$count++;
}
}
}
return $count;
}
function getZmwCode($locations) {
$zmwCode = "";
if (!empty($locations['RESULTS'])) {
foreach ($locations['RESULTS'] as &$place) {
if (strtolower($place['tz']) != 'missing') {
$zmwCode = $place['zmw'];
break;
}
}
}
return $zmwCode;
}
function getWeatherCondition($zmwCode) {
return json_decode(file_get_contents('http://api.wunderground.com/api/ec23707d4592d0cb/conditions/q/zmw:'.urlencode($zmwCode.'.json')), true);
}
function sendWeatherCondition($zmwCode, $weatherCondition) {
$this->send(["attachment"=>[
"type"=>"template",
"payload"=>[
"template_type"=>"list",
"top_element_style"=>"large",
"elements"=>[
[
"title"=>'Current weather in '.$weatherCondition['current_observation']['display_location']['full'].':',
"image_url"=>$weatherCondition['current_observation']['icon_url'],
"subtitle"=>$weatherCondition['current_observation']['weather'].' at '.$weatherCondition['current_observation']['temperature_string'].'.'
],
[
"title"=>'Precipitation forecast:',
"subtitle"=>'Today would be '.$weatherCondition['current_observation']['precip_today_string'].', while '.$weatherCondition['current_observation']['precip_1hr_string'].' in the next hour.'
]
],
"buttons"=>[
[
"type"=>'web_url',
"url"=>'http://www.wunderground.com/q/zmw:'.$zmwCode,
"title"=>"View Full Forecast"
]
]
]
]]);
}
function getListTemplate() {
$template = ["attachment"=>[
"type"=>"template",
"payload"=>[
"template_type"=>"list",
"top_element_style"=>"compact",
"elements"=>array()
]
]];
return $template;
}
function sendMultipleLocationsMessage($locations, $location) {
$this->send("Hey ".$this->user->getFirstName().", there are more than 1 location that matched \"".$location."\". Which one are you looking for:");
$this->sendAction(SenderAction::typingOn);
$responseTemplate = $this->getListTemplate();
$firstMatch = '';
$locationCount = 0;
foreach ($locations['RESULTS'] as &$place) {
if (strtolower($place['tz']) != 'missing') {
if ($firstMatch == '') {
$firstMatch = $place['name'];
}
$responseTemplate["attachment"]["payload"]["elements"][] = [
"title"=>$place['name'],
"buttons"=>[
[
"type"=>'postback',
"title"=>'View Weather',
"payload"=>'WEATHER '.$place['name']
]
]
];
$locationCount++;
if ($locationCount == 4) {
$this->send($responseTemplate);
$responseTemplate = $this->getListTemplate();
$locationCount = 0;
$this->sendAction(SenderAction::typingOn);
}
}
}
if ($locationCount == 1) {
$responseTemplate["attachment"]["payload"]["template_type"] = "generic";
unset($responseTemplate["attachment"]["payload"]["top_element_style"]);
}
if ($locationCount > 0) {
$this->send($responseTemplate);
}
$this->send("Next time you can skip this by providing the location completely e.g. \"WEATHER ".$firstMatch."\".");
}
}