Skip to content

Commit 4d4eb6d

Browse files
authored
Merge pull request witnessmenow#29 from rusboard/master
Changes for sendImageFileByID
2 parents 0bc94f3 + 480320b commit 4d4eb6d

File tree

4 files changed

+30
-28
lines changed

4 files changed

+30
-28
lines changed

examples/ESP8266/SendPhoto/PhotoFromFileID/PhotoFromFileID.ino

Lines changed: 25 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,30 @@ 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+
if (bot.checkForOkResponse(response)) {
42+
DynamicJsonBuffer jsonBuffer;
43+
JsonObject& images = jsonBuffer.parseObject(response);
44+
45+
// There are 3 image sizes after Telegram has process photo
46+
// You may choose what you want, in example was choosed bigger size
47+
int photosArrayLength = images["result"]["photo"].size();
48+
49+
if (photosArrayLength > 0) {
50+
String file_id = images["result"]["photo"][photosArrayLength-1]["file_id"];
51+
52+
if (file_id) {
53+
String send_photo_by_file_id_response = bot.sendPhoto(chat_id, file_id, "This photo was sent using File ID");
54+
55+
if (bot.checkForOkResponse(send_photo_by_file_id_response)) {
56+
// do something
57+
} else {
58+
// or not to do
59+
}
60+
}
61+
}
62+
}
4063
}
4164

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

examples/ESP8266/SendPhoto/PhotoFromSD/PhotoFromSD.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void loop() {
7878
Serial.print("....");
7979

8080
//Content type for PNG image/png
81-
bool sent = bot.sendPhotoByBinary(chat_id, "image/jpeg", myFile.size(),
81+
String sent = bot.sendPhotoByBinary(chat_id, "image/jpeg", myFile.size(),
8282
isMoreDataAvailable,
8383
getNextByte);
8484

src/UniversalTelegramBot.cpp

Lines changed: 2 additions & 22 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,
@@ -511,12 +506,7 @@ String UniversalTelegramBot::sendPhotoByBinary(String chat_id, String contentTyp
511506

512507
if (_debug) Serial.println(response);
513508

514-
if(checkForOkResponse(response))
515-
{
516-
return extractFileIdFromResponse(response);
517-
}
518-
519-
return "";
509+
return response;
520510
}
521511

522512
String UniversalTelegramBot::sendPhoto(String chat_id, String photo, String caption, bool disable_notification, int reply_to_message_id, String keyboard) {
@@ -549,16 +539,6 @@ String UniversalTelegramBot::sendPhoto(String chat_id, String photo, String capt
549539
return sendPostPhoto(payload);
550540
}
551541

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-
562542
bool UniversalTelegramBot::checkForOkResponse(String response) {
563543
int responseLength = response.length();
564544

src/UniversalTelegramBot.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,11 @@ class UniversalTelegramBot
7070
String sendPhotoByBinary(String chat_id, String contentType, int fileSize,
7171
MoreDataAvailable moreDataAvailableCallback,
7272
GetNextByte getNextByteCallback);
73-
String sendPhoto(String chat_id, String photo, String caption,
73+
String sendPhoto(String chat_id, String photo, String caption = "",
7474
bool disable_notification = false, int reply_to_message_id = 0, String keyboard = "");
7575

7676
int getUpdates(long offset);
77+
bool checkForOkResponse(String response);
7778
telegramMessage messages[HANDLE_MESSAGES];
7879
long last_message_received;
7980
String name;
@@ -84,8 +85,6 @@ class UniversalTelegramBot
8485
String _token;
8586
Client *client;
8687
const int maxMessageLength = 1300;
87-
bool checkForOkResponse(String response);
88-
String extractFileIdFromResponse(String response);
8988
bool _debug = false;
9089
};
9190

0 commit comments

Comments
 (0)