@@ -686,6 +686,10 @@ private static function ensureValidAction(string $action): void
686
686
* Use this method to send text messages. On success, the last sent Message is returned
687
687
*
688
688
* All message responses are saved in `$extras['responses']`.
689
+ * Custom encoding can be defined in `$extras['encoding']` (default: `mb_internal_encoding()`)
690
+ * Custom splitting can be defined in `$extras['split']` (default: 4096)
691
+ * `$extras['split'] = null;` // force to not split message at all!
692
+ * `$extras['split'] = 200;` // split message into 200 character chunks
689
693
*
690
694
* @link https://core.telegram.org/bots/api#sendmessage
691
695
*
@@ -699,19 +703,24 @@ private static function ensureValidAction(string $action): void
699
703
*/
700
704
public static function sendMessage (array $ data , ?array &$ extras = []): ServerResponse
701
705
{
706
+ $ extras = array_merge ([
707
+ 'split ' => 4096 ,
708
+ 'encoding ' => mb_internal_encoding (),
709
+ ], (array ) $ extras );
710
+
702
711
$ text = $ data ['text ' ];
703
- $ max_length = 4096 ;
712
+ $ encoding = $ extras ['encoding ' ];
713
+ $ max_length = $ extras ['split ' ] ?: mb_strlen ($ text , $ encoding );
704
714
705
- $ extras = (array ) $ extras ;
706
715
$ responses = [];
707
716
708
717
do {
709
718
// Chop off and send the first message.
710
- $ data ['text ' ] = mb_substr ($ text , 0 , $ max_length );
719
+ $ data ['text ' ] = mb_substr ($ text , 0 , $ max_length, $ encoding );
711
720
$ responses [] = self ::send ('sendMessage ' , $ data );
712
721
713
722
// Prepare the next message.
714
- $ text = mb_substr ($ text , $ max_length );
723
+ $ text = mb_substr ($ text , $ max_length, null , $ encoding );
715
724
} while ($ text !== '' );
716
725
717
726
// Add all response objects to referenced variable.
0 commit comments