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
e9e3588d
Commit
e9e3588d
authored
Apr 15, 2017
by
Jack'lul
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add optional parameter 'interval' to modify check interval for request limiter
parent
5cb0eba0
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
38 additions
and
7 deletions
+38
-7
cron.php
examples/cron.php
+2
-0
getUpdatesCLI.php
examples/getUpdatesCLI.php
+2
-0
hook.php
examples/hook.php
+2
-0
Request.php
src/Request.php
+26
-5
Telegram.php
src/Telegram.php
+6
-2
No files found.
examples/cron.php
View file @
e9e3588d
...
...
@@ -64,7 +64,9 @@ try {
//$telegram->setUploadPath('../Upload');
// Requests Limiter (tries to prevent reaching Telegram API limits)
// Second argument are options
$telegram
->
enableLimiter
();
//$telegram->enableLimiter(['interval' => 0.5);
// Run user selected commands
$telegram
->
runCommands
(
$commands
);
...
...
examples/getUpdatesCLI.php
View file @
e9e3588d
...
...
@@ -70,7 +70,9 @@ try {
//$telegram->enableBotan('your_token', ['timeout' => 3]);
// Requests Limiter (tries to prevent reaching Telegram API limits)
// Second argument are options
$telegram
->
enableLimiter
();
//$telegram->enableLimiter(['interval' => 0.5);
// Handle telegram getUpdates request
$serverResponse
=
$telegram
->
handleGetUpdates
();
...
...
examples/hook.php
View file @
e9e3588d
...
...
@@ -69,7 +69,9 @@ try {
//$telegram->enableBotan('your_token', ['timeout' => 3]);
// Requests Limiter (tries to prevent reaching Telegram API limits)
// Second argument are options
$telegram
->
enableLimiter
();
//$telegram->enableLimiter(['interval' => 0.5);
// Handle telegram webhook request
$telegram
->
handle
();
...
...
src/Request.php
View file @
e9e3588d
...
...
@@ -53,6 +53,13 @@ class Request
*/
private
static
$limiter_enabled
;
/**
* Request limiter's interval between checks
*
* @var boolean
*/
private
static
$limiter_interval
;
/**
* Available actions to send
*
...
...
@@ -990,10 +997,24 @@ class Request
* Enable request limiter
*
* @param boolean $value
* @param array $options
*
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public
static
function
setLimiter
(
$value
=
true
)
public
static
function
setLimiter
(
$value
=
true
,
array
$options
=
[]
)
{
if
(
DB
::
isDbConnected
())
{
$options_default
=
[
'interval'
=>
1
,
];
$options
=
array_merge
(
$options_default
,
$options
);
if
(
!
is_numeric
(
$options
[
'interval'
])
||
$options
[
'interval'
]
<=
0
)
{
throw
new
TelegramException
(
'Interval must be a number and must be greater than zero!'
);
}
self
::
$limiter_interval
=
$options
[
'interval'
];
self
::
$limiter_enabled
=
$value
;
}
}
...
...
@@ -1044,13 +1065,13 @@ class Request
if
(
$requests
[
'LIMIT_PER_SEC'
]
==
0
// No more than one message per second inside a particular chat
&&
(((
$chat_id
>
0
||
$inline_message_id
)
&&
$requests
[
'LIMIT_PER_SEC_ALL'
]
<
30
)
// No more than 30 messages per second globally
||
(
$chat_id
<
0
&&
$requests
[
'LIMIT_PER_MINUTE'
]
<
20
))
||
(
$chat_id
<
0
&&
$requests
[
'LIMIT_PER_MINUTE'
]
<
20
))
// No more than 20 messages per minute in groups and channels
)
{
break
;
}
$timeout
--
;
sleep
(
1
);
usleep
(
self
::
$limiter_interval
*
1000000
);
}
DB
::
insertTelegramRequest
(
$action
,
$data
);
...
...
src/Telegram.php
View file @
e9e3588d
...
...
@@ -882,10 +882,14 @@ class Telegram
/**
* Enable requests limiter
*
* @param array $options
*
* @return \Longman\TelegramBot\Telegram
*/
public
function
enableLimiter
()
public
function
enableLimiter
(
array
$options
=
[]
)
{
Request
::
setLimiter
(
true
);
Request
::
setLimiter
(
true
,
$options
);
return
$this
;
}
...
...
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