Commit 96ee5e5e authored by Armando Lüscher's avatar Armando Lüscher

Modify date command to use Guzzle instead of cURL.

parent 4ea56624
...@@ -10,6 +10,8 @@ ...@@ -10,6 +10,8 @@
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\Exception\TelegramException;
use Longman\TelegramBot\Request; use Longman\TelegramBot\Request;
...@@ -25,16 +27,29 @@ class DateCommand extends UserCommand ...@@ -25,16 +27,29 @@ class DateCommand extends UserCommand
protected $name = 'date'; protected $name = 'date';
protected $description = 'Show date/time by location'; protected $description = 'Show date/time by location';
protected $usage = '/date <location>'; protected $usage = '/date <location>';
protected $version = '1.2.1'; protected $version = '1.3.0';
protected $public = true;
/**#@-*/ /**#@-*/
/**
* Guzzle Client object
*
* @var \GuzzleHttp\Client
*/
private $client;
/** /**
* Base URL for Google Maps API * Base URL for Google Maps API
* *
* @var string * @var string
*/ */
private $base_url = 'https://maps.googleapis.com/maps/api'; private $google_api_base_uri = 'https://maps.googleapis.com/maps/api/';
/**
* The Google API Key from the command config
*
* @var string
*/
private $google_api_key;
/** /**
* Date format * Date format
...@@ -52,32 +67,28 @@ class DateCommand extends UserCommand ...@@ -52,32 +67,28 @@ class DateCommand extends UserCommand
*/ */
private function getCoordinates($location) private function getCoordinates($location)
{ {
$url = $this->base_url . '/geocode/json?'; $path = 'geocode/json';
$params = 'address=' . urlencode($location); $query = ['address' => urlencode($location)];
$google_api_key = $this->getConfig('google_api_key'); if ($this->google_api_key !== '') {
if (!empty($google_api_key)) { $query['key'] = $this->google_api_key;
$params .= '&key=' . $google_api_key;
} }
$data = $this->request($url . $params); try {
if (empty($data)) { $response = $this->client->get($path, ['query' => $query])->getBody();
return false; } catch (RequestException $e) {
throw new TelegramException($e->getMessage());
} }
$data = json_decode($data, true); if (!($result = $this->validateResponseData($response))) {
if (empty($data)) {
return false;
}
if ($data['status'] !== 'OK') {
return false; return false;
} }
$lat = $data['results'][0]['geometry']['location']['lat']; $result = $result['results'][0];
$lng = $data['results'][0]['geometry']['location']['lng']; $lat = $result['geometry']['location']['lat'];
$acc = $data['results'][0]['geometry']['location_type']; $lng = $result['geometry']['location']['lng'];
$types = $data['results'][0]['types']; $acc = $result['geometry']['location_type'];
$types = $result['types'];
return [$lat, $lng, $acc, $types]; return [$lat, $lng, $acc, $types];
} }
...@@ -92,20 +103,44 @@ class DateCommand extends UserCommand ...@@ -92,20 +103,44 @@ class DateCommand extends UserCommand
*/ */
private function getDate($lat, $lng) private function getDate($lat, $lng)
{ {
$url = $this->base_url . '/timezone/json?'; $path = 'timezone/json';
$date_utc = new \DateTime(null, new \DateTimeZone("UTC"));
$date_utc = new \DateTime(null, new \DateTimeZone('UTC'));
$timestamp = $date_utc->format('U'); $timestamp = $date_utc->format('U');
$params = 'location=' . urlencode($lat) . ',' . urlencode($lng) . '&timestamp=' . urlencode($timestamp); $query = [
'location' => urlencode($lat) . ',' . urlencode($lng),
'timestamp' => urlencode($timestamp)
];
$google_api_key = $this->getConfig('google_api_key'); if ($this->google_api_key !== '') {
if (!empty($google_api_key)) { $query['key'] = $this->google_api_key;
$params .= '&key=' . $google_api_key; }
try {
$response = $this->client->get($path, ['query' => $query])->getBody();
} catch (RequestException $e) {
throw new TelegramException($e->getMessage());
}
if (!($result = $this->validateResponseData($response))) {
return false;
} }
$data = $this->request($url . $params); $local_time = $timestamp + $result['rawOffset'] + $result['dstOffset'];
return [$local_time, $result['timeZoneId']];
}
/**
* Evaluate the response data and see if the request was successful
*
* @param string $data
*
* @return bool|array
*/
private function validateResponseData($data)
{
if (empty($data)) { if (empty($data)) {
return false; return false;
} }
...@@ -115,13 +150,11 @@ class DateCommand extends UserCommand ...@@ -115,13 +150,11 @@ class DateCommand extends UserCommand
return false; return false;
} }
if ($data['status'] !== 'OK') { if (isset($data['status']) && $data['status'] !== 'OK') {
return false; return false;
} }
$local_time = $timestamp + $data['rawOffset'] + $data['dstOffset']; return $data;
return [$local_time, $data['timeZoneId']];
} }
/** /**
...@@ -150,56 +183,30 @@ class DateCommand extends UserCommand ...@@ -150,56 +183,30 @@ class DateCommand extends UserCommand
return 'The local time in ' . $timezone_id . ' is: ' . $date_utc->format($this->date_format); return 'The local time in ' . $timezone_id . ' is: ' . $date_utc->format($this->date_format);
} }
/**
* Perform a simple cURL request
*
* @param string $url
*
* @return object
*/
private function request($url)
{
$ch = curl_init();
$curlConfig = [CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true];
curl_setopt_array($ch, $curlConfig);
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($http_code !== 200) {
throw new TelegramException('Error receiving data from url');
}
curl_close($ch);
return $response;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function execute() public function execute()
{ {
//First we set up the necessary member variables.
$this->client = new Client(['base_uri' => $this->google_api_base_uri]);
$this->google_api_key = $this->getConfig('google_api_key');
$message = $this->getMessage(); $message = $this->getMessage();
$chat_id = $message->getChat()->getId(); $chat_id = $message->getChat()->getId();
$message_id = $message->getMessageId(); $message_id = $message->getMessageId();
$text = $message->getText(true); $location = $message->getText(true);
if (empty($text)) { if (empty($location)) {
$text = 'You must specify location in format: /date <city>'; $text = 'You must specify location in format: /date <city>';
} else { } else {
$date = $this->getformattedDate($text); $text = $this->getformattedDate($location);
if (empty($date)) {
$text = 'Can not find date for location: ' . $text;
} else {
$text = $date;
}
} }
$data = [ $data = [
'chat_id' => $chat_id, 'chat_id' => $chat_id,
'reply_to_message_id' => $message_id, 'text' => $text,
'text' => $text,
]; ];
return Request::sendMessage($data); return Request::sendMessage($data);
......
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