@@ -46,7 +46,7 @@ public function getWebhookInfo(): ResponseInterface
4646 return $ this ->httpClient ->post ($ this ->getMethodUrl ('getWebhookInfo ' ));
4747 }
4848
49- public function sendMessage (SendMessage $ sendMessage ): ? Message
49+ public function sendMessage (SendMessage $ sendMessage ): Message
5050 {
5151 $ response = $ this ->httpClient ->post ($ this ->getMethodUrl ('sendMessage ' ), [
5252 'json ' => get_values ($ sendMessage ),
@@ -92,32 +92,60 @@ public function sendPhoto(SendPhoto $sendPhoto): ResponseInterface
9292 ]);
9393 }
9494
95- public function sendDocument (SendDocument $ sendDocument ): ResponseInterface
95+ public function sendDocument (SendDocument $ sendDocument ): Message
9696 {
97- if (strpos ($ sendDocument ->getDocument (), 'http ' ) === 0 ) {
98- return $ this ->httpClient ->post ($ this ->getMethodUrl ('sendDocument ' ), [
99- 'json ' => get_values ($ sendDocument ),
97+ $ doc = $ sendDocument ->getDocument ();
98+
99+ if ($ doc instanceof FileId || $ doc instanceof FileUrl) {
100+ $ data = get_values ($ sendDocument );
101+ $ data ['document ' ] = (string ) $ doc ;
102+
103+ $ response = $ this ->httpClient ->post ($ this ->getMethodUrl ('sendDocument ' ), [
104+ 'json ' => $ data ,
100105 ]);
101- }
102- $ values = get_values ($ sendDocument );
103106
104- $ data [] = [
105- 'name ' => 'document ' ,
106- 'contents ' => $ values ['document ' ],
107- 'filename ' => 'picture.jpg ' ,
108- ];
109- unset($ values ['document ' ]);
107+ $ json = json_decode ((string ) $ response ->getBody (), true );
108+ if (isset ($ json ['ok ' ]) && $ json ['ok ' ]) {
109+ $ message = new Message ();
110+ set_values ($ message , $ json ['result ' ]);
110111
111- foreach ($ values as $ name => $ value ) {
112+ return $ message ;
113+ }
114+
115+ throw new \LogicException ('Unexpected response: ' .(string ) $ response ->getBody ());
116+ }
117+
118+ if ($ doc instanceof InputFile) {
112119 $ data [] = [
113- 'name ' => $ name ,
114- 'contents ' => $ value ,
120+ 'name ' => 'document ' ,
121+ 'contents ' => $ doc ->getContent (),
122+ 'filename ' => $ doc ->getFileName (),
115123 ];
124+
125+ $ values = get_values ($ sendDocument );
126+ foreach ($ values as $ name => $ value ) {
127+ $ data [] = [
128+ 'name ' => $ name ,
129+ 'contents ' => $ value ,
130+ ];
131+ }
132+
133+ $ response = $ this ->httpClient ->post ($ this ->getMethodUrl ('sendDocument ' ), [
134+ 'multipart ' => $ data ,
135+ ]);
136+
137+ $ json = json_decode ((string ) $ response ->getBody (), true );
138+ if (isset ($ json ['ok ' ]) && $ json ['ok ' ]) {
139+ $ message = new Message ();
140+ set_values ($ message , $ json ['result ' ]);
141+
142+ return $ message ;
143+ }
144+
145+ throw new \LogicException ('Unexpected response: ' .(string ) $ response ->getBody ());
116146 }
117147
118- return $ this ->httpClient ->post ($ this ->getMethodUrl ('sendDocument ' ), [
119- 'multipart ' => $ data ,
120- ]);
148+ throw new \LogicException (sprintf ('Unexpected document: %s ' .get_class ($ doc )));
121149 }
122150
123151 public function sendInvoice (SendInvoice $ sendInvoice )
0 commit comments