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
09b0df10
Unverified
Commit
09b0df10
authored
Dec 31, 2016
by
Armando Lüscher
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Make logger more flexible and allow passing of parameters in a sprintf style.
parent
b12b2e99
Changes
6
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
60 additions
and
32 deletions
+60
-32
ShortenerCommand.php
examples/Commands/ShortenerCommand.php
+1
-1
Botan.php
src/Botan.php
+2
-2
Message.php
src/Entities/Message.php
+9
-9
Telegram.php
src/Telegram.php
+2
-2
TelegramLog.php
src/TelegramLog.php
+24
-9
TelegramLogTest.php
tests/unit/TelegramLogTest.php
+22
-9
No files found.
examples/Commands/ShortenerCommand.php
View file @
09b0df10
...
...
@@ -40,7 +40,7 @@ class ShortenerCommand extends UserCommand
$data
=
[];
$data
[
'chat_id'
]
=
$chat_id
;
$text
=
Botan
::
shortenUrl
(
"https://github.com/akalongman/php-telegram-bot"
,
$user_id
);
$text
=
Botan
::
shortenUrl
(
'https://github.com/akalongman/php-telegram-bot'
,
$user_id
);
$data
[
'text'
]
=
$text
;
...
...
src/Botan.php
View file @
09b0df10
...
...
@@ -191,7 +191,7 @@ class Botan
$responseData
=
json_decode
(
$result
,
true
);
if
(
!
$responseData
||
$responseData
[
'status'
]
!==
'accepted'
)
{
TelegramLog
::
debug
(
'Botan.io stats report failed:
'
.
(
$result
?:
'empty response'
)
);
TelegramLog
::
debug
(
'Botan.io stats report failed:
%s'
,
$result
?:
'empty response'
);
return
false
;
}
...
...
@@ -238,7 +238,7 @@ class Botan
}
if
(
filter_var
(
$result
,
FILTER_VALIDATE_URL
)
===
false
)
{
TelegramLog
::
debug
(
'Botan.io URL shortening failed for "
'
.
$url
.
'": '
.
(
$result
?:
'empty response'
)
);
TelegramLog
::
debug
(
'Botan.io URL shortening failed for "
%s": %s'
,
$url
,
$result
?:
'empty response'
);
return
$url
;
}
...
...
src/Entities/Message.php
View file @
09b0df10
...
...
@@ -174,13 +174,13 @@ class Message extends Entity
return
$command
;
}
$
cm
d
=
$this
->
getFullCommand
();
$
full_comman
d
=
$this
->
getFullCommand
();
if
(
strpos
(
$
cm
d
,
'/'
)
===
0
)
{
$
cmd
=
substr
(
$cm
d
,
1
);
if
(
strpos
(
$
full_comman
d
,
'/'
)
===
0
)
{
$
full_command
=
substr
(
$full_comman
d
,
1
);
//check if command is follow by botname
$split_cmd
=
explode
(
'@'
,
$
cm
d
);
$split_cmd
=
explode
(
'@'
,
$
full_comman
d
);
if
(
isset
(
$split_cmd
[
1
]))
{
//command is followed by name check if is addressed to me
if
(
strtolower
(
$split_cmd
[
1
])
===
strtolower
(
$this
->
getBotName
()))
{
...
...
@@ -188,7 +188,7 @@ class Message extends Entity
}
}
else
{
//command is not followed by name
return
$
cm
d
;
return
$
full_comman
d
;
}
}
...
...
@@ -208,10 +208,10 @@ class Message extends Entity
if
(
$without_cmd
&&
$command
=
$this
->
getFullCommand
())
{
if
(
strlen
(
$command
)
+
1
<
strlen
(
$text
))
{
$text
=
substr
(
$text
,
strlen
(
$command
)
+
1
);
}
else
{
$text
=
''
;
return
substr
(
$text
,
strlen
(
$command
)
+
1
);
}
return
''
;
}
return
$text
;
...
...
src/Telegram.php
View file @
09b0df10
...
...
@@ -499,7 +499,7 @@ class Telegram
if
(
is_int
(
$admin_id
)
&&
$admin_id
>
0
&&
!
in_array
(
$admin_id
,
$this
->
admins_list
,
true
))
{
$this
->
admins_list
[]
=
$admin_id
;
}
else
{
TelegramLog
::
error
(
'Invalid value "
'
.
$admin_id
.
'" for admin.'
);
TelegramLog
::
error
(
'Invalid value "
%s" for admin.'
,
$admin_id
);
}
return
$this
;
...
...
@@ -590,7 +590,7 @@ class Telegram
public
function
addCommandsPath
(
$path
,
$before
=
true
)
{
if
(
!
is_dir
(
$path
))
{
TelegramLog
::
error
(
'Commands path "
'
.
$path
.
'" does not exist.'
);
TelegramLog
::
error
(
'Commands path "
%s" does not exist.'
,
$path
);
}
elseif
(
!
in_array
(
$path
,
$this
->
commands_paths
,
true
))
{
if
(
$before
)
{
array_unshift
(
$this
->
commands_paths
,
$path
);
...
...
src/TelegramLog.php
View file @
09b0df10
...
...
@@ -165,12 +165,7 @@ class TelegramLog
{
if
(
is_resource
(
self
::
$debug_log_temp_stream_handle
))
{
rewind
(
self
::
$debug_log_temp_stream_handle
);
self
::
debug
(
sprintf
(
$message
,
stream_get_contents
(
self
::
$debug_log_temp_stream_handle
)
)
);
self
::
debug
(
$message
,
stream_get_contents
(
self
::
$debug_log_temp_stream_handle
));
fclose
(
self
::
$debug_log_temp_stream_handle
);
self
::
$debug_log_temp_stream_handle
=
null
;
}
...
...
@@ -218,7 +213,7 @@ class TelegramLog
*/
public
static
function
isErrorLogActive
()
{
return
(
self
::
$error_log_path
!==
null
)
;
return
self
::
$error_log_path
!==
null
;
}
/**
...
...
@@ -228,7 +223,7 @@ class TelegramLog
*/
public
static
function
isDebugLogActive
()
{
return
(
self
::
$debug_log_path
!==
null
)
;
return
self
::
$debug_log_path
!==
null
;
}
/**
...
...
@@ -238,7 +233,7 @@ class TelegramLog
*/
public
static
function
isUpdateLogActive
()
{
return
(
self
::
$update_log_path
!==
null
)
;
return
self
::
$update_log_path
!==
null
;
}
/**
...
...
@@ -249,6 +244,7 @@ class TelegramLog
public
static
function
error
(
$text
)
{
if
(
self
::
isErrorLogActive
())
{
$text
=
self
::
getLogText
(
$text
,
func_get_args
());
self
::
$monolog
->
error
(
$text
);
}
}
...
...
@@ -261,6 +257,7 @@ class TelegramLog
public
static
function
debug
(
$text
)
{
if
(
self
::
isDebugLogActive
())
{
$text
=
self
::
getLogText
(
$text
,
func_get_args
());
self
::
$monolog
->
debug
(
$text
);
}
}
...
...
@@ -273,7 +270,25 @@ class TelegramLog
public
static
function
update
(
$text
)
{
if
(
self
::
isUpdateLogActive
())
{
$text
=
self
::
getLogText
(
$text
,
func_get_args
());
self
::
$monolog_update
->
info
(
$text
);
}
}
/**
* Applies vsprintf to the text if placeholder replacements are passed along.
*
* @param string $text
* @param array $args
*
* @return string
*/
protected
static
function
getLogText
(
$text
,
array
$args
=
[])
{
// Pop the $text off the array, as it gets passed via func_get_args().
array_shift
(
$args
);
// Suppress warning if placeholders don't match out.
return
@
vsprintf
(
$text
,
$args
)
?:
$text
;
}
}
tests/unit/TelegramLogTest.php
View file @
09b0df10
...
...
@@ -26,7 +26,7 @@ class TelegramLogTest extends TestCase
/**
* @var array Dummy logfile paths
*/
private
$logfiles
=
[
private
static
$logfiles
=
[
'error'
=>
'/tmp/php-telegram-bot-errorlog.log'
,
'debug'
=>
'/tmp/php-telegram-bot-debuglog.log'
,
'update'
=>
'/tmp/php-telegram-bot-updatelog.log'
,
...
...
@@ -48,7 +48,7 @@ class TelegramLogTest extends TestCase
protected
function
tearDown
()
{
// Make sure no logfiles exist.
foreach
(
$this
->
logfiles
as
$file
)
{
foreach
(
self
::
$
logfiles
as
$file
)
{
file_exists
(
$file
)
&&
unlink
(
$file
);
}
}
...
...
@@ -79,37 +79,46 @@ class TelegramLogTest extends TestCase
public
function
testErrorStream
()
{
$file
=
$this
->
logfiles
[
'error'
];
$file
=
self
::
$
logfiles
[
'error'
];
$this
->
assertFileNotExists
(
$file
);
TelegramLog
::
initErrorLog
(
$file
);
TelegramLog
::
error
(
'my error'
);
TelegramLog
::
error
(
'my %s error'
,
'placeholder'
);
$this
->
assertFileExists
(
$file
);
$this
->
assertContains
(
'bot_log.ERROR: my error'
,
file_get_contents
(
$file
));
$error_log
=
file_get_contents
(
$file
);
$this
->
assertContains
(
'bot_log.ERROR: my error'
,
$error_log
);
$this
->
assertContains
(
'bot_log.ERROR: my placeholder error'
,
$error_log
);
}
public
function
testDebugStream
()
{
$file
=
$this
->
logfiles
[
'debug'
];
$file
=
self
::
$
logfiles
[
'debug'
];
$this
->
assertFileNotExists
(
$file
);
TelegramLog
::
initDebugLog
(
$file
);
TelegramLog
::
debug
(
'my debug'
);
TelegramLog
::
debug
(
'my %s debug'
,
'placeholder'
);
$this
->
assertFileExists
(
$file
);
$this
->
assertContains
(
'bot_log.DEBUG: my debug'
,
file_get_contents
(
$file
));
$debug_log
=
file_get_contents
(
$file
);
$this
->
assertContains
(
'bot_log.DEBUG: my debug'
,
$debug_log
);
$this
->
assertContains
(
'bot_log.DEBUG: my placeholder debug'
,
$debug_log
);
}
public
function
testUpdateStream
()
{
$file
=
$this
->
logfiles
[
'update'
];
$file
=
self
::
$
logfiles
[
'update'
];
$this
->
assertFileNotExists
(
$file
);
TelegramLog
::
initUpdateLog
(
$file
);
TelegramLog
::
update
(
'my update'
);
TelegramLog
::
update
(
'my %s update'
,
'placeholder'
);
$this
->
assertFileExists
(
$file
);
$this
->
assertContains
(
'my update'
,
file_get_contents
(
$file
));
$debug_log
=
file_get_contents
(
$file
);
$this
->
assertContains
(
'my update'
,
$debug_log
);
$this
->
assertContains
(
'my placeholder update'
,
$debug_log
);
}
public
function
testExternalStream
()
{
$file
=
$this
->
logfiles
[
'external'
];
$file
=
self
::
$
logfiles
[
'external'
];
$this
->
assertFileNotExists
(
$file
);
$external_monolog
=
new
Logger
(
'bot_update_log'
);
...
...
@@ -118,11 +127,15 @@ class TelegramLogTest extends TestCase
TelegramLog
::
initialize
(
$external_monolog
);
TelegramLog
::
error
(
'my error'
);
TelegramLog
::
error
(
'my %s error'
,
'placeholder'
);
TelegramLog
::
debug
(
'my debug'
);
TelegramLog
::
debug
(
'my %s debug'
,
'placeholder'
);
$this
->
assertFileExists
(
$file
);
$file_contents
=
file_get_contents
(
$file
);
$this
->
assertContains
(
'bot_update_log.ERROR: my error'
,
$file_contents
);
$this
->
assertContains
(
'bot_update_log.ERROR: my placeholder error'
,
$file_contents
);
$this
->
assertContains
(
'bot_update_log.DEBUG: my debug'
,
$file_contents
);
$this
->
assertContains
(
'bot_update_log.DEBUG: my placeholder debug'
,
$file_contents
);
}
}
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