-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
Copy pathWeatherCommand.php
156 lines (136 loc) · 4.22 KB
/
WeatherCommand.php
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
<?php
/**
* This file is part of the PHP Telegram Bot example-bot package.
* https://github.com/php-telegram-bot/example-bot/
*
* (c) PHP Telegram Bot Team
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* User "/weather" command
*
* Get weather info for the location passed as the parameter..
*
* A OpenWeatherMap.org API key is required for this command!
* You can be set in your config.php file:
* ['commands']['configs']['weather'] => ['owm_api_key' => 'your_owm_api_key_here']
*/
namespace Longman\TelegramBot\Commands\UserCommands;
use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Entities\ServerResponse;
use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\TelegramLog;
class WeatherCommand extends UserCommand
{
/**
* @var string
*/
protected $name = 'weather';
/**
* @var string
*/
protected $description = 'Show weather by location';
/**
* @var string
*/
protected $usage = '/weather <location>';
/**
* @var string
*/
protected $version = '1.3.0';
/**
* Base URI for OpenWeatherMap API
*
* @var string
*/
private $owm_api_base_uri = 'http://api.openweathermap.org/data/2.5/';
/**
* Get weather data using HTTP request
*
* @param string $location
*
* @return string
*/
private function getWeatherData($location): string
{
$client = new Client(['base_uri' => $this->owm_api_base_uri]);
$path = 'weather';
$query = [
'q' => $location,
'units' => 'metric',
'APPID' => trim($this->getConfig('owm_api_key')),
];
try {
$response = $client->get($path, ['query' => $query]);
} catch (RequestException $e) {
TelegramLog::error($e->getMessage());
return '';
}
return (string) $response->getBody();
}
/**
* Get weather string from weather data
*
* @param array $data
*
* @return string
*/
private function getWeatherString(array $data): string
{
try {
if (!(isset($data['cod']) && $data['cod'] === 200)) {
return '';
}
//http://openweathermap.org/weather-conditions
$conditions = [
'clear' => ' ☀️',
'clouds' => ' ☁️',
'rain' => ' ☔',
'drizzle' => ' ☔',
'thunderstorm' => ' ⚡️',
'snow' => ' ❄️',
];
$conditions_now = strtolower($data['weather'][0]['main']);
return sprintf(
'The temperature in %s (%s) is %s°C' . PHP_EOL .
'Current conditions are: %s%s',
$data['name'], //city
$data['sys']['country'], //country
$data['main']['temp'], //temperature
$data['weather'][0]['description'], //description of weather
$conditions[$conditions_now] ?? ''
);
} catch (Exception $e) {
TelegramLog::error($e->getMessage());
return '';
}
}
/**
* Main command execution
*
* @return ServerResponse
* @throws TelegramException
*/
public function execute(): ServerResponse
{
// Check to make sure the required OWM API key has been defined.
$owm_api_key = $this->getConfig('owm_api_key');
if (empty($owm_api_key)) {
return $this->replyToChat('OpenWeatherMap API key not defined.');
}
$location = trim($this->getMessage()->getText(true));
if ($location === '') {
return $this->replyToChat('You must specify a location as: ' . $this->getUsage());
}
$text = 'Cannot find weather for location: ' . $location;
if ($weather_data = json_decode($this->getWeatherData($location), true)) {
$text = $this->getWeatherString($weather_data);
}
return $this->replyToChat($text);
}
}