@@ -683,29 +683,41 @@ private static function ensureValidAction(string $action): void
683
683
}
684
684
685
685
/**
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']`.
687
689
*
688
690
* @link https://core.telegram.org/bots/api#sendmessage
689
691
*
690
- * @param array $data
692
+ * @todo Splitting formatted text may break the message.
693
+ *
694
+ * @param array $data
695
+ * @param array|null $extras
691
696
*
692
697
* @return ServerResponse
693
698
* @throws TelegramException
694
699
*/
695
- public static function sendMessage (array $ data ): ServerResponse
700
+ public static function sendMessage (array $ data, ? array & $ extras = [] ): ServerResponse
696
701
{
697
- $ text = $ data ['text ' ];
702
+ $ text = $ data ['text ' ];
703
+ $ max_length = 4096 ;
704
+
705
+ $ extras = (array ) $ extras ;
706
+ $ responses = [];
698
707
699
708
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 );
703
712
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 !== '' );
707
716
708
- return $ response ;
717
+ // Add all response objects to referenced variable.
718
+ $ extras ['responses ' ] = $ responses ;
719
+
720
+ return end ($ responses );
709
721
}
710
722
711
723
/**
@@ -717,7 +729,7 @@ public static function sendMessage(array $data): ServerResponse
717
729
* @return ServerResponse
718
730
* @throws TelegramException
719
731
*/
720
- public static function __callStatic (string $ action , array $ data )
732
+ public static function __callStatic (string $ action , array $ data ): ServerResponse
721
733
{
722
734
// Only argument should be the data array, ignore any others.
723
735
return static ::send ($ action , reset ($ data ) ?: []);
0 commit comments