diff --git a/examples/bot.php b/examples/bot.php index d78f4a13..d099b3a5 100755 --- a/examples/bot.php +++ b/examples/bot.php @@ -79,7 +79,6 @@ class MyEventHandler extends EventHandler return; } $res = \json_encode($update, JSON_PRETTY_PRINT); - try { yield $this->messages->sendMessage(['peer' => $update, 'message' => "$res", 'reply_to_msg_id' => isset($update['message']['id']) ? $update['message']['id'] : null, 'parse_mode' => 'HTML']); if (isset($update['message']['media']) && $update['message']['media']['_'] !== 'messageMediaGame') { diff --git a/src/danog/MadelineProto/APIWrapper.php b/src/danog/MadelineProto/APIWrapper.php index 04f16686..774d9dc7 100644 --- a/src/danog/MadelineProto/APIWrapper.php +++ b/src/danog/MadelineProto/APIWrapper.php @@ -67,6 +67,22 @@ final class APIWrapper * @var integer */ private int $serialized = 0; + /** + * Whether lua is being used. + * + * @internal + * + * @var boolean + */ + private bool $lua = false; + /** + * Whether async is enabled. + * + * @internal + * + * @var boolean + */ + private bool $async = false; /** * AbstractAPIFactory instance. @@ -111,7 +127,7 @@ final class APIWrapper */ public static function __sleep(): array { - return ['API', 'webApiTemplate', 'gettingApiId', 'myTelegramOrgWrapper', 'storage']; + return ['API', 'webApiTemplate', 'gettingApiId', 'myTelegramOrgWrapper', 'storage', 'lua', 'async']; } /** @@ -124,6 +140,16 @@ final class APIWrapper return $this->API; } + /** + * Whether async is being used. + * + * @return boolean + */ + public function isAsync(): bool + { + return $this->async; + } + /** * Get API factory. * diff --git a/src/danog/MadelineProto/AbstractAPIFactory.php b/src/danog/MadelineProto/AbstractAPIFactory.php index ae9296c7..a6954717 100644 --- a/src/danog/MadelineProto/AbstractAPIFactory.php +++ b/src/danog/MadelineProto/AbstractAPIFactory.php @@ -54,7 +54,7 @@ abstract class AbstractAPIFactory extends AsyncConstruct * * @var boolean */ - private bool $async = false; + protected bool $async = false; /** * Method list. * diff --git a/src/danog/MadelineProto/DocsBuilder.php b/src/danog/MadelineProto/DocsBuilder.php index f2533fe3..6173528f 100644 --- a/src/danog/MadelineProto/DocsBuilder.php +++ b/src/danog/MadelineProto/DocsBuilder.php @@ -436,7 +436,11 @@ image: https://docs.madelineproto.xyz/favicons/android-chrome-256x256.png ## Type: bytes [Back to constructor index](index.md) -An object of type `\\danog\\MadelineProto\\TL\\Types\\Bytes`. +```php +$bytes = "simple string of bytes"; +``` + +Internally, an object of type `\\danog\\MadelineProto\\TL\\Types\\Bytes`. When casted to string, turns into a string of bytes of variable length, with length smaller than or equal to 16777215. When JSON-serialized, turns into an array of the following format: ``` diff --git a/src/danog/MadelineProto/InternalDoc.php b/src/danog/MadelineProto/InternalDoc.php index bf254e13..7d5d2be7 100644 --- a/src/danog/MadelineProto/InternalDoc.php +++ b/src/danog/MadelineProto/InternalDoc.php @@ -4121,19 +4121,6 @@ class InternalDoc extends APIFactory { return $this->API->getDataCenterConnections(); } - /** - * Get correct settings array for the latest version. - * - * @param array $settings Current settings array - * @param array $previousSettings Previous settings array - * - * @return array - */ - public function getSettings(array $settings, array $previousSettings = [ - ]): array - { - return \danog\MadelineProto\MTProto::getSettings($settings, $previousSettings); - } /** * Parse, update and store settings. * @@ -4146,6 +4133,15 @@ class InternalDoc extends APIFactory { return $this->__call(__FUNCTION__, [$settings, $reinit, $extra]); } + /** + * Return current settings array. + * + * @return array + */ + public function getSettings(): array + { + return $this->API->getSettings(); + } /** * Setup logger. * diff --git a/src/danog/MadelineProto/Logger.php b/src/danog/MadelineProto/Logger.php index 46b1fac3..47d8787f 100644 --- a/src/danog/MadelineProto/Logger.php +++ b/src/danog/MadelineProto/Logger.php @@ -110,7 +110,7 @@ class Logger { if (!self::$default) { // The getLogger function will automatically init the static logger, but we'll do it again anyway - self::$default = self::getLoggerFromSettings(MTProto::getSettings($settings)); + self::$default = self::getLoggerFromSettings(MTProto::parseSettings($settings)); } } /** diff --git a/src/danog/MadelineProto/MTProto.php b/src/danog/MadelineProto/MTProto.php index 25f34b78..1003ad68 100644 --- a/src/danog/MadelineProto/MTProto.php +++ b/src/danog/MadelineProto/MTProto.php @@ -976,9 +976,11 @@ class MTProto extends AsyncConstruct implements TLCallback * @param array $settings Current settings array * @param array $previousSettings Previous settings array * + * @internal + * * @return array */ - public static function getSettings(array $settings, array $previousSettings = []): array + public static function parseSettings(array $settings, array $previousSettings = []): array { Magic::classExists(); $settings = \array_replace_recursive($previousSettings, $settings); @@ -1296,7 +1298,7 @@ class MTProto extends AsyncConstruct implements TLCallback */ public function updateSettings(array $settings, bool $reinit = true): \Generator { - $settings = self::getSettings($settings, $this->settings); + $settings = self::parseSettings($settings, $this->settings); if ($settings['app_info'] === null) { throw new \danog\MadelineProto\Exception(Lang::$current_lang['api_not_set'], 0, null, 'MadelineProto', 1); } @@ -1312,6 +1314,15 @@ class MTProto extends AsyncConstruct implements TLCallback yield from $this->initAsynchronously(); } } + /** + * Return current settings array. + * + * @return array + */ + public function getSettings(): array + { + return $this->settings; + } /** * Setup logger. * diff --git a/src/danog/MadelineProto/MyTelegramOrgWrapper.php b/src/danog/MadelineProto/MyTelegramOrgWrapper.php index aa5485bd..f6a6ee54 100644 --- a/src/danog/MadelineProto/MyTelegramOrgWrapper.php +++ b/src/danog/MadelineProto/MyTelegramOrgWrapper.php @@ -42,7 +42,7 @@ class MyTelegramOrgWrapper } public function __construct($settings = []) { - $this->settings = MTProto::getSettings($settings, $this->settings); + $this->settings = MTProto::parseSettings($settings, $this->settings); $this->__wakeup(); } public function __wakeup() @@ -53,7 +53,7 @@ class MyTelegramOrgWrapper if (!$this->jar || !$this->jar instanceof InMemoryCookieJar) { $this->jar = new InMemoryCookieJar(); } - $this->settings = MTProto::getSettings($this->settings); + $this->settings = MTProto::parseSettings($this->settings); $this->datacenter = new DataCenter(new class($this->settings) { public function __construct($settings) { diff --git a/src/danog/MadelineProto/TL/Types/Button.php b/src/danog/MadelineProto/TL/Types/Button.php index 7ca115c0..e312b232 100644 --- a/src/danog/MadelineProto/TL/Types/Button.php +++ b/src/danog/MadelineProto/TL/Types/Button.php @@ -19,74 +19,147 @@ namespace danog\MadelineProto\TL\Types; +use danog\MadelineProto\MTProto; +use danog\MadelineProto\Tools; + class Button implements \JsonSerializable, \ArrayAccess { - use \danog\Serializable; - use \danog\MadelineProto\Tools; - private $info = []; - private $data = []; - public function __magic_construct($API, $message, $button) + /** + * Button data. + */ + private array $button; + /** + * MTProto instance. + */ + private MTProto $API; + /** + * Message ID. + */ + private int $id; + /** + * Peer ID. + * + * @var array|int + */ + private $peer; + /** + * Constructor function. + * + * @param MTProto $API API instance + * @param array $message Message + * @param array $button Button info + */ + public function __construct(MTProto $API, array $message, array $button) { - $this->data = $button; - $this->info['peer'] = $message['to_id'] === ['_' => 'peerUser', 'user_id' => $API->authorization['user']['id']] ? $message['from_id'] : $message['to_id']; - $this->info['id'] = $message['id']; - $this->info['API'] = $API; + $this->button = $button; + $this->peer = $message['to_id'] === ['_' => 'peerUser', 'user_id' => $API->authorization['user']['id']] ? $message['from_id'] : $message['to_id']; + $this->id = $message['id']; + $this->API = $API; } - public function __sleep() + /** + * Sleep function. + * + * @return array + */ + public function __sleep(): array { - return ['data', 'info']; + return ['button', 'peer', 'id', 'API']; } - public function click($donotwait = false, $params = []) + /** + * Click on button. + * + * @param boolean $donotwait Whether to wait for the result of the method + * + * @return mixed + */ + public function click(bool $donotwait = true) { - if (\is_array($donotwait)) { - $params = $donotwait; - $donotwait = false; - } - $async = $params['async'] ?? (isset($this->info['API']->wrapper) ? $this->info['API']->wrapper->async : true); + $async = isset($this->API->wrapper) ? $this->API->wrapper->isAsync() : true; $method = $donotwait ? 'methodCallAsyncWrite' : 'methodCallAsyncRead'; - switch ($this->data['_']) { + switch ($this->button['_']) { default: return false; case 'keyboardButtonUrl': - return $this->data['url']; + return $this->button['url']; case 'keyboardButton': - $res = $this->info['API']->methodCallAsyncRead('messages.sendMessage', ['peer' => $this->info['peer'], 'message' => $this->data['text'], 'reply_to_msg_id' => $this->info['id']], ['datacenter' => $this->info['API']->datacenter->curdc]); + $res = $this->API->methodCallAsyncRead('messages.sendMessage', ['peer' => $this->peer, 'message' => $this->button['text'], 'reply_to_msg_id' => $this->id], ['datacenter' => $this->API->datacenter->curdc]); break; case 'keyboardButtonCallback': - $res = $this->info['API']->{$method}('messages.getBotCallbackAnswer', ['peer' => $this->info['peer'], 'msg_id' => $this->info['id'], 'data' => $this->data['data']], ['datacenter' => $this->info['API']->datacenter->curdc]); + $res = $this->API->{$method}('messages.getBotCallbackAnswer', ['peer' => $this->peer, 'msg_id' => $this->id, 'data' => $this->button['data']], ['datacenter' => $this->API->datacenter->curdc]); break; case 'keyboardButtonGame': - $res = $this->info['API']->{$method}('messages.getBotCallbackAnswer', ['peer' => $this->info['peer'], 'msg_id' => $this->info['id'], 'game' => true], ['datacenter' => $this->info['API']->datacenter->curdc]); + $res = $this->API->{$method}('messages.getBotCallbackAnswer', ['peer' => $this->peer, 'msg_id' => $this->id, 'game' => true], ['datacenter' => $this->API->datacenter->curdc]); break; } - return $async ? $res : \danog\MadelineProto\Tools::wait($res); + return $async ? $res : Tools::wait($res); } - public function __debugInfo() + /** + * Get debug info. + * + * @return array + */ + public function __debugInfo(): array { - return ['data' => $this->data, 'info' => ['peer' => $this->info['peer'], 'id' => $this->info['id']]]; + $res = \get_object_vars($this); + unset($res['API']); + return $res; } - public function jsonSerialize() + /** + * Serialize button. + * + * @return array + */ + public function jsonSerialize(): array { - return (array) $this->data; + return $this->button; } - public function offsetSet($name, $value) + /** + * Set button info. + * + * @param $name Offset + * @param mixed $value Value + * + * @return void + */ + public function offsetSet($name, $value): void { if ($name === null) { - $this->data[] = $value; + $this->button[] = $value; } else { - $this->data[$name] = $value; + $this->button[$name] = $value; } } + /** + * Get button info. + * + * @param $name Field name + * + * @return void + */ public function offsetGet($name) { - return $this->data[$name]; + return $this->button[$name]; } - public function offsetUnset($name) + /** + * Unset button info. + * + * @param $name Offset + * + * @return void + */ + public function offsetUnset($name): void { - unset($this->data[$name]); + unset($this->button[$name]); } - public function offsetExists($name) + /** + * Check if button field exists. + * + * @param $name Offset + * + * @return boolean + */ + public function offsetExists($name): bool { - return isset($this->data[$name]); + return isset($this->button[$name]); } } diff --git a/src/danog/MadelineProto/TL/Types/Bytes.php b/src/danog/MadelineProto/TL/Types/Bytes.php index 23483697..edf6f4a5 100644 --- a/src/danog/MadelineProto/TL/Types/Bytes.php +++ b/src/danog/MadelineProto/TL/Types/Bytes.php @@ -21,42 +21,95 @@ namespace danog\MadelineProto\TL\Types; class Bytes implements \JsonSerializable, \ArrayAccess { - use \danog\Serializable; - private $bytes = []; - public function __magic_construct($bytes) + /** + * Bytes. + * + * @var string Bytes + */ + private string $bytes; + /** + * Constructor function. + * + * @param string $bytes Contents + */ + public function __construct(string $bytes) { $this->bytes = $bytes; } - public function __sleep() + /** + * Sleep function. + * + * @return array + */ + public function __sleep(): array { return ['bytes']; } - public function __toString() + /** + * Cast bytes to string. + * + * @return string + */ + public function __toString(): string { return $this->bytes; } - public function jsonSerialize() + /** + * Obtain values for JSON-encoding. + * + * @return array + */ + public function jsonSerialize(): array { return ['_' => 'bytes', 'bytes' => \base64_encode($this->bytes)]; } - public function offsetSet($name, $value) + /** + * Set char at offset. + * + * @param integer|null $offset Offset + * @param string $value Char + * + * @return void + */ + public function offsetSet($offset, $value): void { - if ($name === null) { + if ($offset === null) { $this->bytes .= $value; } else { - $this->bytes[$name] = $value; + $this->bytes[$offset] = $value; } } - public function offsetGet($name) + /** + * Get char at offset. + * + * @param integer $offset Name + * + * @return string + */ + public function offsetGet($offset): string { - return $this->bytes[$name]; + return $this->bytes[$offset]; } - public function offsetUnset($name) + /** + * Unset char at offset. + * + * @param integer $offset Offset + * + * @return void + */ + public function offsetUnset($offset): void { - unset($this->bytes[$name]); + unset($this->bytes[$offset]); } - public function offsetExists($name) + /** + * Check if char at offset exists. + * + * @param integer $offset Offset + * + * @return boolean + */ + public function offsetExists($offset): bool { - return isset($this->bytes[$name]); + return isset($this->bytes[$offset]); } }