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 {
//$telegram->setUploadPath('../Upload');
// Requests Limiter (tries to prevent reaching Telegram API limits)
// First argument are options
$telegram->enableLimiter();
//$telegram->enableLimiter(['interval' => 0.5);
// Run user selected commands
$telegram->runCommands($commands);
......
......@@ -70,7 +70,9 @@ try {
//$telegram->enableBotan('your_token', ['timeout' => 3]);
// Requests Limiter (tries to prevent reaching Telegram API limits)
// First argument are options
$telegram->enableLimiter();
//$telegram->enableLimiter(['interval' => 0.5);
// Handle telegram getUpdates request
$serverResponse = $telegram->handleGetUpdates();
......
......@@ -69,7 +69,9 @@ try {
//$telegram->enableBotan('your_token', ['timeout' => 3]);
// Requests Limiter (tries to prevent reaching Telegram API limits)
// First argument are options
$telegram->enableLimiter();
//$telegram->enableLimiter(['interval' => 0.5);
// Handle telegram webhook request
$telegram->handle();
......
......@@ -173,29 +173,37 @@ class DB
* Fetch update(s) from DB
*
* @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
* @throws \Longman\TelegramBot\Exception\TelegramException
*/
public static function selectTelegramUpdate($limit = null)
public static function selectTelegramUpdate($limit = null, $id = null)
{
if (!self::isDbConnected()) {
return false;
}
try {
$sql = '
SELECT `id`
FROM `' . TB_TELEGRAM_UPDATE . '`
ORDER BY `id` DESC
';
$sql = 'SELECT `id` FROM `' . TB_TELEGRAM_UPDATE . '`';
if ($id !== null) {
$sql .= ' WHERE `id` = :id';
}
$sql .= ' ORDER BY `id` DESC';
if ($limit !== null) {
$sql .= 'LIMIT :limit';
$sql .= ' LIMIT :limit';
}
$sth = self::$pdo->prepare($sql);
$sth->bindParam(':limit', $limit, PDO::PARAM_INT);
if ($id !== null) {
$sth->bindParam(':id', $id, PDO::PARAM_INT);
}
$sth->execute();
return $sth->fetchAll(PDO::FETCH_ASSOC);
......@@ -474,6 +482,10 @@ class DB
$update_id = $update->getUpdateId();
$update_type = $update->getUpdateType();
if (count(self::selectTelegramUpdate(1, $update->getUpdateId())) === 1) {
throw new TelegramException('Duplicate update received!');
}
if ($update_type === 'message') {
$message = $update->getMessage();
......@@ -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
');
$date = self::getTimestamp(time());
$date = self::getTimestamp();
$date_minute = self::getTimestamp(strtotime('-1 minute'));
$sth->bindParam(':chat_id', $chat_id, \PDO::PARAM_STR);
......
......@@ -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;
}
}
......@@ -1042,15 +1063,15 @@ class Request
$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
&& ((($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))
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)) // No more than 20 messages per minute in groups and channels
) {
break;
}
$timeout--;
sleep(1);
usleep(self::$limiter_interval * 1000000);
}
DB::insertTelegramRequest($action, $data);
......
......@@ -130,6 +130,13 @@ class Telegram
*/
protected $botan_enabled = false;
/**
* Check if runCommands() is running in this session
*
* @var boolean
*/
protected $run_commands = false;
/**
* Telegram constructor.
*
......@@ -875,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;
}
......@@ -896,6 +907,7 @@ class Telegram
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!
$result = Request::getMe()->getResult();
......@@ -924,8 +936,8 @@ class Telegram
],
'date' => time(),
'chat' => [
'id' => $bot_id,
'type' => 'private',
'id' => $bot_id,
'type' => 'private',
],
'text' => $command,
],
......@@ -935,4 +947,14 @@ class Telegram
$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