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
dc174281
Commit
dc174281
authored
Jul 16, 2015
by
MBoretto
Browse files
Options
Browse Files
Download
Plain Diff
Merge remote-tracking branch 'upstream/master'
parents
ac19ac11
2b293902
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
71 additions
and
20 deletions
+71
-20
README.md
README.md
+3
-0
Command.php
src/Command.php
+15
-0
DateCommand.php
src/Commands/DateCommand.php
+16
-11
Telegram.php
src/Telegram.php
+36
-8
Bootstrap.php
tests/Bootstrap.php
+1
-1
No files found.
README.md
View file @
dc174281
...
...
@@ -131,6 +131,9 @@ try {
// create Telegram API object
$telegram
=
new
Longman\TelegramBot\Telegram
(
$API_KEY
,
$BOT_NAME
);
// here you can set some command specified parameters, for example, google geocode/timezone api key for date command:
$telegram
->
setCommandConfig
(
'date'
,
array
(
'google_api_key'
=>
'your_google_api_key_here'
));
// handle telegram webhook request
$telegram
->
handle
();
}
catch
(
Longman\TelegramBot\Exception\TelegramException
$e
)
{
...
...
src/Command.php
View file @
dc174281
...
...
@@ -25,9 +25,13 @@ abstract class Command
protected
$enabled
=
true
;
protected
$name
=
''
;
protected
$config
;
public
function
__construct
(
Telegram
$telegram
)
{
$this
->
telegram
=
$telegram
;
$this
->
config
=
$telegram
->
getCommandConfig
(
$this
->
name
);
}
public
function
setUpdate
(
Update
$update
)
...
...
@@ -49,6 +53,17 @@ abstract class Command
return
$this
->
message
;
}
public
function
getConfig
(
$name
=
null
)
{
if
(
isset
(
$this
->
config
[
$name
]))
{
return
$this
->
config
[
$name
];
}
else
{
return
null
;
}
return
$this
->
config
;
}
public
function
getTelegram
()
{
return
$this
->
telegram
;
...
...
src/Commands/DateCommand.php
View file @
dc174281
...
...
@@ -20,19 +20,22 @@ class DateCommand extends Command
protected
$name
=
'date'
;
protected
$description
=
'Show date/time by location'
;
protected
$usage
=
'/date <location>'
;
protected
$version
=
'1.
0
.0'
;
protected
$version
=
'1.
2
.0'
;
protected
$enabled
=
true
;
private
$google_api_key
=
''
;
private
$base_url
=
'https://maps.googleapis.com/maps/api'
;
private
$date_format
=
'd-m-Y H:i:s'
;
private
function
getCoordinates
(
$location
)
{
$url
=
$this
->
base_url
.
'/geocode/json?'
;
$params
=
'address='
.
urlencode
(
$location
);
if
(
!
empty
(
$this
->
google_api_key
))
{
$params
.=
'&key='
.
$this
->
google_api_key
;
$google_api_key
=
$this
->
getConfig
(
'google_api_key'
);
if
(
!
empty
(
$google_api_key
))
{
$params
.=
'&key='
.
$google_api_key
;
}
$data
=
$this
->
request
(
$url
.
$params
);
...
...
@@ -61,11 +64,15 @@ class DateCommand extends Command
{
$url
=
$this
->
base_url
.
'/timezone/json?'
;
$timestamp
=
time
();
$date_utc
=
new
\DateTime
(
null
,
new
\DateTimeZone
(
"UTC"
));
$timestamp
=
$date_utc
->
format
(
'U'
);
$params
=
'location='
.
urlencode
(
$lat
)
.
','
.
urlencode
(
$lng
)
.
'×tamp='
.
urlencode
(
$timestamp
);
if
(
!
empty
(
$this
->
google_api_key
))
{
$params
.=
'&key='
.
$this
->
google_api_key
;
$google_api_key
=
$this
->
getConfig
(
'google_api_key'
);
if
(
!
empty
(
$google_api_key
))
{
$params
.=
'&key='
.
$google_api_key
;
}
$data
=
$this
->
request
(
$url
.
$params
);
...
...
@@ -100,11 +107,9 @@ class DateCommand extends Command
list
(
$local_time
,
$timezone_id
)
=
$this
->
getDate
(
$lat
,
$lng
);
$date_utc
=
new
\DateTime
(
date
(
'Y-m-d H:i:s'
,
$local_time
),
new
\DateTimeZone
(
$timezone_id
));
//$timestamp = $date_utc->format($this->date_format);
$date_utc
=
new
\DateTime
(
gmdate
(
'Y-m-d H:i:s'
,
$local_time
),
new
\DateTimeZone
(
$timezone_id
));
$return
=
'The local time in '
.
$timezone_id
.
' is: '
.
date
(
$this
->
date_format
,
$local_time
)
.
''
;
$return
=
'The local time in '
.
$timezone_id
.
' is: '
.
$date_utc
->
format
(
$this
->
date_format
)
.
''
;
return
$return
;
}
...
...
src/Telegram.php
View file @
dc174281
...
...
@@ -30,7 +30,7 @@ class Telegram
*
* @var string
*/
protected
$version
=
'0.0.
5
'
;
protected
$version
=
'0.0.
7
'
;
/**
* Telegram API key
...
...
@@ -102,6 +102,18 @@ class Telegram
*/
protected
$pdo
;
/**
* Commands config
*
* @var array
*/
protected
$commands_config
;
/**
* Constructor
*
...
...
@@ -364,13 +376,6 @@ class Telegram
$status
=
$sth
->
execute
();
/*$status = $executeQuery->execute(
array(
$update_id, $message_id, $from, $date, $chat, $forward_from,
$forward_date, $reply_to_message, $text,
)
);*/
}
catch
(
PDOException
$e
)
{
throw
new
TelegramException
(
$e
->
getMessage
());
}
...
...
@@ -392,6 +397,29 @@ class Telegram
return
$this
;
}
/**
* Set command config
*
* @return object
*/
public
function
setCommandConfig
(
$command
,
array
$array
)
{
$this
->
commands_config
[
$command
]
=
$array
;
return
$this
;
}
/**
* Get command config
*
* @return object
*/
public
function
getCommandConfig
(
$command
)
{
return
isset
(
$this
->
commands_config
[
$command
])
?
$this
->
commands_config
[
$command
]
:
array
();
}
/**
* Get API KEY
*
...
...
tests/Bootstrap.php
View file @
dc174281
...
...
@@ -25,7 +25,7 @@ $root = realpath(dirname(dirname(__FILE__)));
* Check that --dev composer installation was done
*/
if
(
!
file_exists
(
$root
.
'/vendor/autoload.php'
))
{
throw
new
Exception
(
throw
new
\
Exception
(
'Please run "php composer.phar install --dev" in root directory '
.
'to setup unit test dependencies before running the tests'
);
...
...
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