1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the TelegramBot package. |
4
|
|
|
* |
5
|
|
|
* (c) Avtandil Kikabidze aka LONGMAN <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Longman\TelegramBot\Commands\UserCommands; |
12
|
|
|
|
13
|
|
|
use Exception; |
14
|
|
|
use GuzzleHttp\Client; |
15
|
|
|
use GuzzleHttp\Exception\RequestException; |
16
|
|
|
use Longman\TelegramBot\Commands\UserCommand; |
17
|
|
|
use Longman\TelegramBot\Request; |
18
|
|
|
use Longman\TelegramBot\TelegramLog; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* User "/weather" command |
22
|
|
|
*/ |
23
|
|
|
class WeatherCommand extends UserCommand |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $name = 'weather'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $description = 'Show weather by location'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $usage = '/weather <location>'; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $version = '1.2.0'; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Base URI for OpenWeatherMap API |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
private $owm_api_base_uri = 'http://api.openweathermap.org/data/2.5/'; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get weather data using HTTP request |
54
|
|
|
* |
55
|
|
|
* @param string $location |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
|
|
private function getWeatherData($location) |
60
|
|
|
{ |
61
|
|
|
$client = new Client(['base_uri' => $this->owm_api_base_uri]); |
62
|
|
|
$path = 'weather'; |
63
|
|
|
$query = [ |
64
|
|
|
'q' => $location, |
65
|
|
|
'units' => 'metric', |
66
|
|
|
'APPID' => trim($this->getConfig('owm_api_key')), |
67
|
|
|
]; |
68
|
|
|
|
69
|
|
|
try { |
70
|
|
|
$response = $client->get($path, ['query' => $query]); |
71
|
|
|
} catch (RequestException $e) { |
72
|
|
|
TelegramLog::error($e->getMessage()); |
73
|
|
|
|
74
|
|
|
return ''; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return (string) $response->getBody(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get weather string from weather data |
82
|
|
|
* |
83
|
|
|
* @param array $data |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
private function getWeatherString(array $data) |
88
|
|
|
{ |
89
|
|
|
try { |
90
|
|
|
if (!(isset($data['cod']) && $data['cod'] === 200)) { |
91
|
|
|
return ''; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
//http://openweathermap.org/weather-conditions |
95
|
|
|
$conditions = [ |
96
|
|
|
'clear' => ' ☀️', |
97
|
|
|
'clouds' => ' ☁️', |
98
|
|
|
'rain' => ' ☔', |
99
|
|
|
'drizzle' => ' ☔', |
100
|
|
|
'thunderstorm' => ' ⚡️', |
101
|
|
|
'snow' => ' ❄️', |
102
|
|
|
]; |
103
|
|
|
$conditions_now = strtolower($data['weather'][0]['main']); |
104
|
|
|
|
105
|
|
|
return sprintf( |
106
|
|
|
'The temperature in %s (%s) is %s°C' . PHP_EOL . |
107
|
|
|
'Current conditions are: %s%s', |
108
|
|
|
$data['name'], //city |
109
|
|
|
$data['sys']['country'], //country |
110
|
|
|
$data['main']['temp'], //temperature |
111
|
|
|
$data['weather'][0]['description'], //description of weather |
112
|
|
|
isset($conditions[$conditions_now]) ? $conditions[$conditions_now] : '' |
113
|
|
|
); |
114
|
|
|
} catch (Exception $e) { |
115
|
|
|
TelegramLog::error($e->getMessage()); |
116
|
|
|
|
117
|
|
|
return ''; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Command execute method |
123
|
|
|
* |
124
|
|
|
* @return mixed |
125
|
|
|
* @throws \Longman\TelegramBot\Exception\TelegramException |
126
|
|
|
*/ |
127
|
|
|
public function execute() |
128
|
|
|
{ |
129
|
|
|
$message = $this->getMessage(); |
130
|
|
|
$chat_id = $message->getChat()->getId(); |
131
|
|
|
$text = ''; |
132
|
|
|
|
133
|
|
|
if (trim($this->getConfig('owm_api_key'))) { |
134
|
|
|
$location = trim($message->getText(true)); |
135
|
|
|
if ($location !== '') { |
136
|
|
|
if ($weather_data = json_decode($this->getWeatherData($location), true)) { |
137
|
|
|
$text = $this->getWeatherString($weather_data); |
138
|
|
|
} |
139
|
|
|
if ($text === '') { |
140
|
|
|
$text = 'Cannot find weather for location: ' . $location; |
141
|
|
|
} |
142
|
|
|
} else { |
143
|
|
|
$text = 'You must specify location in format: /weather <city>'; |
144
|
|
|
} |
145
|
|
|
} else { |
146
|
|
|
$text = 'OpenWeatherMap API key not defined.'; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$data = [ |
150
|
|
|
'chat_id' => $chat_id, |
151
|
|
|
'text' => $text, |
152
|
|
|
]; |
153
|
|
|
|
154
|
|
|
return Request::sendMessage($data); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|