Further public API improvements

This commit is contained in:
Daniil Gentili 2020-02-26 18:12:00 +01:00
parent fe8e795f8a
commit 5af7130b25
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
10 changed files with 234 additions and 72 deletions

View File

@ -79,7 +79,6 @@ class MyEventHandler extends EventHandler
return;
}
$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') {

View File

@ -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.
*

View File

@ -54,7 +54,7 @@ abstract class AbstractAPIFactory extends AsyncConstruct
*
* @var boolean
*/
private bool $async = false;
protected bool $async = false;
/**
* Method list.
*

View File

@ -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:
```

View File

@ -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.
*

View File

@ -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));
}
}
/**

View File

@ -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.
*

View File

@ -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)
{

View File

@ -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]);
}
}

View File

@ -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]);
}
}