Improve bot

This commit is contained in:
Daniil Gentili 2019-12-30 17:27:28 +00:00
parent a3150540d7
commit 5b5c00b5a1
2 changed files with 28 additions and 15 deletions

View File

@ -54,7 +54,7 @@ class EventHandler extends \danog\MadelineProto\EventHandler
private $states = []; private $states = [];
public function onUpdateNewChannelMessage($update) public function onUpdateNewChannelMessage($update)
{ {
yield $this->onUpdateNewMessage($update); //yield $this->onUpdateNewMessage($update);
} }
public function report($message) public function report($message)
{ {
@ -79,17 +79,17 @@ class EventHandler extends \danog\MadelineProto\EventHandler
$messageId = $update['message']['id']; $messageId = $update['message']['id'];
if ($update['message']['message'] === '/start') { if ($update['message']['message'] === '/start') {
return $this->messages->sendMessage(['peer' => $peerId, 'message' => self::START, 'parse_mode' => 'Markdown', 'reply_to_msg_id' => $update['message']['id']]); return $this->messages->sendMessage(['peer' => $peerId, 'message' => self::START, 'parse_mode' => 'Markdown', 'reply_to_msg_id' => $messageId]);
} }
if (isset($update['message']['media']['_']) && $update['message']['media']['_'] !== 'messageMediaWebPage') { if (isset($update['message']['media']['_']) && $update['message']['media']['_'] !== 'messageMediaWebPage') {
$id = yield $this->messages->sendMessage(['peer' => $peerId, 'message' => 'Give me a new name for this file: ', 'reply_to_msg_id' => $messageId])['id']; yield $this->messages->sendMessage(['peer' => $peerId, 'message' => 'Give me a new name for this file: ', 'reply_to_msg_id' => $messageId]);
$this->states[$peerId] = [$update['message']['media'], $id]; $this->states[$peerId] = $update['message']['media'];
return; return;
} }
if (isset($this->states[$peerId])) { if (isset($this->states[$peerId])) {
$name = $update['message']['message']; $name = $update['message']['message'];
list($url, $id) = $this->states[$peerId]; $url = $this->states[$peerId];
unset($this->states[$peerId]); unset($this->states[$peerId]);
} else { } else {
$url = \explode(' ', $update['message']['message'], 2); $url = \explode(' ', $update['message']['message'], 2);
@ -102,20 +102,32 @@ class EventHandler extends \danog\MadelineProto\EventHandler
$url = "http://$url"; $url = "http://$url";
} }
} }
$id = yield $this->messages->sendMessage(['peer' => $peerId, 'message' => 'Preparing...', 'reply_to_msg_id' => $messageId])['id']; $id = yield $this->messages->sendMessage(['peer' => $peerId, 'message' => 'Preparing...', 'reply_to_msg_id' => $messageId]);
if (!isset($id['id'])) {
$this->report(\json_encode($id));
foreach ($id['updates'] as $updat) {
if (isset($updat['id'])) {
$id = $updat['id'];
break;
}
}
} else {
$id = $id['id'];
}
$url = new \danog\MadelineProto\FileCallback( $url = new \danog\MadelineProto\FileCallback(
$url, $url,
function ($progress, $speed, $time) use ($peerId, $id) { function ($progress, $speed, $time) use ($peerId, $id) {
$this->logger("Upload progress: $progress%"); $this->logger("Upload progress: $progress%");
static $prev = -1; static $prev = 0;
$progressR = (int) ($progress / 10); $now = \time();
if ($progressR === $prev) { if ($now - $prev < 10 && $progress < 100) {
return; return;
} }
$prev = $progressR; $prev = $now;
try { try {
yield $this->messages->editMessage(['peer' => $peerId, 'id' => $id, 'message' => "Upload progress: $progress%\nSpeed: $speed mbps\nTime elapsed since start: $time"]); yield $this->messages->editMessage(['peer' => $peerId, 'id' => $id, 'message' => "Upload progress: $progress%\nSpeed: $speed mbps\nTime elapsed since start: $time"], ['FloodWaitLimit' => 0]);
} catch (\danog\MadelineProto\RPCErrorException $e) { } catch (\danog\MadelineProto\RPCErrorException $e) {
} }
} }
@ -142,7 +154,7 @@ class EventHandler extends \danog\MadelineProto\EventHandler
yield $this->messages->deleteMessages(['revoke' => true, 'id' => [$id]]); yield $this->messages->deleteMessages(['revoke' => true, 'id' => [$id]]);
} }
} catch (\Throwable $e) { } catch (\Throwable $e) {
if (\strpos($e->getMessage(), 'Could not connect to URI') === false && !($e instanceof UriException)) { if (\strpos($e->getMessage(), 'Could not connect to URI') === false && !($e instanceof UriException) && \strpos($e->getMessage(), 'URI') === false) {
$this->report((string) $e); $this->report((string) $e);
$this->logger((string) $e, \danog\MadelineProto\Logger::FATAL_ERROR); $this->logger((string) $e, \danog\MadelineProto\Logger::FATAL_ERROR);
} }
@ -172,7 +184,7 @@ $settings = [
], ],
]; ];
$MadelineProto = new \danog\MadelineProto\API('bot.madeline', $settings); $MadelineProto = new \danog\MadelineProto\API(($argv[1] ?? 'bot').'.madeline', $settings);
$MadelineProto->async(true); $MadelineProto->async(true);
while (true) { while (true) {
try { try {

View File

@ -322,6 +322,7 @@ trait ResponseHandler
Loop::defer(function () use (&$request, $data) { Loop::defer(function () use (&$request, $data) {
if (isset($request['promise'])) { if (isset($request['promise'])) {
$this->logger->logger('Rejecting: '.(isset($request['_']) ? $request['_'] : '-')); $this->logger->logger('Rejecting: '.(isset($request['_']) ? $request['_'] : '-'));
$this->logger->logger("Rejecting: $data");
$promise = $request['promise']; $promise = $request['promise'];
unset($request['promise']); unset($request['promise']);
@ -519,12 +520,12 @@ trait ResponseHandler
return; return;
case 420: case 420:
$seconds = \preg_replace('/[^0-9]+/', '', $response['error_message']); $seconds = \preg_replace('/[^0-9]+/', '', $response['error_message']);
$limit = isset($request['FloodWaitLimit']) ? $request['FloodWaitLimit'] : $this->API->settings['flood_timeout']['wait_if_lt']; $limit = $request['FloodWaitLimit'] ?? $this->API->settings['flood_timeout']['wait_if_lt'];
if (\is_numeric($seconds) && $seconds < $limit) { if (\is_numeric($seconds) && $seconds < $limit) {
//$this->gotResponseForOutgoingMessageId($request_id); //$this->gotResponseForOutgoingMessageId($request_id);
$this->logger->logger('Flood, waiting '.$seconds.' seconds before repeating async call of '.($request['_'] ?? '').'...', \danog\MadelineProto\Logger::NOTICE); $this->logger->logger('Flood, waiting '.$seconds.' seconds before repeating async call of '.($request['_'] ?? '').'...', \danog\MadelineProto\Logger::NOTICE);
$request['sent'] += $seconds; $request['sent'] = ($request['sent'] ?? \time()) + $seconds;
Loop::delay($seconds * 1000, [$this, 'methodRecall'], ['message_id' => $request_id, ]); Loop::delay($seconds * 1000, [$this, 'methodRecall'], ['message_id' => $request_id, ]);
return; return;