Skip to content

Commit f0ed638

Browse files
committed
WIP on image send from SD, does not work
1 parent 047f74f commit f0ed638

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

src/UniversalTelegramBot.cpp

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,107 @@ String UniversalTelegramBot::sendPostToTelegram(String command, JsonObject& payl
117117
return response;
118118
}
119119

120+
String UniversalTelegramBot::sendImageFromFileToTelegram(File* file, String chat_id){
121+
122+
String response = "";
123+
long now;
124+
bool responseRecieved;
125+
// Connect with api.telegram.org
126+
if (client->connect(HOST, SSL_PORT)) {
127+
// POST URI
128+
client->print("POST /sendPhoto?chat_id="+chat_id); client->println(" HTTP/1.1");
129+
// Host header
130+
client->print("Host:"); client->println(HOST);
131+
// JSON content type
132+
client->println("Content-Type: multipart/form-data, boundary=UH3xBZZtzewr09oPP");
133+
client->print("Content-Length:"); client->println(file->size() + 175);
134+
client->println(); //2
135+
client->println("--UH3xBZZtzewr09oPP"); //19 + 2
136+
client->println("content-disposition: form-data; name=\"photo\"; filename=\"img.jpg\""); //64 + 2
137+
client->println("Content-Type: image/jpeg"); //24 + 2
138+
client->println("Content-Transfer-Encoding: binary"); //33 + 2
139+
client->println(); //2
140+
141+
142+
Serial.println("data");
143+
int count = 0;
144+
char ch;
145+
while (file->available()) {
146+
ch = file->read();
147+
client->println(ch);
148+
//Serial.println(ch);
149+
count++;
150+
if(count > 50){
151+
ESP.wdtFeed();
152+
Serial.println("feed");
153+
count = 0;
154+
}
155+
}
156+
157+
158+
client->println(); //2
159+
client->println("--UH3xBZZtzewr09oPP--"); // 21 + 2
160+
// // Content length
161+
// //int length = payload.measureLength();
162+
// client->print("Content-Length:"); client->println(file->size());
163+
// Serial.println(file->size());
164+
// // End of headers
165+
// client->println();
166+
//
167+
// Serial.println("data");
168+
// int count = 0;
169+
// char ch;
170+
// while (file->available()) {
171+
// ch = file->read();
172+
// client->println(ch);
173+
// //Serial.println(ch);
174+
// count++;
175+
// if(count > 50){
176+
// ESP.wdtFeed();
177+
// Serial.println("feed");
178+
// count = 0;
179+
// }
180+
// }
181+
182+
Serial.println("Done");
183+
Serial.println("Done");
184+
Serial.println("Done");
185+
Serial.println("Done");
186+
187+
188+
count = 0;
189+
int ch_count=0;
190+
char c;
191+
now=millis();
192+
responseRecieved=false;
193+
while (millis()-now<1500) {
194+
// if(count > 100){
195+
// ESP.wdtFeed();
196+
// Serial.println("feed1");
197+
// count = 0;
198+
// }
199+
// count++;
200+
while (client->available()) {
201+
char c = client->read();
202+
//Serial.write(c);
203+
if (ch_count < maxMessageLength) {
204+
response=response+c;
205+
ch_count++;
206+
}
207+
responseRecieved=true;
208+
}
209+
if (responseRecieved) {
210+
Serial.println();
211+
Serial.println(response);
212+
Serial.println();
213+
break;
214+
}
215+
}
216+
}
217+
218+
return response;
219+
}
220+
120221

121222
bool UniversalTelegramBot::getMe() {
122223
String command="bot"+_token+"/getMe";

src/UniversalTelegramBot.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2828
#include <ArduinoJson.h>
2929
#include <Client.h>
3030

31+
#include <SD.h>
32+
3133
#define HOST "api.telegram.org"
3234
#define SSL_PORT 443
3335
#define HANDLE_MESSAGES 1
@@ -47,6 +49,7 @@ class UniversalTelegramBot
4749
UniversalTelegramBot (String token, Client &client);
4850
String sendGetToTelegram(String command);
4951
String sendPostToTelegram(String command, JsonObject& payload);
52+
String sendImageFromFileToTelegram(File* file, String chat_id);
5053
bool getMe();
5154
bool sendSimpleMessage(String chat_id, String text, String parse_mode);
5255
bool sendMessage(String chat_id, String text, String parse_mode);

0 commit comments

Comments
 (0)