1
0
mirror of https://github.com/danog/MadelineProto.git synced 2024-11-30 04:08:59 +01:00

Improve message splitting logic
Some checks failed
ci/woodpecker/push/build Pipeline failed
ci/woodpecker/tag/build Pipeline was successful
ci/woodpecker/tag/deploy Pipeline was successful

This commit is contained in:
Daniil Gentili 2023-01-12 14:58:14 +01:00
parent 43c68b3a46
commit d0a0b17b8b
4 changed files with 53 additions and 54 deletions

View File

@ -392,6 +392,9 @@ trait BotAPI
}
return $arguments;
}
private const MAX_ENTITY_LENGTH = 100;
private const MAX_ENTITY_SIZE = 8110;
/**
* Split too long message into chunks.
*
@ -402,34 +405,41 @@ trait BotAPI
public function splitToChunks($args): array
{
$args = $this->parseMode($args);
if (!isset($args['entities'])) {
$args['entities'] = [];
}
$args['entities'] ??= [];
var_dump($args);
// UTF-8 length, not UTF-16
$max_length = isset($args['media']) ? $this->config['caption_length_max'] : $this->config['message_length_max'];
$max_entity_length = 100;
$max_entity_size = 8110;
$text_arr = [];
foreach ($this->multipleExplodeKeepDelimiters(["\n"], $args['message']) as $word) {
if (\mb_strlen($word, 'UTF-8') > $max_length) {
foreach (StrTools::mbStrSplit($word, $max_length) as $vv) {
$text_arr[] = $vv;
}
} else {
$text_arr[] = $word;
}
}
$cur_len = 0;
$cur = '';
$multiple_args_base = \array_merge($args, ['entities' => [], 'parse_mode' => 'text', 'message' => '']);
$multiple_args = [$multiple_args_base];
$i = 0;
foreach ($text_arr as $word) {
if (StrTools::mbStrlen($multiple_args[$i]['message'].$word) <= $max_length) {
$multiple_args[$i]['message'] .= $word;
} else {
$i++;
$multiple_args[$i] = $multiple_args_base;
$multiple_args[$i]['message'] .= $word;
$multiple_args = [];
foreach (\explode("\n", $args['message']) as $word) {
foreach (\mb_str_split($word."\n", $max_length, 'UTF-8') as $vv) {
$len = \mb_strlen($vv, 'UTF-8');
if ($cur_len + $len <= $max_length) {
$cur .= $vv;
$cur_len += $len;
} else {
if (\trim($cur) !== '') {
$multiple_args[] = [
...$multiple_args_base,
'message' => $cur
];
}
$cur = $vv;
$cur_len = $len;
}
}
}
if (\trim($cur) !== '') {
$multiple_args[] = [
...$multiple_args_base,
'message' => $cur
];
}
var_dump($multiple_args);
$i = 0;
$offset = 0;
for ($k = 0; $k < \count($args['entities']); $k++) {
@ -480,8 +490,8 @@ trait BotAPI
}
$total = 0;
foreach ($multiple_args as $args) {
if (\count($args['entities']) > $max_entity_length) {
$total += \count($args['entities']) - $max_entity_length;
if (\count($args['entities']) > self::MAX_ENTITY_LENGTH) {
$total += \count($args['entities']) - self::MAX_ENTITY_LENGTH;
}
$c = 0;
foreach ($args['entities'] as $entity) {
@ -489,7 +499,7 @@ trait BotAPI
$c += \strlen($entity['url']);
}
}
if ($c >= $max_entity_size) {
if ($c >= self::MAX_ENTITY_SIZE) {
$this->logger->logger('Entity size limit possibly exceeded, you may get an error indicating that the entities are too long. Reduce the number of entities and/or size of the URLs used.', Logger::FATAL_ERROR);
}
}
@ -498,25 +508,6 @@ trait BotAPI
}
return $multiple_args;
}
/**
* @return string[]
*
* @psalm-return list<string>
*/
private function multipleExplodeKeepDelimiters($delimiters, $string): array
{
$initialArray = \explode(\chr(1), \str_replace($delimiters, \chr(1), $string));
$finalArray = [];
/** @var int */
$delimOffset = 0;
foreach ($initialArray as $item) {
$delimOffset += StrTools::mbStrlen($item);
/** @var int $delimOffset */
$finalArray[] = $item.($delimOffset < StrTools::mbStrlen($string) ? $string[$delimOffset] : '');
$delimOffset++;
}
return $finalArray;
}
/**
* @return ((array|string)[][]|string)[]
*

View File

@ -43,15 +43,15 @@ trait Ads
if ($cache && $cache[0] > \time()) {
return $cache[1];
}
$result = (yield from $this->methodCallAsyncRead('channels.getSponsoredMessages', ['channel' => $peer]));
if (array_key_exists('messages', $result)) {
$result = $result['messages'];
} else {
$result = null;
}
$result = (yield from $this->methodCallAsyncRead('channels.getSponsoredMessages', ['channel' => $peer]));
if (\array_key_exists('messages', $result)) {
$result = $result['messages'];
} else {
$result = null;
}
$this->sponsoredMessages->set($peer, [\time() + 5*60, $result]);
return $result;
$this->sponsoredMessages->set($peer, [\time() + 5*60, $result]);
return $result;
}
/**
* Mark sponsored message as read.

View File

@ -293,6 +293,9 @@ $MadelineProto->loop(function () use ($MadelineProto) {
$sentMessage = yield $MadelineProto->messages->sendMessage(['peer' => $peer, 'message' => $message, 'entities' => [['_' => 'inputMessageEntityMentionName', 'offset' => 0, 'length' => mb_strlen($message), 'user_id' => $mention]]]);
$MadelineProto->logger($sentMessage, \danog\MadelineProto\Logger::NOTICE);
$sentMessage = yield $MadelineProto->messages->sendMessage(['peer' => $peer, 'message' => str_repeat('a', 4096*4)]);
$MadelineProto->logger($sentMessage, \danog\MadelineProto\Logger::NOTICE);
foreach ($media as $type => $inputMedia) {
if ($type !== 'sticker' && $type !== 'voice') {
$MadelineProto->logger("Sending multi $type");

View File

@ -4,5 +4,10 @@
"ennexa/amp-update-cache": "dev-master",
"phpunit/phpunit": "^9",
"amphp/php-cs-fixer-config": "v2.x-dev"
},
"config": {
"allow-plugins": {
"dealerdirect/phpcodesniffer-composer-installer": true
}
}
}