Introduction
Hello! If you’ve ever tried to extract user data from large telegram groups and channels, you’re probably aware of the technical difficulties that can arise. Within our project — platforms for speakers and coaches — we are faced with the need to effectively promote our courses using advertising in other groups and channels. However, not everyone is ready to provide access to their channel or group, especially if the proposed course topics overlap with their own.
To solve this problem, our marketing team has developed a three-level affiliate program. The essence of the program is that group owners who agree to cooperate receive a unique identifier in our system. All new users are registered under this ID, which, if courses are purchased, generates a reward for the group owner.
This initiative seemed promising to us, but during the implementation process we encountered a serious limitation. When trying to download a list of users from a group of 13,000 people, the system could only process 10,000. This limitation of the Telegram API significantly complicated the implementation of the intended plan, requiring additional technical solutions from us.
Solution
In the API Telegram channels->getParticipants there was a search parameter ‘filter’ => [‘_’ => ‘channelParticipantsSearch’, ‘q’ => “Перебираем все символы которые могут быть в логине”]
An example solution in PHP using the MadelineProto library
<?
//.....................................
//подключаем библиотеки, считываем данные из базы
$telegram_group_id = $group['telegram_group_id'];
$groupInfo = $MadelineProto->getFullInfo($telegram_group_id);
$participantCount = $groupInfo['full']['participants_count'] ?? 0;
echo "В группе $participantCount пользователей!<br>";
$allParticipants = [];
$limit = 200;
if ($participantCount > 10000) {
$nextchar = $group['nextchar'];
if ($nextchar === '00') {
echo "Все символы были обработаны.<br>";
echo '</body>';
echo '</html>';
exit;
}
if ($nextchar === '' || is_null($nextchar)) {
$nextchar="a";
}
$alphabet = array_merge(range('a', 'z'), range('0', '9')); //можно добавить еще символов
$currentCharIndex = array_search($nextchar, $alphabet);
if ($currentCharIndex === false) {
die("Неверное значение nextchar.");
}
$currentChar = $alphabet[$currentCharIndex];
$offset = 0;
do {
$response = $MadelineProto->channels->getParticipants([
'channel' => $telegram_group_id,
'filter' => ['_' => 'channelParticipantsSearch', 'q' => $currentChar],
'offset' => $offset,
'limit' => $limit
]);
foreach ($response['participants'] as $participant) {
if (!isset($participant['user_id'])) {
continue; // Пропускаем текущую итерацию, если ключа нет
}
$allParticipants[$participant['user_id']] = $participant;
}
$offset += $limit;
} while (count($response['participants']) == $limit);
$nextCharIndex = $currentCharIndex + 1;
if ($nextCharIndex >= count($alphabet)) {
$updateNextChar="00"; //прошли все символы
} else {
$updateNextChar = $alphabet[$nextCharIndex];
}
// Обновление следующего символа в базе данных
$updateQuery = "UPDATE telegram_group SET nextchar = :nextchar WHERE id = :id_telegram_group";
$updateStmt = $currentDb->prepare($updateQuery);
$updateStmt->bindParam(':nextchar', $updateNextChar);
$updateStmt->bindParam(':id_telegram_group', $id_telegram_group);
$updateStmt->execute();
$linkToAdd = strtolower("https://Формируем ссылку для запуска cron на этот скрипт");
//типа INSERT INTO crons (links, active, minuts) VALUES (:link, 1, 5)
//убираем задачу если прошли все варианты
//if ($updateNextChar === '00') {
// Если обработка символов завершена, устанавливаем статус активна в 0 для этой ссылки
// $query = "
// UPDATE crons SET active = 0 WHERE links = :link
// ";
// $statement = $db->prepare($query);
// $statement->bindParam(':link', $linkToAdd);
// $statement->execute();
// }
?>
in order not to create a large load on the project, the script is launched once every 5 minutes, the code has successfully passed the test and is used in the project
Conclusion
I hope that the presented solution to the problem with the Telegram API limiting the processing of data from more than 10,000 users will be useful to you. This task, although it may seem difficult at first glance, is solved quite simply.
Acknowledgement and Usage Notice
The editorial team at TechBurst Magazine acknowledges the invaluable contribution of the author of the original article that forms the foundation of our publication. We sincerely appreciate the author’s work. All images in this publication are sourced directly from the original article, where a reference to the author’s profile is provided as well. This publication respects the author’s rights and enhances the visibility of their original work. If there are any concerns or the author wishes to discuss this matter further, we welcome an open dialogue to address potential issues and find an amicable resolution. Feel free to contact us through the ‘Contact Us’ section; the link is available in the website footer.