Skip to content

Commit dad0ff6

Browse files
authored
Update UniversalTelegramBot.cpp
added two functions to support serial read of photo from camera
1 parent ba04372 commit dad0ff6

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

src/UniversalTelegramBot.cpp

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,128 @@ String UniversalTelegramBot::sendMultipartFormDataToTelegram(
312312
return body;
313313
}
314314

315+
String UniversalTelegramBot::sendMultipartFormSerialDataToTelegram(String command, String binaryProperyName,
316+
String fileName, String contentType,
317+
String chat_id, int fileSize,
318+
MoreDataAvailable moreDataAvailableCallback,
319+
GetNextBuffer getNextBufferCallback,
320+
GetNextBufferLen getNextBufferLenCallback) {
321+
322+
String body = "";
323+
String headers = "";
324+
long now;
325+
bool responseReceived;
326+
String boundry = "------------------------b8f610217e83e29b";
327+
// Connect with api.telegram.org
328+
if (client->connect(HOSTS5, S5_PORT)) {
329+
330+
String start_request = "";
331+
String end_request = "";
332+
333+
start_request = start_request + "--" + boundry + "\r\n";
334+
start_request = start_request + "content-disposition: form-data; name=\"chat_id\"" + "\r\n";
335+
start_request = start_request + "\r\n";
336+
start_request = start_request + chat_id + "\r\n";
337+
338+
start_request = start_request + "--" + boundry + "\r\n";
339+
start_request = start_request + "content-disposition: form-data; name=\"" + binaryProperyName + "\"; filename=\"" + fileName + "\"" + "\r\n";
340+
start_request = start_request + "Content-Type: " + contentType + "\r\n";
341+
start_request = start_request + "\r\n";
342+
343+
344+
end_request = end_request + "\r\n";
345+
end_request = end_request + "--" + boundry + "--" + "\r\n";
346+
347+
client->print("POST /bot"+_token+"/" + command); client->println(" HTTP/1.1");
348+
// Host header
349+
client->print("Host: "); client->println(HOST);
350+
client->println("User-Agent: arduino/1.0");
351+
client->println("Accept: */*");
352+
353+
int contentLength = fileSize + start_request.length() + end_request.length();
354+
if (_debug) Serial.println("Content-Length: " + String(contentLength));
355+
client->print("Content-Length: "); client->println(String(contentLength));
356+
client->println("Content-Type: multipart/form-data; boundary=" + boundry);
357+
client->println("");
358+
359+
client->print(start_request);
360+
361+
if (_debug) Serial.print("Start request: " + start_request);
362+
363+
while (moreDataAvailableCallback()) {
364+
client->write((const uint8_t *)getNextBufferCallback(), getNextBufferLenCallback());
365+
}
366+
367+
client->print(end_request);
368+
if (_debug) Serial.print(end_request);
369+
370+
int count = 0;
371+
int ch_count=0;
372+
char c;
373+
now=millis();
374+
bool finishedHeaders = false;
375+
bool currentLineIsBlank = true;
376+
while (millis()-now<30000) {
377+
while (client->available()) {
378+
char c = client->read();
379+
responseReceived=true;
380+
381+
382+
if(!finishedHeaders){
383+
if (currentLineIsBlank && c == '\n') {
384+
finishedHeaders = true;
385+
}
386+
else {
387+
headers = headers + c;
388+
}
389+
} else {
390+
if (ch_count < maxMessageLength) {
391+
body=body+c;
392+
ch_count++;
393+
}
394+
}
395+
396+
if (c == '\n') {
397+
currentLineIsBlank = true;
398+
}else if (c != '\r') {
399+
currentLineIsBlank = false;
400+
}
401+
402+
}
403+
404+
if (responseReceived) {
405+
if (_debug) {
406+
Serial.println();
407+
Serial.println(body);
408+
Serial.println();
409+
}
410+
break;
411+
}
412+
}
413+
}
414+
415+
return body;
416+
}
417+
418+
419+
420+
String UniversalTelegramBot::sendPhotoFromBuffer(String chat_id, String contentType, int fileSize,
421+
MoreDataAvailable moreDataAvailableCallback,
422+
GetNextBuffer getNextBufferCallback,
423+
GetNextBufferLen getNextBufferLenCallback) {
424+
425+
if (_debug) Serial.println("SEND Photo");
426+
427+
String response = sendMultipartFormSerialDataToTelegram("sendPhoto", "photo", "img.jpg",
428+
contentType, chat_id, fileSize,
429+
moreDataAvailableCallback, getNextBufferCallback, getNextBufferLenCallback);
430+
431+
if (_debug) Serial.println(response);
432+
433+
return response;
434+
}
435+
436+
315437
bool UniversalTelegramBot::getMe() {
316438
String command = "bot" + _token + "/getMe";
317439
String response =

0 commit comments

Comments
 (0)