Commit 9afb163d authored by Armando Lüscher's avatar Armando Lüscher Committed by GitHub

Merge pull request #466 from jacklul/additions

Another small additions and changes
parents 6c3e463e 46758594
...@@ -64,7 +64,9 @@ try { ...@@ -64,7 +64,9 @@ try {
//$telegram->setUploadPath('../Upload'); //$telegram->setUploadPath('../Upload');
// Requests Limiter (tries to prevent reaching Telegram API limits) // Requests Limiter (tries to prevent reaching Telegram API limits)
// First argument are options
$telegram->enableLimiter(); $telegram->enableLimiter();
//$telegram->enableLimiter(['interval' => 0.5);
// Run user selected commands // Run user selected commands
$telegram->runCommands($commands); $telegram->runCommands($commands);
......
...@@ -70,7 +70,9 @@ try { ...@@ -70,7 +70,9 @@ try {
//$telegram->enableBotan('your_token', ['timeout' => 3]); //$telegram->enableBotan('your_token', ['timeout' => 3]);
// Requests Limiter (tries to prevent reaching Telegram API limits) // Requests Limiter (tries to prevent reaching Telegram API limits)
// First argument are options
$telegram->enableLimiter(); $telegram->enableLimiter();
//$telegram->enableLimiter(['interval' => 0.5);
// Handle telegram getUpdates request // Handle telegram getUpdates request
$serverResponse = $telegram->handleGetUpdates(); $serverResponse = $telegram->handleGetUpdates();
......
...@@ -69,7 +69,9 @@ try { ...@@ -69,7 +69,9 @@ try {
//$telegram->enableBotan('your_token', ['timeout' => 3]); //$telegram->enableBotan('your_token', ['timeout' => 3]);
// Requests Limiter (tries to prevent reaching Telegram API limits) // Requests Limiter (tries to prevent reaching Telegram API limits)
// First argument are options
$telegram->enableLimiter(); $telegram->enableLimiter();
//$telegram->enableLimiter(['interval' => 0.5);
// Handle telegram webhook request // Handle telegram webhook request
$telegram->handle(); $telegram->handle();
......
...@@ -173,29 +173,37 @@ class DB ...@@ -173,29 +173,37 @@ class DB
* Fetch update(s) from DB * Fetch update(s) from DB
* *
* @param int $limit Limit the number of updates to fetch * @param int $limit Limit the number of updates to fetch
* @param int $id Check for unique update id
* *
* @return array|bool Fetched data or false if not connected * @return array|bool Fetched data or false if not connected
* @throws \Longman\TelegramBot\Exception\TelegramException * @throws \Longman\TelegramBot\Exception\TelegramException
*/ */
public static function selectTelegramUpdate($limit = null) public static function selectTelegramUpdate($limit = null, $id = null)
{ {
if (!self::isDbConnected()) { if (!self::isDbConnected()) {
return false; return false;
} }
try { try {
$sql = ' $sql = 'SELECT `id` FROM `' . TB_TELEGRAM_UPDATE . '`';
SELECT `id`
FROM `' . TB_TELEGRAM_UPDATE . '` if ($id !== null) {
ORDER BY `id` DESC $sql .= ' WHERE `id` = :id';
'; }
$sql .= ' ORDER BY `id` DESC';
if ($limit !== null) { if ($limit !== null) {
$sql .= 'LIMIT :limit'; $sql .= ' LIMIT :limit';
} }
$sth = self::$pdo->prepare($sql); $sth = self::$pdo->prepare($sql);
$sth->bindParam(':limit', $limit, PDO::PARAM_INT); $sth->bindParam(':limit', $limit, PDO::PARAM_INT);
if ($id !== null) {
$sth->bindParam(':id', $id, PDO::PARAM_INT);
}
$sth->execute(); $sth->execute();
return $sth->fetchAll(PDO::FETCH_ASSOC); return $sth->fetchAll(PDO::FETCH_ASSOC);
...@@ -474,6 +482,10 @@ class DB ...@@ -474,6 +482,10 @@ class DB
$update_id = $update->getUpdateId(); $update_id = $update->getUpdateId();
$update_type = $update->getUpdateType(); $update_type = $update->getUpdateType();
if (count(self::selectTelegramUpdate(1, $update->getUpdateId())) === 1) {
throw new TelegramException('Duplicate update received!');
}
if ($update_type === 'message') { if ($update_type === 'message') {
$message = $update->getMessage(); $message = $update->getMessage();
...@@ -1089,7 +1101,7 @@ class DB ...@@ -1089,7 +1101,7 @@ class DB
(SELECT COUNT(*) FROM `' . TB_REQUEST_LIMITER . '` WHERE `chat_id` = :chat_id AND `created_at` >= :date_minute) as LIMIT_PER_MINUTE (SELECT COUNT(*) FROM `' . TB_REQUEST_LIMITER . '` WHERE `chat_id` = :chat_id AND `created_at` >= :date_minute) as LIMIT_PER_MINUTE
'); ');
$date = self::getTimestamp(time()); $date = self::getTimestamp();
$date_minute = self::getTimestamp(strtotime('-1 minute')); $date_minute = self::getTimestamp(strtotime('-1 minute'));
$sth->bindParam(':chat_id', $chat_id, \PDO::PARAM_STR); $sth->bindParam(':chat_id', $chat_id, \PDO::PARAM_STR);
......
...@@ -53,6 +53,13 @@ class Request ...@@ -53,6 +53,13 @@ class Request
*/ */
private static $limiter_enabled; private static $limiter_enabled;
/**
* Request limiter's interval between checks
*
* @var boolean
*/
private static $limiter_interval;
/** /**
* Available actions to send * Available actions to send
* *
...@@ -990,10 +997,24 @@ class Request ...@@ -990,10 +997,24 @@ class Request
* Enable request limiter * Enable request limiter
* *
* @param boolean $value * @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()) { 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; self::$limiter_enabled = $value;
} }
} }
...@@ -1042,15 +1063,15 @@ class Request ...@@ -1042,15 +1063,15 @@ class Request
$requests = DB::getTelegramRequestCount($chat_id, $inline_message_id); $requests = DB::getTelegramRequestCount($chat_id, $inline_message_id);
if ($requests['LIMIT_PER_SEC'] == 0 // No more than one message per second inside a particular chat 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 || $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; break;
} }
$timeout--; $timeout--;
sleep(1); usleep(self::$limiter_interval * 1000000);
} }
DB::insertTelegramRequest($action, $data); DB::insertTelegramRequest($action, $data);
......
...@@ -130,6 +130,13 @@ class Telegram ...@@ -130,6 +130,13 @@ class Telegram
*/ */
protected $botan_enabled = false; protected $botan_enabled = false;
/**
* Check if runCommands() is running in this session
*
* @var boolean
*/
protected $run_commands = false;
/** /**
* Telegram constructor. * Telegram constructor.
* *
...@@ -875,10 +882,14 @@ class Telegram ...@@ -875,10 +882,14 @@ class Telegram
/** /**
* Enable requests limiter * 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; return $this;
} }
...@@ -896,6 +907,7 @@ class Telegram ...@@ -896,6 +907,7 @@ class Telegram
throw new TelegramException('No command(s) provided!'); throw new TelegramException('No command(s) provided!');
} }
$this->run_commands = true;
$this->botan_enabled = false; // Force disable Botan.io integration, we don't want to track self-executed commands! $this->botan_enabled = false; // Force disable Botan.io integration, we don't want to track self-executed commands!
$result = Request::getMe()->getResult(); $result = Request::getMe()->getResult();
...@@ -924,8 +936,8 @@ class Telegram ...@@ -924,8 +936,8 @@ class Telegram
], ],
'date' => time(), 'date' => time(),
'chat' => [ 'chat' => [
'id' => $bot_id, 'id' => $bot_id,
'type' => 'private', 'type' => 'private',
], ],
'text' => $command, 'text' => $command,
], ],
...@@ -935,4 +947,14 @@ class Telegram ...@@ -935,4 +947,14 @@ class Telegram
$this->executeCommand($this->update->getMessage()->getCommand()); $this->executeCommand($this->update->getMessage()->getCommand());
} }
} }
/**
* Is this session initiated by runCommands()
*
* @return bool
*/
public function isRunCommands()
{
return $this->run_commands;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment