Improvements for examples

This commit is contained in:
Daniil Gentili 2020-10-18 15:41:06 +02:00
parent 906a0ab888
commit ad85b0a6c0
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
6 changed files with 53 additions and 62 deletions

View File

@ -21,9 +21,7 @@
use danog\MadelineProto\API;
use danog\MadelineProto\EventHandler;
use danog\MadelineProto\Exception;
use danog\MadelineProto\Logger;
use danog\MadelineProto\RPCErrorException;
/*
* Various ways to load MadelineProto
@ -80,18 +78,10 @@ class MyEventHandler extends EventHandler
}
$res = \json_encode($update, JSON_PRETTY_PRINT);
try {
yield $this->messages->sendMessage(['peer' => $update, 'message' => "<code>$res</code>", 'reply_to_msg_id' => isset($update['message']['id']) ? $update['message']['id'] : null, 'parse_mode' => 'HTML']);
if (isset($update['message']['media']) && $update['message']['media']['_'] !== 'messageMediaGame') {
yield $this->messages->sendMedia(['peer' => $update, 'message' => $update['message']['message'], 'media' => $update]);
}
} catch (RPCErrorException $e) {
$this->report("Surfaced: $e");
} catch (Exception $e) {
if (\stripos($e->getMessage(), 'invalid constructor given') === false) {
$this->report("Surfaced: $e");
}
}
}
}

View File

@ -78,7 +78,6 @@ class SecretHandler extends \danog\MadelineProto\EventHandler
*/
public function onUpdateNewEncryptedMessage(array $update): \Generator
{
try {
if (isset($update['message']['decrypted_message']['media'])) {
$this->logger(yield $this->downloadToDir($update, '.'));
}
@ -122,11 +121,6 @@ class SecretHandler extends \danog\MadelineProto\EventHandler
yield $this->messages->sendMessage(['peer' => $update, 'message' => (string) ($i++)]);
}
$this->sent[$update['message']['chat_id']] = true;
} catch (\danog\MadelineProto\RPCErrorException $e) {
\danog\MadelineProto\Logger::log($e);
} catch (\danog\MadelineProto\Exception $e) {
\danog\MadelineProto\Logger::log($e);
}
}
}

View File

@ -37,6 +37,9 @@ abstract class EventHandler extends InternalDoc
* API instance.
*/
protected MTProto $API;
public function __construct($API) // BC
{
}
/**
* Internal constructor.
*

View File

@ -118,6 +118,7 @@ class WriteLoop extends ResumableSignalLoop
$API->logger->logger("Sent $message as unencrypted message to DC $datacenter!", \danog\MadelineProto\Logger::ULTRA_VERBOSE);
unset($connection->pendingOutgoing[$k]);
$message->setMsgId($message_id);
$connection->outgoing_messages[$message_id] = $message;
$connection->new_outgoing[$message_id] = $message;

View File

@ -20,6 +20,7 @@
namespace danog\MadelineProto\MTProtoSession;
use danog\MadelineProto\DataCenterConnection;
use danog\MadelineProto\Logger;
use danog\MadelineProto\MTProto\IncomingMessage;
use danog\MadelineProto\MTProto\OutgoingMessage;
@ -58,6 +59,8 @@ trait AckHandler
// The server acknowledges that it received my message
if (isset($this->new_outgoing[$outgoingMessage->getMsgId()])) {
unset($this->new_outgoing[$outgoingMessage->getMsgId()]);
} else {
$this->logger->logger("Could not find $outgoingMessage in new_outgoing!", Logger::FATAL_ERROR);
}
}
/**

View File

@ -179,7 +179,7 @@ trait ResponseHandler
/** @var OutgoingMessage */
$request = $this->outgoing_messages[$requestId];
if ($request->getState() & OutgoingMessage::STATE_REPLIED) {
$this->logger->logger("Already got a reponse to $request, but there is another reply $message with message ID $requestId!", Logger::FATAL_ERROR);
$this->logger->logger("Already got a response to $request, but there is another reply $message with message ID $requestId!", Logger::FATAL_ERROR);
return;
}
if ($response['_'] === 'rpc_result') {