Skip to content

Commit 0ca61fb

Browse files
committed
Read all response from API when sending image
1 parent adc8984 commit 0ca61fb

File tree

3 files changed

+14
-19
lines changed

3 files changed

+14
-19
lines changed

examples/ESP8266/SendPhoto/PhotoFromFileID/PhotoFromFileID.ino

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <ESP8266WiFi.h>
88
#include <WiFiClientSecure.h>
99
#include <UniversalTelegramBot.h>
10+
#include <ArduinoJson.h>
1011

1112
// Initialize Wifi connection to the router
1213
char ssid[] = "XXXXXX"; // your network SSID (name)
@@ -35,8 +36,18 @@ void handleNewMessages(int numNewMessages) {
3536
if (from_name == "") from_name = "Guest";
3637

3738
if (text == "/get_test_photo") {
38-
String file_id = bot.sendPhoto(chat_id, test_photo_url, "This photo was sent using URL");
39-
bot.sendPhoto(chat_id, file_id, "This photo was sent using File ID");
39+
String response = bot.sendPhoto(chat_id, test_photo_url, "This photo was sent using URL");
40+
41+
DynamicJsonBuffer jsonBuffer;
42+
JsonObject& images = jsonBuffer.parseObject(response);
43+
44+
// There are 3 image sizes after Telegram has process photo
45+
// You may choose what you want, in example was choosed bigger size
46+
String file_id = images["result"]["photo"][2]["file_id"];
47+
48+
if (file_id) {
49+
bot.sendPhoto(chat_id, file_id, "This photo was sent using File ID");
50+
}
4051
}
4152

4253
if (text == "/start") {

src/UniversalTelegramBot.cpp

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -491,12 +491,7 @@ String UniversalTelegramBot::sendPostPhoto(JsonObject& payload) {
491491
}
492492
}
493493

494-
if(sent)
495-
{
496-
return extractFileIdFromResponse(response);
497-
}
498-
499-
return "";
494+
return response;
500495
}
501496

502497
String UniversalTelegramBot::sendPhotoByBinary(String chat_id, String contentType, int fileSize,
@@ -549,16 +544,6 @@ String UniversalTelegramBot::sendPhoto(String chat_id, String photo, String capt
549544
return sendPostPhoto(payload);
550545
}
551546

552-
String UniversalTelegramBot::extractFileIdFromResponse(String response) {
553-
int indexOfFileId = response.lastIndexOf("\"file_id\":\"");
554-
if(indexOfFileId > -1){
555-
// 11 is the length of "file_id":""
556-
// 55 is the length of the file_id
557-
return response.substring(indexOfFileId + 11, indexOfFileId + 11 + 55);
558-
}
559-
return "";
560-
}
561-
562547
bool UniversalTelegramBot::checkForOkResponse(String response) {
563548
int responseLength = response.length();
564549

src/UniversalTelegramBot.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ class UniversalTelegramBot
8585
Client *client;
8686
const int maxMessageLength = 1300;
8787
bool checkForOkResponse(String response);
88-
String extractFileIdFromResponse(String response);
8988
bool _debug = false;
9089
};
9190

0 commit comments

Comments
 (0)