Commit 9f3388d4 authored by Armando Lüscher's avatar Armando Lüscher

Modify weather command to use Guzzle instead of cURL.

Also rewrite entire command to require and API key from the config, as it is now required to fetch weather data.
parent a477aad2
...@@ -10,7 +10,10 @@ ...@@ -10,7 +10,10 @@
namespace Longman\TelegramBot\Commands\UserCommands; namespace Longman\TelegramBot\Commands\UserCommands;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use Longman\TelegramBot\Commands\UserCommand; use Longman\TelegramBot\Commands\UserCommand;
use Longman\TelegramBot\Exception\TelegramException;
use Longman\TelegramBot\Request; use Longman\TelegramBot\Request;
/** /**
...@@ -24,89 +27,79 @@ class WeatherCommand extends UserCommand ...@@ -24,89 +27,79 @@ class WeatherCommand extends UserCommand
protected $name = 'weather'; protected $name = 'weather';
protected $description = 'Show weather by location'; protected $description = 'Show weather by location';
protected $usage = '/weather <location>'; protected $usage = '/weather <location>';
protected $version = '1.0.1'; protected $version = '1.1.0';
protected $public = true;
/**#@-*/ /**#@-*/
/** /**
* Get weather using cURL request * 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 * @param string $location
* *
* @return object * @return string
*/ */
private function getWeather($location) private function getWeatherData($location)
{ {
$url = 'http://api.openweathermap.org/data/2.5/weather?q=' . $location . '&units=metric'; $client = new Client(['base_uri' => $this->owm_api_base_uri]);
$path = 'weather';
$ch = curl_init(); $query = [
$curlConfig = [ 'q' => $location,
CURLOPT_URL => $url, 'units' => 'metric',
CURLOPT_RETURNTRANSFER => true, 'APPID' => trim($this->getConfig('owm_api_key')),
]; ];
curl_setopt_array($ch, $curlConfig); try {
$response = curl_exec($ch); $response = $client->get($path, ['query' => $query]);
} catch (RequestException $e) {
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); throw new TelegramException($e->getMessage());
if ($http_code !== 200) {
throw new \Exception('Error receiving data from url');
} }
curl_close($ch);
return $response; return (string)$response->getBody();
} }
/** /**
* Get weather string * Get weather string from weather data
* *
* @param string $location * @param array $data
* *
* @return bool|string * @return bool|string
*/ */
private function getWeatherString($location) private function getWeatherString(array $data)
{ {
if (empty($location)) {
return false;
}
try { try {
$data = $this->getWeather($location); if (empty($data) || $data['cod'] !== 200) {
$decode = json_decode($data, true);
if (empty($decode) || $decode['cod'] != 200) {
return false; return false;
} }
$city = $decode['name']; //http://openweathermap.org/weather-conditions
$country = $decode['sys']['country']; $conditions = [
$temp = 'The temperature in ' . $city . ' (' . $country . ') is ' . $decode['main']['temp'] . '°C'; 'clear' => ' ☀️',
$conditions = 'Current conditions are: ' . $decode['weather'][0]['description']; 'clouds' => ' ☁️',
'rain' => ' ☔',
switch (strtolower($decode['weather'][0]['main'])) { 'drizzle' => ' ☔',
case 'clear': 'thunderstorm' => ' ⚡️',
$conditions .= ' ☀'; 'snow' => ' ❄️',
break; ];
$conditions_now = strtolower($data['weather'][0]['main']);
case 'clouds':
$conditions .= ' ☁☁'; return sprintf(
break; 'The temperature in %1$s (%2$s) is %3$s°C' . "\n" .
'Current conditions are: %4$s%5$s',
case 'rain': $data['name'], //city
$conditions .= ' ☔'; $data['sys']['country'], //country
break; $data['main']['temp'], //temperature
$data['weather'][0]['description'], //description of weather
case 'thunderstorm': (isset($conditions[$conditions_now])) ? $conditions[$conditions_now] : ''
$conditions .= ' ☔☔☔☔'; );
break;
}
$result = $temp . "\n" . $conditions;
} catch (\Exception $e) { } catch (\Exception $e) {
$result = ''; return false;
} }
return $result;
} }
/** /**
...@@ -115,25 +108,26 @@ class WeatherCommand extends UserCommand ...@@ -115,25 +108,26 @@ class WeatherCommand extends UserCommand
public function execute() public function execute()
{ {
$message = $this->getMessage(); $message = $this->getMessage();
$chat_id = $message->getChat()->getId(); $chat_id = $message->getChat()->getId();
$message_id = $message->getMessageId(); $text = '';
$text = $message->getText(true);
if (empty($text)) { if (trim($this->getConfig('owm_api_key'))) {
$text = 'You must specify location in format: /weather <city>'; if ($location = trim($message->getText(true))) {
} else { if ($weather_data = json_decode($this->getWeatherData($location), true)) {
$weather = $this->getWeatherString($text); $text = $this->getWeatherString($weather_data);
if (empty($weather)) { }
$text = 'Can not find weather for location: ' . $text; if (!$text) {
$text = 'Cannot find weather for location: ' . $location;
}
} else { } else {
$text = $weather; $text = 'You must specify location in format: /weather <city>';
} }
} else {
$text = 'OpenWeatherMap API key not defined.';
} }
$data = [ $data = [
'chat_id' => $chat_id, 'chat_id' => $chat_id,
'reply_to_message_id' => $message_id,
'text' => $text, 'text' => $text,
]; ];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment