1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-30 08:18:59 +01:00
This commit is contained in:
Daniil Gentili 2023-08-13 12:26:52 +02:00
parent 5c09a9a20f
commit 48f2551e6e
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7

View File

@ -81,16 +81,15 @@ $settings->getAppInfo()
*/
echo 'Loading MadelineProto...'.PHP_EOL;
$MadelineProto = new API(__DIR__.'/../testing.madeline', $settings);
$MadelineProto->async(true);
$MadelineProto->loop(function () use ($MadelineProto) {
yield $MadelineProto->start();
yield $MadelineProto->fileGetContents('https://google.com');
$MadelineProto->start();
$MadelineProto->fileGetContents('https://google.com');
try {
yield $MadelineProto->getSelf();
$MadelineProto->getSelf();
} catch (\danog\MadelineProto\Exception $e) {
if ($e->getMessage() === 'TOS action required, check the logs') {
yield $MadelineProto->acceptTos();
$MadelineProto->acceptTos();
}
}
@ -116,25 +115,25 @@ $MadelineProto->loop(function () use ($MadelineProto) {
/*
* Try making a phone call
*/
if (!getenv('GITHUB_SHA') && stripos((yield $MadelineProto->readline('Do you want to make a call? (y/n): ')) ?? '', 'y') !== false) {
$controller = yield $MadelineProto->requestCall(getenv('TEST_SECRET_CHAT'))->play('input.raw')->then('input.raw')->playOnHold(['input.raw'])->setOutputFile('output.raw');
if (!getenv('GITHUB_SHA') && stripos(($MadelineProto->readline('Do you want to make a call? (y/n): ')) ?? '', 'y') !== false) {
$controller = $MadelineProto->requestCall(getenv('TEST_SECRET_CHAT'))->play('input.raw')->then('input.raw')->playOnHold(['input.raw'])->setOutputFile('output.raw');
while ($controller->getCallState() < VoIP::CALL_STATE_READY) {
yield $MadelineProto->sleep(1);
$MadelineProto->sleep(1);
}
$MadelineProto->logger($controller->configuration);
while ($controller->getCallState() < VoIP::CALL_STATE_ENDED) {
yield $MadelineProto->sleep(1);
$MadelineProto->sleep(1);
}
}
/*
* Try receiving a phone call
*/
if (!getenv('GITHUB_SHA') && stripos((yield $MadelineProto->readline('Do you want to handle incoming calls? (y/n): ')) ?? '', 'y') !== false) {
$howmany = yield $MadelineProto->readline('How many calls would you like me to handle? ');
if (!getenv('GITHUB_SHA') && stripos(($MadelineProto->readline('Do you want to handle incoming calls? (y/n): ')) ?? '', 'y') !== false) {
$howmany = $MadelineProto->readline('How many calls would you like me to handle? ');
$offset = 0;
while ($howmany > 0) {
$updates = yield $MadelineProto->getUpdates(['offset' => $offset, 'limit' => 50, 'timeout' => 0]); // Just like in the bot API, you can specify an offset, a limit and a timeout
$updates = $MadelineProto->getUpdates(['offset' => $offset, 'limit' => 50, 'timeout' => 0]); // Just like in the bot API, you can specify an offset, a limit and a timeout
foreach ($updates as $update) {
$MadelineProto->logger($update);
$offset = $update['update_id'] + 1; // Just like in the bot API, the offset must be set to the last update_id
@ -152,22 +151,22 @@ $MadelineProto->loop(function () use ($MadelineProto) {
/*
* Secret chat usage
*/
if (!getenv('GITHUB_SHA') && stripos((yield $MadelineProto->readline('Do you want to make the secret chat tests? (y/n): ')) ?? '', 'y') !== false) {
if (!getenv('GITHUB_SHA') && stripos(($MadelineProto->readline('Do you want to make the secret chat tests? (y/n): ')) ?? '', 'y') !== false) {
if (!getenv('TEST_SECRET_CHAT')) {
throw new Exception('No TEST_SECRET_CHAT environment variable was provided!');
}
/**
* Request a secret chat.
*/
$secret_chat_id = yield $MadelineProto->requestSecretChat(getenv('TEST_SECRET_CHAT'));
$secret_chat_id = $MadelineProto->requestSecretChat(getenv('TEST_SECRET_CHAT'));
echo 'Waiting 10 seconds for '.getenv('TEST_SECRET_CHAT').' (secret chat id '.$secret_chat_id.') to accept the secret chat...'.PHP_EOL;
yield $MadelineProto->sleep(10);
$MadelineProto->sleep(10);
/**
* Send a markdown-formatted text message with expiration after 10 seconds.
*/
$sentMessage = yield $MadelineProto->messages->sendEncrypted([
$sentMessage = $MadelineProto->messages->sendEncrypted([
'peer' => $secret_chat_id,
'message' => [
'_' => 'decryptedMessage',
@ -247,16 +246,16 @@ $MadelineProto->loop(function () use ($MadelineProto) {
foreach ($secret_media as $type => $smessage) {
$MadelineProto->logger("Encrypting and uploading $type...");
$type = yield $MadelineProto->messages->sendEncryptedFile($smessage);
$type = $MadelineProto->messages->sendEncryptedFile($smessage);
}
}
if (!getenv('TEST_USERNAME')) {
throw new Exception('No TEST_USERNAME environment variable was provided!');
}
/*yield $MadelineProto->refreshPeerCache(\getenv('TEST_USERNAME'));
yield $MadelineProto->refreshFullPeerCache(\getenv('TEST_USERNAME'));*/
$mention = yield $MadelineProto->getInfo(getenv('TEST_USERNAME')); // Returns an array with all of the constructors that can be extracted from a username or an id
/*$MadelineProto->refreshPeerCache(\getenv('TEST_USERNAME'));
$MadelineProto->refreshFullPeerCache(\getenv('TEST_USERNAME'));*/
$mention = $MadelineProto->getInfo(getenv('TEST_USERNAME')); // Returns an array with all of the constructors that can be extracted from a username or an id
$mention = $mention['user_id']; // Selects only the numeric user id
$media = [];
@ -285,7 +284,7 @@ $MadelineProto->loop(function () use ($MadelineProto) {
$media['document'] = ['_' => 'inputMediaDocumentExternal', 'url' => 'https://github.com/danog/MadelineProto/raw/v8/tests/60'];
$message = 'yay '.PHP_VERSION_ID;
$mention = yield $MadelineProto->getInfo(getenv('TEST_USERNAME')); // Returns an array with all of the constructors that can be extracted from a username or an id
$mention = $MadelineProto->getInfo(getenv('TEST_USERNAME')); // Returns an array with all of the constructors that can be extracted from a username or an id
$mention = $mention['user_id']; // Selects only the numeric user id
$peers = json_decode(getenv('TEST_DESTINATION_GROUPS'), true);
@ -293,16 +292,16 @@ $MadelineProto->loop(function () use ($MadelineProto) {
die("No TEST_DESTINATION_GROUPS array was provided!");
}
foreach ($peers as $peer) {
$sentMessage = yield $MadelineProto->messages->sendMessage(['peer' => $peer, 'message' => $message, 'entities' => [['_' => 'inputMessageEntityMentionName', 'offset' => 0, 'length' => mb_strlen($message), 'user_id' => $mention]]]);
$sentMessage = $MadelineProto->messages->sendMessage(['peer' => $peer, 'message' => $message, 'entities' => [['_' => 'inputMessageEntityMentionName', 'offset' => 0, 'length' => mb_strlen($message), 'user_id' => $mention]]]);
$MadelineProto->logger($sentMessage, Logger::NOTICE);
$sentMessage = yield $MadelineProto->messages->sendMessage(['peer' => $peer, 'message' => str_repeat('a', 4096*4)]);
$sentMessage = $MadelineProto->messages->sendMessage(['peer' => $peer, 'message' => str_repeat('a', 4096*4)]);
$MadelineProto->logger($sentMessage, \danog\MadelineProto\Logger::NOTICE);
foreach ($media as $type => $inputMedia) {
if ($type !== 'sticker' && $type !== 'voice') {
$MadelineProto->logger("Sending multi $type");
yield $MadelineProto->messages->sendMultiMedia(['peer' => $peer, 'multi_media' => [
$MadelineProto->messages->sendMultiMedia(['peer' => $peer, 'multi_media' => [
['_' => 'inputSingleMedia', 'media' => $inputMedia, 'message' => '['.$message.'](mention:'.$mention.')', 'parse_mode' => 'markdown'],
['_' => 'inputSingleMedia', 'media' => $inputMedia, 'message' => '['.$message.'](mention:'.$mention.')', 'parse_mode' => 'markdown'],
]]);
@ -311,19 +310,19 @@ $MadelineProto->loop(function () use ($MadelineProto) {
? read($inputMedia['file'])
: $MadelineProto->fileGetContents($inputMedia['url']);
$MadelineProto->logger("Sending $type");
$dl = $MadelineProto->extractMessage(yield $MadelineProto->messages->sendMedia(['peer' => $peer, 'media' => $inputMedia, 'message' => '['.$message.'](mention:'.$mention.')', 'parse_mode' => 'markdown']));
$dl = $MadelineProto->extractMessage($MadelineProto->messages->sendMedia(['peer' => $peer, 'media' => $inputMedia, 'message' => '['.$message.'](mention:'.$mention.')', 'parse_mode' => 'markdown']));
$MadelineProto->logger("Downloading $type");
$file = yield $MadelineProto->downloadToDir($dl, '/tmp');
$file = $MadelineProto->downloadToDir($dl, '/tmp');
if ($type !== 'photo') {
Assert::eq(read($file), $fileOrig, "Not equal!");
}
$MadelineProto->logger("Uploading $type");
$media = yield $MadelineProto->messages->uploadMedia(['peer' => '@me', 'media' => $inputMedia]);
$media = $MadelineProto->messages->uploadMedia(['peer' => '@me', 'media' => $inputMedia]);
$MadelineProto->logger("Downloading $type");
$file = yield $MadelineProto->downloadToDir($media, '/tmp');
$file = $MadelineProto->downloadToDir($media, '/tmp');
if ($type !== 'photo') {
Assert::eq(read($file), $fileOrig, "Not equal!");
}
@ -331,10 +330,10 @@ $MadelineProto->loop(function () use ($MadelineProto) {
$MadelineProto->logger("Re-sending $type");
$inputMedia['file'] = $media;
$dl = yield $MadelineProto->messages->uploadMedia(['peer' => '@me', 'media' => $inputMedia]);
$dl = $MadelineProto->messages->uploadMedia(['peer' => '@me', 'media' => $inputMedia]);
$MadelineProto->logger("Re-downloading $type");
$file = yield $MadelineProto->downloadToDir($dl, '/tmp');
$file = $MadelineProto->downloadToDir($dl, '/tmp');
if ($type !== 'photo') {
Assert::eq(read($file), $fileOrig, "Not equal!");
}
@ -342,7 +341,7 @@ $MadelineProto->loop(function () use ($MadelineProto) {
}
foreach (json_decode(getenv('TEST_DESTINATION_GROUPS'), true) as $peer) {
$sentMessage = yield $MadelineProto->messages->sendMessage(['peer' => $peer, 'message' => $message, 'entities' => [['_' => 'inputMessageEntityMentionName', 'offset' => 0, 'length' => mb_strlen($message), 'user_id' => $mention]]]);
$sentMessage = $MadelineProto->messages->sendMessage(['peer' => $peer, 'message' => $message, 'entities' => [['_' => 'inputMessageEntityMentionName', 'offset' => 0, 'length' => mb_strlen($message), 'user_id' => $mention]]]);
$MadelineProto->logger($sentMessage, Logger::NOTICE);
}
});