Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
TelegramBot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kulya
TelegramBot
Commits
96ee5e5e
Commit
96ee5e5e
authored
Jun 01, 2016
by
Armando Lüscher
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Modify date command to use Guzzle instead of cURL.
parent
4ea56624
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
75 additions
and
68 deletions
+75
-68
DateCommand.php
src/Commands/UserCommands/DateCommand.php
+75
-68
No files found.
src/Commands/UserCommands/DateCommand.php
View file @
96ee5e5e
...
@@ -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
)
.
'×tamp='
.
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
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment