1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-26 20:54:42 +01:00

Add FromAdminOrOutgoing simple filter, markdownUrlEscape function, fix pagination hash & other fixes

This commit is contained in:
Daniil Gentili 2023-08-04 20:15:37 +02:00
parent 17e84ec003
commit 89e1230b06
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
11 changed files with 126 additions and 19 deletions

View File

@ -415,6 +415,8 @@ Want to add your own open-source project to this list? [Click here!](https://doc
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.toggleBotInAttachMenu.html" name="messages.toggleBotInAttachMenu">Enable or disable web bot attachment menu »: messages.toggleBotInAttachMenu</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/channels.toggleSignatures.html" name="channels.toggleSignatures">Enable/disable message signatures in channels: channels.toggleSignatures</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/contacts.toggleTopPeers.html" name="contacts.toggleTopPeers">Enable/disable top peers: contacts.toggleTopPeers</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#markdownurlescape-string-what-string" name="markdownUrlEscape">Escape string for URL: markdownUrlEscape</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#markdowncodeblockescape-string-what-string" name="markdownCodeblockEscape">Escape string for markdown codeblock: markdownCodeblockEscape</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#markdownescape-string-what-string" name="markdownEscape">Escape string for markdown: markdownEscape</a>
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#broadcastcustom-action-action-danog-madelineproto-broadcast-filter-filter-null-int" name="broadcastCustom">Executes a custom broadcast action with all peers (users, chats, channels) of the bot: broadcastCustom</a>
* <a href="https://docs.madelineproto.xyz/API_docs/methods/chatlists.exportChatlistInvite.html" name="chatlists.exportChatlistInvite">Export a folder », creating a chat folder deep link »: chatlists.exportChatlistInvite</a>

2
docs

@ -1 +1 @@
Subproject commit 8b4df61a4eee0d9783086bcc15014759ac927b03
Subproject commit 698fd73e6c9ba286286314917ec6f955f3393aa5

View File

@ -334,7 +334,7 @@ final class Connection
$arguments['message']['reply_to_random_id'] = $arguments['message']['reply_to_msg_id'];
}
} elseif ($method === 'messages.uploadMedia' || $method === 'messages.sendMedia') {
if (is_array($arguments['media'])) {
if (\is_array($arguments['media'])) {
if ($arguments['media']['_'] === 'inputMediaPhotoExternal') {
$arguments['media']['_'] = 'inputMediaUploadedPhoto';
$arguments['media']['file'] = new RemoteUrl($arguments['media']['url']);
@ -342,7 +342,7 @@ final class Connection
$arguments['media']['_'] = 'inputMediaUploadedDocument';
$arguments['media']['file'] = new RemoteUrl($arguments['media']['url']);
$arguments['media']['mime_type'] = Extension::getMimeFromExtension(
pathinfo($arguments['media']['url'], PATHINFO_EXTENSION),
\pathinfo($arguments['media']['url'], PATHINFO_EXTENSION),
'application/octet-stream'
);
}

View File

@ -21,6 +21,7 @@ use danog\MadelineProto\EventHandler\Message\GroupMessage;
use danog\MadelineProto\EventHandler\Message\PrivateMessage;
use danog\MadelineProto\EventHandler\Message\ServiceMessage;
use danog\MadelineProto\EventHandler\SimpleFilter\FromAdmin;
use danog\MadelineProto\EventHandler\SimpleFilter\FromAdminOrOutgoing;
use danog\MadelineProto\EventHandler\SimpleFilter\HasAudio;
use danog\MadelineProto\EventHandler\SimpleFilter\HasDocument;
use danog\MadelineProto\EventHandler\SimpleFilter\HasDocumentPhoto;
@ -91,6 +92,7 @@ abstract class Filter
HasSticker::class => new FilterSticker,
HasVideo::class => new FilterVideo,
HasVoice::class => new FilterVoice,
FromAdminOrOutgoing::class => new FiltersOr(new FilterFromAdmin, new FilterOutgoing),
default => \is_subclass_of($type->getName(), Update::class)
? new class($type->getName()) extends Filter {
public function __construct(private readonly string $class)

View File

@ -0,0 +1,8 @@
<?php declare(strict_types=1);
namespace danog\MadelineProto\EventHandler\SimpleFilter;
/** Allows messages from the bot admin or outgoing messages */
interface FromAdminOrOutgoing
{
}

View File

@ -645,11 +645,11 @@ abstract class InternalDoc
*
* Returns a vector hash.
*
* @param array $ints IDs
* @param array $longs IDs
*/
public static function genVectorHash(array $ints): string
public static function genVectorHash(array $longs): string
{
return \danog\MadelineProto\Tools::genVectorHash($ints);
return \danog\MadelineProto\Tools::genVectorHash($longs);
}
/**
* Get admin IDs (equal to all user report peers).
@ -1303,6 +1303,15 @@ abstract class InternalDoc
{
return \danog\MadelineProto\StrTools::markdownToMessageEntities($markdown);
}
/**
* Escape string for URL.
*
* @param string $what String to escape
*/
public static function markdownUrlEscape(string $what): string
{
return \danog\MadelineProto\StrTools::markdownUrlEscape($what);
}
/**
* Telegram UTF-8 multibyte split.
*

View File

@ -201,6 +201,7 @@ abstract class StrTools extends Extension
{
return \str_replace(
[
'\\',
'_',
'*',
'[',
@ -221,6 +222,7 @@ abstract class StrTools extends Extension
'!',
],
[
'\\\\',
'\\_',
'\\*',
'\\[',
@ -250,7 +252,16 @@ abstract class StrTools extends Extension
*/
public static function markdownCodeblockEscape(string $what): string
{
return \str_replace('`', '\\`', $what);
return \str_replace('```', '\\```', $what);
}
/**
* Escape string for URL.
*
* @param string $what String to escape
*/
public static function markdownUrlEscape(string $what): string
{
return \str_replace(')', '\\)', $what);
}
/**
* Escape type name.

View File

@ -217,7 +217,7 @@ trait BotAPI
if (isset($data['fwd_from']['channel_post'])) {
$newd['forward_from_message_id'] = $data['fwd_from']['channel_post'];
}
if (isset($data['media'])) {
if (isset($data['media']) && $data['media']['_'] !== 'messageMediaWebPage') {
$newd = \array_merge($newd, $this->MTProtoToBotAPI($data['media']));
}
return $newd;

View File

@ -98,10 +98,13 @@ final class MarkdownEntities extends Entities
$offset++;
}
$piece = '';
$posClose = $offset;
while (($posClose = \strpos($markdown, '```', $posClose)) !== false) {
if ($markdown[$posClose-1] === '\\') {
$posClose++;
$piece .= \substr($markdown, $offset, ($posClose-$offset)-1)."```";
$posClose += 3;
$offset = $posClose;
continue;
}
break;
@ -109,10 +112,11 @@ final class MarkdownEntities extends Entities
if ($posClose === false) {
throw new AssertionError("Unclosed ``` opened @ pos $offset!");
}
$piece .= \substr($markdown, $offset, $posClose-$offset);
$start = $messageLen;
$message .= $piece = \substr($markdown, $offset, $posClose-$offset);
$message .= $piece;
$pieceLen = StrTools::mbStrlen($piece);
$messageLen += $pieceLen;

View File

@ -152,16 +152,16 @@ abstract class Tools extends AsyncTools
*
* Returns a vector hash.
*
* @param array $ints IDs
* @param array $longs IDs
*/
public static function genVectorHash(array $ints): string
public static function genVectorHash(array $longs): string
{
$hash = 0;
foreach ($ints as $id) {
$hash = $hash ^ ($id >> 21);
$hash = $hash ^ ($id << 35);
$hash = $hash ^ ($id >> 4);
$hash = $hash + $id;
foreach ($longs as $long) {
$hash ^= $hash >> 21;
$hash ^= $hash << 35;
$hash ^= $hash >> 4;
$hash = $hash + $long;
}
return self::packSignedLong($hash);
}
@ -177,8 +177,6 @@ abstract class Tools extends AsyncTools
}
try {
return \random_int(0, PHP_INT_MAX) % $modulus;
} catch (Exception $e) {
// random_compat will throw an Exception, which in PHP 5 does not implement Throwable
} catch (Throwable $e) {
// If a sufficient source of randomness is unavailable, random_bytes() will throw an
// object that implements the Throwable interface (Exception, TypeError, Error).

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace danog\MadelineProto\Test;
use danog\MadelineProto\StrTools;
use danog\MadelineProto\Tools;
/** @internal */
class EntitiesTest extends MadelineTestCase
@ -45,8 +46,35 @@ class EntitiesTest extends MadelineTestCase
true
),
);
$resultMTProto = self::$MadelineProto->messages->sendMessage(peer: \getenv('DEST'), message: \htmlentities($html), parse_mode: $mode);
$resultMTProto = self::$MadelineProto->extractMessage($resultMTProto);
$result = self::$MadelineProto->MTProtoToBotAPI($resultMTProto);
$this->assertEquals($html, $result['text']);
$this->assertNoRelevantEntities($result['entities']);
} else {
$resultMTProto = self::$MadelineProto->messages->sendMessage(peer: \getenv('DEST'), message: Tools::markdownEscape($html), parse_mode: $mode);
$resultMTProto = self::$MadelineProto->extractMessage($resultMTProto);
$result = self::$MadelineProto->MTProtoToBotAPI($resultMTProto);
$this->assertEquals($html, $result['text']);
$this->assertNoRelevantEntities($result['entities']);
$resultMTProto = self::$MadelineProto->messages->sendMessage(peer: \getenv('DEST'), message: "```\n".Tools::markdownCodeblockEscape($html)."\n```", parse_mode: $mode);
$resultMTProto = self::$MadelineProto->extractMessage($resultMTProto);
$result = self::$MadelineProto->MTProtoToBotAPI($resultMTProto);
$this->assertEquals($html, \rtrim($result['text']));
$this->assertEquals([['offset' => 0, 'length' => StrTools::mbStrlen($html), 'language' => '', 'type' => 'pre']], $result['entities']);
}
}
private function assertNoRelevantEntities(array $entities): void
{
$entities = \array_filter($entities, fn (array $e) => !\in_array(
$e['type'],
['url', 'email', 'phone_number', 'mention', 'bot_command'],
true
));
$this->assertEmpty($entities);
}
public function provideEntities(): array
{
$this->setUpBeforeClass();
@ -310,6 +338,25 @@ class EntitiesTest extends MadelineTestCase
],
],
],
[
'markdown',
StrTools::markdownEscape('\\ test testovich _*~'),
'\\ test testovich _*~',
[],
],
[
'markdown',
"```\n".StrTools::markdownCodeblockEscape('\\ ```').'```',
'\\ ```',
[
[
'offset' => 0,
'length' => 5,
'type' => 'pre',
'language' => ''
]
],
],
[
'markdown',
'[link ](https://google.com/)test',
@ -323,6 +370,32 @@ class EntitiesTest extends MadelineTestCase
],
],
],
[
'markdown',
'[link]('.StrTools::markdownUrlEscape('https://google.com/').')',
'link',
[
[
'offset' => 0,
'length' => 4,
'type' => 'text_url',
'url' => 'https://google.com/'
],
],
],
[
'markdown',
'[link]('.StrTools::markdownUrlEscape('https://google.com/?v=\\test').')',
'link',
[
[
'offset' => 0,
'length' => 4,
'type' => 'text_url',
'url' => 'https://google.com/?v=\\test'
],
],
],
[
'markdown',
'[link ](https://google.com/)',