mirror of
https://github.com/danog/MadelineProto.git
synced 2024-11-30 06:39:01 +01:00
Simplify code escaping
This commit is contained in:
parent
853fa4167d
commit
1dcc9af659
@ -448,7 +448,7 @@ Want to add your own open-source project to this list? [Click here!](https://doc
|
|||||||
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#extractmessageupdate-array-updates-array" name="extractMessageUpdate">Extract an update message constructor from an Updates constructor: extractMessageUpdate</a>
|
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#extractmessageupdate-array-updates-array" name="extractMessageUpdate">Extract an update message constructor from an Updates constructor: extractMessageUpdate</a>
|
||||||
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#extractbotapifile-array-info-array" name="extractBotAPIFile">Extract file info from bot API message: extractBotAPIFile</a>
|
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#extractbotapifile-array-info-array" name="extractBotAPIFile">Extract file info from bot API message: extractBotAPIFile</a>
|
||||||
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.getCustomEmojiDocuments.html" name="messages.getCustomEmojiDocuments">Fetch custom emoji stickers »: messages.getCustomEmojiDocuments</a>
|
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.getCustomEmojiDocuments.html" name="messages.getCustomEmojiDocuments">Fetch custom emoji stickers »: messages.getCustomEmojiDocuments</a>
|
||||||
* <a href="https://docs.madelineproto.xyz/API_docs/methods/chatlists.getChatlistUpdates.html" name="chatlists.getChatlistUpdates">Fetch new chats associated with an imported chat folder deep link ». Must be invoked at most every chatlistupdateperiod seconds (as per the related client configuration parameter »): chatlists.getChatlistUpdates</a>
|
* <a href="https://docs.madelineproto.xyz/API_docs/methods/chatlists.getChatlistUpdates.html" name="chatlists.getChatlistUpdates">Fetch new chats associated with an imported chat folder deep link ». Must be invoked at most every chatlist_update_period seconds (as per the related client configuration parameter »): chatlists.getChatlistUpdates</a>
|
||||||
* <a href="https://docs.madelineproto.xyz/API_docs/methods/account.getSavedRingtones.html" name="account.getSavedRingtones">Fetch saved notification sounds: account.getSavedRingtones</a>
|
* <a href="https://docs.madelineproto.xyz/API_docs/methods/account.getSavedRingtones.html" name="account.getSavedRingtones">Fetch saved notification sounds: account.getSavedRingtones</a>
|
||||||
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.getMessageEditData.html" name="messages.getMessageEditData">Find out if a media message's caption can be edited: messages.getMessageEditData</a>
|
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.getMessageEditData.html" name="messages.getMessageEditData">Find out if a media message's caption can be edited: messages.getMessageEditData</a>
|
||||||
* <a href="https://docs.madelineproto.xyz/API_docs/methods/account.finishTakeoutSession.html" name="account.finishTakeoutSession">Finish account takeout session: account.finishTakeoutSession</a>
|
* <a href="https://docs.madelineproto.xyz/API_docs/methods/account.finishTakeoutSession.html" name="account.finishTakeoutSession">Finish account takeout session: account.finishTakeoutSession</a>
|
||||||
@ -615,7 +615,7 @@ 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.getUnreadReactions.html" name="messages.getUnreadReactions">Get unread reactions to messages you sent: messages.getUnreadReactions</a>
|
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.getUnreadReactions.html" name="messages.getUnreadReactions">Get unread reactions to messages you sent: messages.getUnreadReactions</a>
|
||||||
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#getwebwarnings-string" name="getWebWarnings">Get various warnings to show to the user in the web UI: getWebWarnings</a>
|
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#getwebwarnings-string" name="getWebWarnings">Get various warnings to show to the user in the web UI: getWebWarnings</a>
|
||||||
* <a href="https://docs.madelineproto.xyz/API_docs/methods/account.getWebAuthorizations.html" name="account.getWebAuthorizations">Get web login widget authorizations: account.getWebAuthorizations</a>
|
* <a href="https://docs.madelineproto.xyz/API_docs/methods/account.getWebAuthorizations.html" name="account.getWebAuthorizations">Get web login widget authorizations: account.getWebAuthorizations</a>
|
||||||
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.getMessageReadParticipants.html" name="messages.getMessageReadParticipants">Get which users read a specific message: only available for groups and supergroups with less than chatreadmarksizethreshold members, read receipts will be stored for chatreadmarkexpireperiod seconds after the message was sent, see client configuration for more info »: messages.getMessageReadParticipants</a>
|
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.getMessageReadParticipants.html" name="messages.getMessageReadParticipants">Get which users read a specific message: only available for groups and supergroups with less than chat_read_mark_size_threshold members, read receipts will be stored for chat_read_mark_expire_period seconds after the message was sent, see client configuration for more info »: messages.getMessageReadParticipants</a>
|
||||||
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#getsecretmessage-int-chatid-int-randomid-danog-madelineproto-eventhandler-message-secretmessage" name="getSecretMessage">Gets a secret chat message: getSecretMessage</a>
|
* <a href="https://docs.madelineproto.xyz/PHP/danog/MadelineProto/API.html#getsecretmessage-int-chatid-int-randomid-danog-madelineproto-eventhandler-message-secretmessage" name="getSecretMessage">Gets a secret chat message: getSecretMessage</a>
|
||||||
* <a href="https://docs.madelineproto.xyz/API_docs/methods/account.getNotifySettings.html" name="account.getNotifySettings">Gets current notification settings for a given user/group, from all users/all groups: account.getNotifySettings</a>
|
* <a href="https://docs.madelineproto.xyz/API_docs/methods/account.getNotifySettings.html" name="account.getNotifySettings">Gets current notification settings for a given user/group, from all users/all groups: account.getNotifySettings</a>
|
||||||
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.getFeaturedEmojiStickers.html" name="messages.getFeaturedEmojiStickers">Gets featured custom emoji stickersets: messages.getFeaturedEmojiStickers</a>
|
* <a href="https://docs.madelineproto.xyz/API_docs/methods/messages.getFeaturedEmojiStickers.html" name="messages.getFeaturedEmojiStickers">Gets featured custom emoji stickersets: messages.getFeaturedEmojiStickers</a>
|
||||||
|
2
docs
2
docs
@ -1 +1 @@
|
|||||||
Subproject commit 768b8692b4c31ebb2fa485a554d351b3b5edcac0
|
Subproject commit 8e5b001495ee886ff2a9f7b252ac7d41f1b013cb
|
19
src/EventHandler/Message/Entities/InputMentionName.php
Normal file
19
src/EventHandler/Message/Entities/InputMentionName.php
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
<?php declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace danog\MadelineProto\EventHandler\Message\Entities;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Message entity representing a [user mention](https://core.telegram.org/api/mentions) created by the user, not returned by the API.
|
||||||
|
*/
|
||||||
|
final class InputMentionName extends MessageEntity
|
||||||
|
{
|
||||||
|
/** @var int|string Identifier of the user that was mentioned */
|
||||||
|
public readonly int|string $userId;
|
||||||
|
|
||||||
|
/** @internal */
|
||||||
|
protected function __construct(array $rawEntities)
|
||||||
|
{
|
||||||
|
parent::__construct($rawEntities);
|
||||||
|
$this->userId = $rawEntities['user_id'];
|
||||||
|
}
|
||||||
|
}
|
@ -41,7 +41,8 @@ abstract class MessageEntity implements JsonSerializable
|
|||||||
'messageEntityCode' => new Code($entity),
|
'messageEntityCode' => new Code($entity),
|
||||||
'messageEntityPre' => new Pre($entity),
|
'messageEntityPre' => new Pre($entity),
|
||||||
'messageEntityTextUrl' => new TextUrl($entity),
|
'messageEntityTextUrl' => new TextUrl($entity),
|
||||||
'messageEntityMentionName','inputMessageEntityMentionName' => new MentionName($entity),
|
'messageEntityMentionName' => new MentionName($entity),
|
||||||
|
'inputMessageEntityMentionName' => new InputMentionName($entity),
|
||||||
'messageEntityPhone' => new Phone($entity),
|
'messageEntityPhone' => new Phone($entity),
|
||||||
'messageEntityCashtag' => new Cashtag($entity),
|
'messageEntityCashtag' => new Cashtag($entity),
|
||||||
'messageEntityUnderline' => new Underline($entity),
|
'messageEntityUnderline' => new Underline($entity),
|
||||||
|
@ -1321,6 +1321,15 @@ abstract class InternalDoc
|
|||||||
{
|
{
|
||||||
$this->wrapper->getAPI()->logout();
|
$this->wrapper->getAPI()->logout();
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Escape string for markdown code section.
|
||||||
|
*
|
||||||
|
* @param string $what String to escape
|
||||||
|
*/
|
||||||
|
public static function markdownCodeEscape(string $what): string
|
||||||
|
{
|
||||||
|
return \danog\MadelineProto\StrTools::markdownCodeEscape($what);
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Escape string for markdown codeblock.
|
* Escape string for markdown codeblock.
|
||||||
*
|
*
|
||||||
|
@ -9,7 +9,7 @@ final class Blacklist
|
|||||||
'account.getPasswordSettings' => 'You cannot use this method directly; use $MadelineProto->update2fa($params), instead (see https://docs.madelineproto.xyz for more info)',
|
'account.getPasswordSettings' => 'You cannot use this method directly; use $MadelineProto->update2fa($params), instead (see https://docs.madelineproto.xyz for more info)',
|
||||||
'messages.receivedQueue' => 'You cannot use this method directly',
|
'messages.receivedQueue' => 'You cannot use this method directly',
|
||||||
'messages.getDhConfig' => 'You cannot use this method directly, instead use $MadelineProto->getDhConfig();',
|
'messages.getDhConfig' => 'You cannot use this method directly, instead use $MadelineProto->getDhConfig();',
|
||||||
'auth.bindTempAuthKey' => 'You cannot use this method directly, instead modify the PFS and default_temp_auth_key_expires_in settings, see https://docs.madelineproto.xyz/docs/SETTINGS.html for more info',
|
'auth.bindTempAuthKey' => 'You cannot use this method directly, instead modify the PFS settings, see https://docs.madelineproto.xyz/docs/SETTINGS.html for more info',
|
||||||
'auth.exportAuthorization' => 'You cannot use this method directly, use $MadelineProto->exportAuthorization() instead, see https://docs.madelineproto.xyz/docs/LOGIN.html',
|
'auth.exportAuthorization' => 'You cannot use this method directly, use $MadelineProto->exportAuthorization() instead, see https://docs.madelineproto.xyz/docs/LOGIN.html',
|
||||||
'auth.importAuthorization' => 'You cannot use this method directly, use $MadelineProto->importAuthorization($authorization) instead, see https://docs.madelineproto.xyz/docs/LOGIN.html',
|
'auth.importAuthorization' => 'You cannot use this method directly, use $MadelineProto->importAuthorization($authorization) instead, see https://docs.madelineproto.xyz/docs/LOGIN.html',
|
||||||
'auth.logOut' => 'You cannot use this method directly, use the logout method instead (see https://docs.madelineproto.xyz for more info)',
|
'auth.logOut' => 'You cannot use this method directly, use the logout method instead (see https://docs.madelineproto.xyz for more info)',
|
||||||
|
@ -25,6 +25,7 @@ use danog\MadelineProto\EventHandler\Message\Entities\Bold;
|
|||||||
use danog\MadelineProto\EventHandler\Message\Entities\Code;
|
use danog\MadelineProto\EventHandler\Message\Entities\Code;
|
||||||
use danog\MadelineProto\EventHandler\Message\Entities\CustomEmoji;
|
use danog\MadelineProto\EventHandler\Message\Entities\CustomEmoji;
|
||||||
use danog\MadelineProto\EventHandler\Message\Entities\Email;
|
use danog\MadelineProto\EventHandler\Message\Entities\Email;
|
||||||
|
use danog\MadelineProto\EventHandler\Message\Entities\InputMentionName;
|
||||||
use danog\MadelineProto\EventHandler\Message\Entities\Italic;
|
use danog\MadelineProto\EventHandler\Message\Entities\Italic;
|
||||||
use danog\MadelineProto\EventHandler\Message\Entities\Mention;
|
use danog\MadelineProto\EventHandler\Message\Entities\Mention;
|
||||||
use danog\MadelineProto\EventHandler\Message\Entities\MentionName;
|
use danog\MadelineProto\EventHandler\Message\Entities\MentionName;
|
||||||
@ -161,6 +162,7 @@ abstract class StrTools extends Extension
|
|||||||
$entity instanceof Spoiler => $allowTelegramTags ? '<tg-spoiler>' : '',
|
$entity instanceof Spoiler => $allowTelegramTags ? '<tg-spoiler>' : '',
|
||||||
$entity instanceof CustomEmoji => $allowTelegramTags ? '<tg-emoji emoji-id="'.$entity->documentId.'">' : '',
|
$entity instanceof CustomEmoji => $allowTelegramTags ? '<tg-emoji emoji-id="'.$entity->documentId.'">' : '',
|
||||||
$entity instanceof MentionName => $allowTelegramTags ? '<a href="tg://user?id='.$entity->userId.'">' : '',
|
$entity instanceof MentionName => $allowTelegramTags ? '<a href="tg://user?id='.$entity->userId.'">' : '',
|
||||||
|
$entity instanceof InputMentionName => $allowTelegramTags ? '<a href="tg://user?id='.$entity->userId.'">' : '',
|
||||||
default => '',
|
default => '',
|
||||||
};
|
};
|
||||||
$offset += $length;
|
$offset += $length;
|
||||||
@ -176,6 +178,7 @@ abstract class StrTools extends Extension
|
|||||||
$entity instanceof Spoiler => $allowTelegramTags ? '</tg-spoiler>' : '',
|
$entity instanceof Spoiler => $allowTelegramTags ? '</tg-spoiler>' : '',
|
||||||
$entity instanceof CustomEmoji => $allowTelegramTags ? "</tg-emoji>" : '',
|
$entity instanceof CustomEmoji => $allowTelegramTags ? "</tg-emoji>" : '',
|
||||||
$entity instanceof MentionName => $allowTelegramTags ? '</a>' : '',
|
$entity instanceof MentionName => $allowTelegramTags ? '</a>' : '',
|
||||||
|
$entity instanceof InputMentionName => $allowTelegramTags ? '</a>' : '',
|
||||||
default => '',
|
default => '',
|
||||||
} . ($insertions[$offset] ?? '');
|
} . ($insertions[$offset] ?? '');
|
||||||
}
|
}
|
||||||
@ -274,6 +277,15 @@ abstract class StrTools extends Extension
|
|||||||
{
|
{
|
||||||
return str_replace('```', '\\```', $what);
|
return str_replace('```', '\\```', $what);
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Escape string for markdown code section.
|
||||||
|
*
|
||||||
|
* @param string $what String to escape
|
||||||
|
*/
|
||||||
|
public static function markdownCodeEscape(string $what): string
|
||||||
|
{
|
||||||
|
return str_replace('`', '\\`', $what);
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Escape string for URL.
|
* Escape string for URL.
|
||||||
*
|
*
|
||||||
|
@ -232,6 +232,10 @@ trait BotAPI
|
|||||||
unset($data['_']);
|
unset($data['_']);
|
||||||
$data['type'] = 'phone_number';
|
$data['type'] = 'phone_number';
|
||||||
return $data;
|
return $data;
|
||||||
|
case 'messageEntityBlockquote':
|
||||||
|
unset($data['_']);
|
||||||
|
$data['type'] = 'block_quote';
|
||||||
|
return $data;
|
||||||
case 'messageEntityMention':
|
case 'messageEntityMention':
|
||||||
unset($data['_']);
|
unset($data['_']);
|
||||||
$data['type'] = 'mention';
|
$data['type'] = 'mention';
|
||||||
|
@ -98,24 +98,30 @@ final class MarkdownEntities extends Entities
|
|||||||
}
|
}
|
||||||
$offset++;
|
$offset++;
|
||||||
$char = "](";
|
$char = "](";
|
||||||
} elseif ($char === '`' && $next === '`' && ($markdown[$offset+1] ?? '') === '`') {
|
} elseif ($char === '`') {
|
||||||
$message .= $piece;
|
$message .= $piece;
|
||||||
$messageLen += StrTools::mbStrlen($piece);
|
$messageLen += StrTools::mbStrlen($piece);
|
||||||
|
|
||||||
$offset += 2;
|
$token = '`';
|
||||||
$langLen = strcspn($markdown, "\n ", $offset);
|
$language = null;
|
||||||
$language = substr($markdown, $offset, $langLen);
|
if ($next === '`' && ($markdown[$offset+1] ?? '') === '`') {
|
||||||
$offset += $langLen;
|
$token = '```';
|
||||||
if ($markdown[$offset] === "\n") {
|
|
||||||
$offset++;
|
$offset += 2;
|
||||||
|
$langLen = strcspn($markdown, "\n ", $offset);
|
||||||
|
$language = substr($markdown, $offset, $langLen);
|
||||||
|
$offset += $langLen;
|
||||||
|
if ($markdown[$offset] === "\n") {
|
||||||
|
$offset++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$piece = '';
|
$piece = '';
|
||||||
$posClose = $offset;
|
$posClose = $offset;
|
||||||
while (($posClose = strpos($markdown, '```', $posClose)) !== false) {
|
while (($posClose = strpos($markdown, $token, $posClose)) !== false) {
|
||||||
if ($markdown[$posClose-1] === '\\') {
|
if ($markdown[$posClose-1] === '\\') {
|
||||||
$piece .= substr($markdown, $offset, ($posClose-$offset)-1)."```";
|
$piece .= substr($markdown, $offset, ($posClose-$offset)-1).$token;
|
||||||
$posClose += 3;
|
$posClose += \strlen($token);
|
||||||
$offset = $posClose;
|
$offset = $posClose;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -144,14 +150,17 @@ final class MarkdownEntities extends Entities
|
|||||||
}
|
}
|
||||||
if ($pieceLen > 0) {
|
if ($pieceLen > 0) {
|
||||||
$entities []= [
|
$entities []= [
|
||||||
'_' => 'messageEntityPre',
|
'_' => match ($token) {
|
||||||
|
'```' => 'messageEntityPre',
|
||||||
|
'`' => 'messageEntityCode',
|
||||||
|
},
|
||||||
'language' => $language,
|
'language' => $language,
|
||||||
'offset' => $start,
|
'offset' => $start,
|
||||||
'length' => $pieceLen,
|
'length' => $pieceLen,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
$offset = $posClose+3;
|
$offset = $posClose+\strlen($token);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -218,6 +218,11 @@ class EntitiesTest extends MadelineTestCase
|
|||||||
'length' => 9,
|
'length' => 9,
|
||||||
'type' => 'underline',
|
'type' => 'underline',
|
||||||
],
|
],
|
||||||
|
[
|
||||||
|
'offset' => 49,
|
||||||
|
'length' => 10,
|
||||||
|
'type' => 'block_quote',
|
||||||
|
],
|
||||||
[
|
[
|
||||||
'offset' => 60,
|
'offset' => 60,
|
||||||
'length' => 18,
|
'length' => 18,
|
||||||
@ -244,7 +249,7 @@ class EntitiesTest extends MadelineTestCase
|
|||||||
'type' => 'spoiler',
|
'type' => 'spoiler',
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
'<b>test</b><br><i>test</i> <code>test</code> <pre language="html">test</pre> <a href="https://example.com/">test</a> <s>strikethrough</s> <u>underline</u> blockquote <a href="https://google.com">https://google.com</a> <a href="mailto:daniil@daniil.it">daniil@daniil.it</a> <a href="phone:+39398172758722">+39398172758722</a> <a href="https://t.me/daniilgentili">@daniilgentili</a> <tg-spoiler>spoiler</tg-spoiler> <b>not_bold</b>',
|
'<b>test</b><br><i>test</i> <code>test</code> <pre language="html">test</pre> <a href="https://example.com/">test</a> <s>strikethrough</s> <u>underline</u> <blockquote>blockquote</blockquote> <a href="https://google.com">https://google.com</a> <a href="mailto:daniil@daniil.it">daniil@daniil.it</a> <a href="phone:+39398172758722">+39398172758722</a> <a href="https://t.me/daniilgentili">@daniilgentili</a> <tg-spoiler>spoiler</tg-spoiler> <b>not_bold</b>',
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
'markdown',
|
'markdown',
|
||||||
@ -346,14 +351,26 @@ class EntitiesTest extends MadelineTestCase
|
|||||||
],
|
],
|
||||||
[
|
[
|
||||||
'markdown',
|
'markdown',
|
||||||
"```\n".StrTools::markdownCodeblockEscape('\\ ```').'```',
|
"```\na_b\n".StrTools::markdownCodeblockEscape('\\ ```').'```',
|
||||||
'\\ ```',
|
"a_b\n\\ ```",
|
||||||
|
[
|
||||||
|
[
|
||||||
|
'offset' => 0,
|
||||||
|
'length' => 9,
|
||||||
|
'type' => 'pre',
|
||||||
|
'language' => '',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'markdown',
|
||||||
|
'`a_b '.StrTools::markdownCodeEscape('`').'`',
|
||||||
|
'a_b `',
|
||||||
[
|
[
|
||||||
[
|
[
|
||||||
'offset' => 0,
|
'offset' => 0,
|
||||||
'length' => 5,
|
'length' => 5,
|
||||||
'type' => 'pre',
|
'type' => 'code',
|
||||||
'language' => '',
|
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
|
Loading…
Reference in New Issue
Block a user