diff --git a/examples/botAllMessages.php b/examples/botAllMessages.php new file mode 100644 index 000000000..c4d02cb3a --- /dev/null +++ b/examples/botAllMessages.php @@ -0,0 +1,67 @@ +#!/usr/bin/env php +. + * + * @author Alexander Panlratov + * @copyright 2016-2020 Daniil Gentili + * @license https://opensource.org/licenses/AGPL-3.0 AGPLv3 + * + * @link https://docs.madelineproto.xyz MadelineProto documentation + */ + +/* + * Various ways to load MadelineProto + */ +if (\file_exists('vendor/autoload.php')) { + include 'vendor/autoload.php'; +} else { + if (!\file_exists('madeline.php')) { + \copy('https://phar.madelineproto.xyz/madeline.php', 'madeline.php'); + } + /** + * @psalm-suppress MissingFile + */ + include 'madeline.php'; +} + +$MadelineProto = new \danog\MadelineProto\API('bot.madeline'); + +$MadelineProto->async(true); +$MadelineProto->loop(static function() use($MadelineProto) { + yield $MadelineProto->start(); + + $lastMessageId = 0; + $threads = 5; + $step = 20; + $totalMessages = 0; + $start = microtime(true); + while(true) { + $promises = []; + for ($i = 0; $i < $threads; $i++) { + $promises[] = $MadelineProto->messages->getMessages(['id' => range($lastMessageId+1, $lastMessageId+$step)]); + $lastMessageId +=$step; + } + $results = yield \Amp\Promise\all($promises); + foreach ($results as $result) { + foreach ($result['messages'] as $message) { + if ($message['_'] === 'messageEmpty') { + break 3; + } + $totalMessages++; + yield $MadelineProto->echo("\rTotal messages processed: {$totalMessages}"); + } + } + }; + $time = microtime(true)-$start; + yield $MadelineProto->echo("\nTime: {$time}\n"); +});