Skip to content

Commit 3f466ee

Browse files
committed
Sending image finally works! needs to be tidied up more though
1 parent 6d0816e commit 3f466ee

File tree

1 file changed

+31
-86
lines changed

1 file changed

+31
-86
lines changed

src/UniversalTelegramBot.cpp

Lines changed: 31 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -123,131 +123,76 @@ String UniversalTelegramBot::sendImageFromFileToTelegram(File* file, String chat
123123
String response = "";
124124
long now;
125125
bool responseRecieved;
126-
String boundry = "AaB03x";
126+
String boundry = "------------------------b8f610217e83e29b";
127127
// Connect with api.telegram.org
128-
//if (client->connect(HOST, SSL_PORT)) {
129128
if (client->connect(HOST, SSL_PORT)) {
130129
Serial.println("connected");
131130
// POST URI
132131
String start_request = "";
133132
String end_request = "";
134133

135-
start_request = start_request + "\n";
136-
start_request = start_request + "--------------------------fa174948e0da42aa" + "\n";
137-
start_request = start_request + "content-disposition: form-data; name=\"chat_id\"" + "\n";
138-
start_request = start_request + "\n";
139-
start_request = start_request + chat_id + "\n";
134+
start_request = start_request + "--" + boundry + "\r\n";
135+
start_request = start_request + "content-disposition: form-data; name=\"chat_id\"" + "\r\n";
136+
start_request = start_request + "\r\n";
137+
start_request = start_request + chat_id + "\r\n";
140138

141-
start_request = start_request + boundry + "\n";
142-
start_request = start_request + "content-disposition: form-data; name=\"photo\"; filename=\"img.jpg\"" + "\n";
143-
start_request = start_request + "Content-Type: image/jpeg" + "\n";
144-
start_request = start_request + "\n";
139+
start_request = start_request + "--" + boundry + "\r\n";
140+
start_request = start_request + "content-disposition: form-data; name=\"photo\"; filename=\"img.jpg\"" + "\r\n";
141+
start_request = start_request + "Content-Type: image/jpeg" + "\r\n";
142+
start_request = start_request + "\r\n";
145143

146144

147-
end_request = end_request + "\n";
148-
end_request = end_request + boundry + "--" + "\n";
145+
end_request = end_request + "\r\n";
146+
end_request = end_request + "--" + boundry + "--" + "\r\n";
149147

150-
//client->print("POST /bot"+_token+"/sendPhoto"); client->println(" HTTP/1.1");
151-
client->print("POST /post.php"); client->println(" HTTP/1.1");
148+
client->print("POST /bot"+_token+"/sendPhoto"); client->println(" HTTP/1.1");
152149
// Host header
153150
client->print("Host: "); client->println(HOST);
154151
client->println("User-Agent: arduino/1.0");
155152
client->println("Accept: */*");
156-
// JSON content type
157-
//client->print("Content-Length:"); client->println(file->size() + 225);
158-
//175753396
159-
Serial.println("Content-Length: " + String(file->size() + start_request.length() + end_request.length()));
160-
Serial.println("File-Length: " + String(file->size()));
161-
client->print("Content-Length: "); client->println(file->size() + start_request.length() + end_request.length());
162153

154+
int contentLength = file->size() + start_request.length() + end_request.length();
155+
Serial.println("Content-Length: " + String(contentLength));
156+
client->print("Content-Length: "); client->println(String(contentLength));
163157
client->println("Content-Type: multipart/form-data; boundary=" + boundry);
164-
//
165-
// client->println("--------------------------fa174948e0da42aa"); //42 + 2
166-
// client->println("content-disposition: form-data; name=\"chat_id\""); //48 + 2
167-
// client->println(); //2
168-
// client->println(chat_id); //2 + chat_id length
169-
// client->println("--------------------------fa174948e0da42aa"); //42 + 2
170-
// client->println("content-disposition: form-data; name=\"photo\"; filename=\"box.jpg\""); //64 + 2
171-
//
172-
// //client->println("Content-Type: application/octet-stream"); //38 + 2
173-
//
174-
// client->println("Content-Type: image/jpeg"); //24 + 2
175-
// //client->println("Content-Transfer-Encoding: binary"); //33 + 2
176-
// client->println(); //
158+
client->println("");
177159

178160
client->print(start_request);
179-
Serial.println(start_request);
180-
181-
byte buff[1055];
182-
int clientCount = 0;
183-
161+
Serial.print(start_request);
184162

185-
Serial.println("data");
163+
Serial.println("Sending....");
164+
char buffer[512];
186165
int count = 0;
187166
char ch;
188167
while (file->available()) {
189-
// buff[clientCount] = file->read();
190-
// clientCount++;
191-
ch = file->read();
192-
client->write(ch);
193-
Serial.write(ch);
168+
buffer[count] = file->read();
169+
//client->write(ch);
170+
//Serial.write(ch);
194171
count++;
195-
if(count > 100){
172+
if(count == 512){
196173
//yield();
197-
//Serial.println("feed");
174+
Serial.println("Sending full buffer");
175+
client->write((const uint8_t *)buffer, 512);
198176
count = 0;
199177
}
200178
}
201179

202-
// if(clientCount > 0) {
203-
// client->write(buff,clientCount);
204-
// }
180+
if(count > 0) {
181+
Serial.println("Sending remaining buffer");
182+
client->write((const uint8_t *)buffer, count);
183+
}
205184

206185
client->print(end_request);
207-
Serial.println(end_request);
208-
209-
// client->println(); //2
210-
// client->println("--------------------------fa174948e0da42aa--"); // 44 + 2
211-
// // Content length
212-
// //int length = payload.measureLength();
213-
// client->print("Content-Length:"); client->println(file->size());
214-
// Serial.println(file->size());
215-
// // End of headers
216-
// client->println();
217-
//
218-
// Serial.println("data");
219-
// int count = 0;
220-
// char ch;
221-
// while (file->available()) {
222-
// ch = file->read();
223-
// client->println(ch);
224-
// //Serial.println(ch);
225-
// count++;
226-
// if(count > 50){
227-
// ESP.wdtFeed();
228-
// Serial.println("feed");
229-
// count = 0;
230-
// }
231-
// }
232-
233-
Serial.println("Done");
234-
Serial.println("Done");
235-
Serial.println("Done");
236-
Serial.println("Done");
186+
Serial.print(end_request);
237187

238188

189+
Serial.println("Done");
239190
count = 0;
240191
int ch_count=0;
241192
char c;
242193
now=millis();
243194
responseRecieved=false;
244195
while (millis()-now<1500) {
245-
// if(count > 100){
246-
// ESP.wdtFeed();
247-
// Serial.println("feed1");
248-
// count = 0;
249-
// }
250-
// count++;
251196
while (client->available()) {
252197
char c = client->read();
253198
//Serial.write(c);

0 commit comments

Comments
 (0)