Skip to content

Commit 2d413fb

Browse files
committed
Creating a sendPhoto method and making a sendMultipart data method for potential re-use with other binary files
1 parent f6e5e97 commit 2d413fb

File tree

4 files changed

+62
-25
lines changed

4 files changed

+62
-25
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ env:
1515
- SCRIPT=platformioSingle EXAMPLE_FOLDER=FlashledBot EXAMPLE_FILE=FlashledBot BOARDTYPE=ESP8266 BOARD=d1_mini
1616
- SCRIPT=platformioSingle EXAMPLE_FOLDER=UsingWiFiManager EXAMPLE_FILE=UsingWiFiManager BOARDTYPE=ESP8266 BOARD=d1_mini
1717
- SCRIPT=platformioSingle EXAMPLE_FOLDER=BulkMessages EXAMPLE_FILE=BulkMessages BOARDTYPE=ESP8266 BOARD=d1_mini
18+
- SCRIPT=platformioSingle EXAMPLE_FOLDER=SendPhoto/ImageFromSD EXAMPLE_FILE=ImageFromSD BOARDTYPE=ESP8266 BOARD=d1_mini
1819

1920
# This will run all the ESP8266 examples
2021
#- SCRIPT=platformioEsp8266 BOARD=d1_mini

examples/ESP8266/ImageFromSD/ImageFromSD.ino renamed to examples/ESP8266/SendPhoto/ImageFromSD/ImageFromSD.ino

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,24 @@ void loop() {
8383
while(numNewMessages) {
8484
Serial.println("got response");
8585

86-
DynamicJsonBuffer jsonBuffer;
87-
JsonObject& payload = jsonBuffer.createObject();
88-
payload["chat_id"] = bot.messages[0].chat_id;
86+
87+
String chat_id = bot.messages[0].chat_id;
8988

9089
myFile = SD.open("box.jpg");
9190
if (myFile) {
9291
Serial.println("box.jpg:");
9392

94-
bot.sendImageFromFileToTelegram(bot.messages[0].chat_id, myFile.size(), isMoreDataAvailable, getNextByte);
95-
// close the file:
93+
//Content type for PNG image/png
94+
bool sent = bot.sendImage(chat_id, "image/jpeg", myFile.size(),
95+
isMoreDataAvailable,
96+
getNextByte);
97+
98+
if(sent){
99+
Serial.println("Succesfully sent image");
100+
} else {
101+
Serial.println("Error sending image");
102+
}
103+
96104
myFile.close();
97105
} else {
98106
// if the file didn't open, print an error:

src/UniversalTelegramBot.cpp

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

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) {
123127

124-
Serial.println("sendImageFromFileToTelegram");
125128
String response = "";
126129
long now;
127130
bool responseRecieved;
128131
String boundry = "------------------------b8f610217e83e29b";
129132
// Connect with api.telegram.org
130133
if (client->connect(HOST, SSL_PORT)) {
131-
Serial.println("connected");
132-
// POST URI
134+
133135
String start_request = "";
134136
String end_request = "";
135137

@@ -139,30 +141,30 @@ String UniversalTelegramBot::sendImageFromFileToTelegram(String chat_id, int fil
139141
start_request = start_request + chat_id + "\r\n";
140142

141143
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";
144146
start_request = start_request + "\r\n";
145147

146148

147149
end_request = end_request + "\r\n";
148150
end_request = end_request + "--" + boundry + "--" + "\r\n";
149151

150-
client->print("POST /bot"+_token+"/sendPhoto"); client->println(" HTTP/1.1");
152+
client->print("POST /bot"+_token+"/" + command); client->println(" HTTP/1.1");
151153
// Host header
152154
client->print("Host: "); client->println(HOST);
153155
client->println("User-Agent: arduino/1.0");
154156
client->println("Accept: */*");
155157

156158
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));
158160
client->print("Content-Length: "); client->println(String(contentLength));
159161
client->println("Content-Type: multipart/form-data; boundary=" + boundry);
160162
client->println("");
161163

162164
client->print(start_request);
163-
Serial.print(start_request);
164165

165-
Serial.println("Sending....");
166+
if (_debug) Serial.print(start_request);
167+
166168
byte buffer[512];
167169
int count = 0;
168170
char ch;
@@ -173,22 +175,24 @@ String UniversalTelegramBot::sendImageFromFileToTelegram(String chat_id, int fil
173175
count++;
174176
if(count == 512){
175177
//yield();
176-
Serial.println("Sending full buffer");
178+
if (_debug) {
179+
Serial.println("Sending full buffer");
180+
}
177181
client->write((const uint8_t *)buffer, 512);
178182
count = 0;
179183
}
180184
}
181185

182186
if(count > 0) {
183-
Serial.println("Sending remaining buffer");
187+
if (_debug) {
188+
Serial.println("Sending remaining buffer");
189+
}
184190
client->write((const uint8_t *)buffer, count);
185191
}
186192

187193
client->print(end_request);
188-
Serial.print(end_request);
194+
if (_debug) Serial.print(end_request);
189195

190-
191-
Serial.println("Done");
192196
count = 0;
193197
int ch_count=0;
194198
char c;
@@ -205,9 +209,11 @@ String UniversalTelegramBot::sendImageFromFileToTelegram(String chat_id, int fil
205209
responseRecieved=true;
206210
}
207211
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+
}
211217
break;
212218
}
213219
}
@@ -425,6 +431,21 @@ bool UniversalTelegramBot::sendPostMessage(JsonObject& payload) {
425431
return sent;
426432
}
427433

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+
428449
bool UniversalTelegramBot::checkForOkResponse(String response) {
429450
int responseLength = response.length();
430451

src/UniversalTelegramBot.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
3030
#include <SD.h>
3131

3232
#define HOST "api.telegram.org"
33-
#define HOST2 "posttestserver.com"
3433
#define SSL_PORT 443
3534
#define HANDLE_MESSAGES 1
3635

@@ -52,7 +51,12 @@ class UniversalTelegramBot
5251
UniversalTelegramBot (String token, Client &client);
5352
String sendGetToTelegram(String command);
5453
String sendPostToTelegram(String command, JsonObject& payload);
55-
String sendImageFromFileToTelegram(String chat_id, int fileSize, MoreDataAvailable moreDataAvailableCallback, GetNextByte getNextByteCallback);
54+
String sendMultipartFormDataToTelegram(String command, String binaryProperyName,
55+
String fileName, String contentType,
56+
String chat_id, int fileSize,
57+
MoreDataAvailable moreDataAvailableCallback,
58+
GetNextByte getNextByteCallback);
59+
5660
bool getMe();
5761
bool sendSimpleMessage(String chat_id, String text, String parse_mode);
5862
bool sendMessage(String chat_id, String text, String parse_mode);
@@ -62,6 +66,9 @@ class UniversalTelegramBot
6266
bool sendMessageWithInlineKeyboard(String chat_id, String text,
6367
String parse_mode, String keyboard);
6468
bool sendPostMessage(JsonObject& payload);
69+
bool sendImage(String chat_id, String contentType, int fileSize,
70+
MoreDataAvailable moreDataAvailableCallback,
71+
GetNextByte getNextByteCallback);
6572
int getUpdates(long offset);
6673
telegramMessage messages[HANDLE_MESSAGES];
6774
long last_message_received;

0 commit comments

Comments
 (0)