Skip to content

Commit f6e5e97

Browse files
committed
Abstracting reference to File from sending code
1 parent 3be0372 commit f6e5e97

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

examples/ESP8266/ImageFromSD/ImageFromSD.ino

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@
1313
#include <SPI.h>
1414
#include <SD.h>
1515

16+
bool isMoreDataAvailable();
17+
byte getNextByte();
1618

1719
// Initialize Wifi connection to the router
1820
char ssid[] = "xxxxxxxxxxxxxxxxxxxxxx"; // your network SSID (name)
1921
char password[] = "yyyyyyyy"; // your network key
2022

23+
File myFile;
2124

2225
// Initialize Telegram BOT
2326
#define BOTtoken "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get from Botfather)
@@ -65,6 +68,14 @@ void setup() {
6568

6669
}
6770

71+
bool isMoreDataAvailable(){
72+
return myFile.available();
73+
}
74+
75+
byte getNextByte(){
76+
return myFile.read();
77+
}
78+
6879
void loop() {
6980

7081
if (millis() > Bot_lasttime + Bot_mtbs) {
@@ -76,11 +87,11 @@ void loop() {
7687
JsonObject& payload = jsonBuffer.createObject();
7788
payload["chat_id"] = bot.messages[0].chat_id;
7889

79-
File myFile = SD.open("box.jpg");
90+
myFile = SD.open("box.jpg");
8091
if (myFile) {
8192
Serial.println("box.jpg:");
8293

83-
bot.sendImageFromFileToTelegram(&myFile, bot.messages[0].chat_id);
94+
bot.sendImageFromFileToTelegram(bot.messages[0].chat_id, myFile.size(), isMoreDataAvailable, getNextByte);
8495
// close the file:
8596
myFile.close();
8697
} else {

src/UniversalTelegramBot.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ String UniversalTelegramBot::sendPostToTelegram(String command, JsonObject& payl
119119
return response;
120120
}
121121

122-
String UniversalTelegramBot::sendImageFromFileToTelegram(File* file, String chat_id){
122+
String UniversalTelegramBot::sendImageFromFileToTelegram(String chat_id, int fileSize, MoreDataAvailable moreDataAvailableCallback, GetNextByte getNextByteCallback){
123123

124124
Serial.println("sendImageFromFileToTelegram");
125125
String response = "";
@@ -153,7 +153,7 @@ String UniversalTelegramBot::sendImageFromFileToTelegram(File* file, String chat
153153
client->println("User-Agent: arduino/1.0");
154154
client->println("Accept: */*");
155155

156-
int contentLength = file->size() + start_request.length() + end_request.length();
156+
int contentLength = fileSize + start_request.length() + end_request.length();
157157
Serial.println("Content-Length: " + String(contentLength));
158158
client->print("Content-Length: "); client->println(String(contentLength));
159159
client->println("Content-Type: multipart/form-data; boundary=" + boundry);
@@ -163,11 +163,11 @@ String UniversalTelegramBot::sendImageFromFileToTelegram(File* file, String chat
163163
Serial.print(start_request);
164164

165165
Serial.println("Sending....");
166-
char buffer[512];
166+
byte buffer[512];
167167
int count = 0;
168168
char ch;
169-
while (file->available()) {
170-
buffer[count] = file->read();
169+
while (moreDataAvailableCallback()) {
170+
buffer[count] = getNextByteCallback();
171171
//client->write(ch);
172172
//Serial.write(ch);
173173
count++;

src/UniversalTelegramBot.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
3434
#define SSL_PORT 443
3535
#define HANDLE_MESSAGES 1
3636

37+
typedef bool (*MoreDataAvailable)();
38+
typedef byte (*GetNextByte)();
39+
3740
struct telegramMessage{
3841
String text;
3942
String chat_id;
@@ -49,7 +52,7 @@ class UniversalTelegramBot
4952
UniversalTelegramBot (String token, Client &client);
5053
String sendGetToTelegram(String command);
5154
String sendPostToTelegram(String command, JsonObject& payload);
52-
String sendImageFromFileToTelegram(File* file, String chat_id);
55+
String sendImageFromFileToTelegram(String chat_id, int fileSize, MoreDataAvailable moreDataAvailableCallback, GetNextByte getNextByteCallback);
5356
bool getMe();
5457
bool sendSimpleMessage(String chat_id, String text, String parse_mode);
5558
bool sendMessage(String chat_id, String text, String parse_mode);

0 commit comments

Comments
 (0)