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
25c689b4
Commit
25c689b4
authored
Jul 09, 2015
by
LONGMAN
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added command related settings. Fixed bug in date command
parent
165d88f6
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
66 additions
and
18 deletions
+66
-18
README.md
README.md
+3
-0
Command.php
src/Command.php
+15
-0
DateCommand.php
src/Commands/DateCommand.php
+13
-11
Telegram.php
src/Telegram.php
+35
-7
No files found.
README.md
View file @
25c689b4
...
...
@@ -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 @
25c689b4
...
...
@@ -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 @
25c689b4
...
...
@@ -20,19 +20,20 @@ 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.
1
.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
;
if
(
!
empty
(
$
google_api_key
=
$this
->
getConfig
(
'google_api_key'
)
))
{
$params
.=
'&key='
.
$google_api_key
;
}
$data
=
$this
->
request
(
$url
.
$params
);
...
...
@@ -61,11 +62,14 @@ 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
;
if
(
!
empty
(
$
google_api_key
=
$this
->
getConfig
(
'google_api_key'
)
))
{
$params
.=
'&key='
.
$google_api_key
;
}
$data
=
$this
->
request
(
$url
.
$params
);
...
...
@@ -100,11 +104,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 @
25c689b4
...
...
@@ -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
*
...
...
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