1212 */
1313class TelegramChannel
1414{
15- /**
16- * @var Telegram
17- */
18- protected $ telegram ;
19-
2015 /**
2116 * @var Dispatcher
2217 */
@@ -25,9 +20,8 @@ class TelegramChannel
2520 /**
2621 * Channel constructor.
2722 */
28- public function __construct (Telegram $ telegram , Dispatcher $ dispatcher )
23+ public function __construct (Dispatcher $ dispatcher )
2924 {
30- $ this ->telegram = $ telegram ;
3125 $ this ->dispatcher = $ dispatcher ;
3226 }
3327
@@ -58,56 +52,11 @@ public function send($notifiable, Notification $notification): ?array
5852 }
5953
6054 if ($ message ->hasToken ()) {
61- $ this ->telegram ->setToken ($ message ->token );
55+ $ message ->telegram ->setToken ($ message ->token );
6256 }
6357
64- $ params = $ message ->toArray ();
65-
66- $ sendMethod = str_replace ('Telegram ' , 'send ' , array_reverse (explode ('\\' , get_class ($ message )))[0 ]);
67-
6858 try {
69- if ($ message instanceof TelegramMessage) {
70- if ($ message ->shouldChunk ()) {
71- $ replyMarkup = $ message ->getPayloadValue ('reply_markup ' );
72-
73- if ($ replyMarkup ) {
74- unset($ params ['reply_markup ' ]);
75- }
76-
77- $ messages = $ this ->chunk ($ message ->getPayloadValue ('text ' ), $ message ->chunkSize );
78-
79- $ payloads = collect ($ messages )->filter ()->map (function ($ text ) use ($ params ) {
80- return array_merge ($ params , ['text ' => $ text ]);
81- });
82-
83- if ($ replyMarkup ) {
84- $ lastMessage = $ payloads ->pop ();
85- $ lastMessage ['reply_markup ' ] = $ replyMarkup ;
86- $ payloads ->push ($ lastMessage );
87- }
88-
89- return $ payloads ->map (function ($ payload ) {
90- $ response = $ this ->telegram ->sendMessage ($ payload );
91-
92- // To avoid rate limit of one message per second.
93- sleep (1 );
94-
95- if ($ response ) {
96- return json_decode ($ response ->getBody ()->getContents (), true );
97- }
98-
99- return $ response ;
100- })->toArray ();
101- }
102-
103- $ response = $ this ->telegram ->sendMessage ($ params );
104- } elseif ($ message instanceof TelegramFile) {
105- $ response = $ this ->telegram ->sendFile ($ params , $ message ->type , $ message ->hasFile ());
106- } elseif (method_exists ($ this ->telegram , $ sendMethod )) {
107- $ response = $ this ->telegram ->{$ sendMethod }($ params );
108- } else {
109- return null ;
110- }
59+ $ response = $ message ->send ();
11160 } catch (CouldNotSendNotification $ exception ) {
11261 $ this ->dispatcher ->dispatch (new NotificationFailed (
11362 $ notifiable ,
@@ -121,27 +70,4 @@ public function send($notifiable, Notification $notification): ?array
12170
12271 return json_decode ($ response ->getBody ()->getContents (), true );
12372 }
124-
125- /**
126- * Chunk the given string into an array of strings.
127- */
128- public function chunk (string $ value , int $ limit = 4096 ): array
129- {
130- if (mb_strwidth ($ value , 'UTF-8 ' ) <= $ limit ) {
131- return [$ value ];
132- }
133-
134- if ($ limit >= 4097 ) {
135- $ limit = 4096 ;
136- }
137-
138- $ output = explode ('%#TGMSG#% ' , wordwrap ($ value , $ limit , '%#TGMSG#% ' ));
139-
140- // Fallback for when the string is too long and wordwrap doesn't cut it.
141- if (count ($ output ) <= 1 ) {
142- $ output = mb_str_split ($ value , $ limit , 'UTF-8 ' );
143- }
144-
145- return $ output ;
146- }
14773}
0 commit comments