Skip to content

Commit b748699

Browse files
committed
Return all sendMessage response objects in referenced array variable
1 parent f3e7790 commit b748699

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
77
### Notes
88
- [:ledger: View file changes][Unreleased]
99
### Added
10+
- Extra parameter for `Request::sendMessage()` to return all response objects for split messages.
1011
### Changed
1112
### Deprecated
1213
### Removed

src/Request.php

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -683,29 +683,41 @@ private static function ensureValidAction(string $action): void
683683
}
684684

685685
/**
686-
* Use this method to send text messages. On success, the sent Message is returned
686+
* Use this method to send text messages. On success, the last sent Message is returned
687+
*
688+
* All message responses are saved in `$extras['responses']`.
687689
*
688690
* @link https://core.telegram.org/bots/api#sendmessage
689691
*
690-
* @param array $data
692+
* @todo Splitting formatted text may break the message.
693+
*
694+
* @param array $data
695+
* @param array|null $extras
691696
*
692697
* @return ServerResponse
693698
* @throws TelegramException
694699
*/
695-
public static function sendMessage(array $data): ServerResponse
700+
public static function sendMessage(array $data, ?array &$extras = []): ServerResponse
696701
{
697-
$text = $data['text'];
702+
$text = $data['text'];
703+
$max_length = 4096;
704+
705+
$extras = (array) $extras;
706+
$responses = [];
698707

699708
do {
700-
//Chop off and send the first message
701-
$data['text'] = mb_substr($text, 0, 4096);
702-
$response = self::send('sendMessage', $data);
709+
// Chop off and send the first message.
710+
$data['text'] = mb_substr($text, 0, $max_length);
711+
$responses[] = self::send('sendMessage', $data);
703712

704-
//Prepare the next message
705-
$text = mb_substr($text, 4096);
706-
} while (mb_strlen($text, 'UTF-8') > 0);
713+
// Prepare the next message.
714+
$text = mb_substr($text, $max_length);
715+
} while ($text !== '');
707716

708-
return $response;
717+
// Add all response objects to referenced variable.
718+
$extras['responses'] = $responses;
719+
720+
return end($responses);
709721
}
710722

711723
/**
@@ -717,7 +729,7 @@ public static function sendMessage(array $data): ServerResponse
717729
* @return ServerResponse
718730
* @throws TelegramException
719731
*/
720-
public static function __callStatic(string $action, array $data)
732+
public static function __callStatic(string $action, array $data): ServerResponse
721733
{
722734
// Only argument should be the data array, ignore any others.
723735
return static::send($action, reset($data) ?: []);

0 commit comments

Comments
 (0)