@@ -119,17 +119,19 @@ String UniversalTelegramBot::sendPostToTelegram(String command, JsonObject& payl
119
119
return response;
120
120
}
121
121
122
- String UniversalTelegramBot::sendImageFromFileToTelegram (String chat_id, int fileSize, MoreDataAvailable moreDataAvailableCallback, GetNextByte getNextByteCallback){
122
+ String UniversalTelegramBot::sendMultipartFormDataToTelegram (String command, String binaryProperyName,
123
+ String fileName, String contentType,
124
+ String chat_id, int fileSize,
125
+ MoreDataAvailable moreDataAvailableCallback,
126
+ GetNextByte getNextByteCallback) {
123
127
124
- Serial.println (" sendImageFromFileToTelegram" );
125
128
String response = " " ;
126
129
long now;
127
130
bool responseRecieved;
128
131
String boundry = " ------------------------b8f610217e83e29b" ;
129
132
// Connect with api.telegram.org
130
133
if (client->connect (HOST, SSL_PORT)) {
131
- Serial.println (" connected" );
132
- // POST URI
134
+
133
135
String start_request = " " ;
134
136
String end_request = " " ;
135
137
@@ -139,30 +141,30 @@ String UniversalTelegramBot::sendImageFromFileToTelegram(String chat_id, int fil
139
141
start_request = start_request + chat_id + " \r\n " ;
140
142
141
143
start_request = start_request + " --" + boundry + " \r\n " ;
142
- start_request = start_request + " content-disposition: form-data; name=\" photo \" ; filename=\" img.jpg \" " + " \r\n " ;
143
- start_request = start_request + " Content-Type: image/jpeg " + " \r\n " ;
144
+ start_request = start_request + " content-disposition: form-data; name=\" " + binaryProperyName + " \" ; filename=\" " + fileName + " \" " + " \r\n " ;
145
+ start_request = start_request + " Content-Type: " + contentType + " \r\n " ;
144
146
start_request = start_request + " \r\n " ;
145
147
146
148
147
149
end_request = end_request + " \r\n " ;
148
150
end_request = end_request + " --" + boundry + " --" + " \r\n " ;
149
151
150
- client->print (" POST /bot" +_token+" /sendPhoto " ); client->println (" HTTP/1.1" );
152
+ client->print (" POST /bot" +_token+" /" + command ); client->println (" HTTP/1.1" );
151
153
// Host header
152
154
client->print (" Host: " ); client->println (HOST);
153
155
client->println (" User-Agent: arduino/1.0" );
154
156
client->println (" Accept: */*" );
155
157
156
158
int contentLength = fileSize + start_request.length () + end_request.length ();
157
- Serial.println (" Content-Length: " + String (contentLength));
159
+ if (_debug) Serial.println (" Content-Length: " + String (contentLength));
158
160
client->print (" Content-Length: " ); client->println (String (contentLength));
159
161
client->println (" Content-Type: multipart/form-data; boundary=" + boundry);
160
162
client->println (" " );
161
163
162
164
client->print (start_request);
163
- Serial.print (start_request);
164
165
165
- Serial.println (" Sending...." );
166
+ if (_debug) Serial.print (start_request);
167
+
166
168
byte buffer[512 ];
167
169
int count = 0 ;
168
170
char ch;
@@ -173,22 +175,24 @@ String UniversalTelegramBot::sendImageFromFileToTelegram(String chat_id, int fil
173
175
count++;
174
176
if (count == 512 ){
175
177
// yield();
176
- Serial.println (" Sending full buffer" );
178
+ if (_debug) {
179
+ Serial.println (" Sending full buffer" );
180
+ }
177
181
client->write ((const uint8_t *)buffer, 512 );
178
182
count = 0 ;
179
183
}
180
184
}
181
185
182
186
if (count > 0 ) {
183
- Serial.println (" Sending remaining buffer" );
187
+ if (_debug) {
188
+ Serial.println (" Sending remaining buffer" );
189
+ }
184
190
client->write ((const uint8_t *)buffer, count);
185
191
}
186
192
187
193
client->print (end_request);
188
- Serial.print (end_request);
194
+ if (_debug) Serial.print (end_request);
189
195
190
-
191
- Serial.println (" Done" );
192
196
count = 0 ;
193
197
int ch_count=0 ;
194
198
char c;
@@ -205,9 +209,11 @@ String UniversalTelegramBot::sendImageFromFileToTelegram(String chat_id, int fil
205
209
responseRecieved=true ;
206
210
}
207
211
if (responseRecieved) {
208
- Serial.println ();
209
- Serial.println (response);
210
- Serial.println ();
212
+ if (_debug) {
213
+ Serial.println ();
214
+ Serial.println (response);
215
+ Serial.println ();
216
+ }
211
217
break ;
212
218
}
213
219
}
@@ -425,6 +431,21 @@ bool UniversalTelegramBot::sendPostMessage(JsonObject& payload) {
425
431
return sent;
426
432
}
427
433
434
+ bool UniversalTelegramBot::sendImage (String chat_id, String contentType, int fileSize,
435
+ MoreDataAvailable moreDataAvailableCallback,
436
+ GetNextByte getNextByteCallback) {
437
+
438
+ if (_debug) Serial.println (" SEND Photo" );
439
+
440
+ String response = sendMultipartFormDataToTelegram (" sendPhoto" , " photo" , " img.jpg" ,
441
+ contentType, chat_id, fileSize,
442
+ moreDataAvailableCallback, getNextByteCallback);
443
+
444
+ if (_debug) Serial.println (response);
445
+
446
+ return checkForOkResponse (response);
447
+ }
448
+
428
449
bool UniversalTelegramBot::checkForOkResponse (String response) {
429
450
int responseLength = response.length ();
430
451
0 commit comments