Skip to content

Commit 35eafd1

Browse files
committed
more work on sendPhoto, still not working
1 parent 68b0c9e commit 35eafd1

File tree

2 files changed

+129
-11
lines changed

2 files changed

+129
-11
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*******************************************************************
2+
* An example of bot that echos back any messages received *
3+
* *
4+
* written by Giacarlo Bacchio (Gianbacchio on Github) *
5+
* adapted by Brian Lough *
6+
*******************************************************************/
7+
8+
#include <ESP8266WiFi.h>
9+
#include <WiFiClientSecure.h>
10+
#include <UniversalTelegramBot.h>
11+
#include <ArduinoJson.h>
12+
13+
#include <SPI.h>
14+
#include <SD.h>
15+
16+
17+
// Initialize Wifi connection to the router
18+
char ssid[] = "Mikrotik"; // your network SSID (name)
19+
char password[] = "carolinebrian"; // your network key
20+
21+
22+
23+
// Initialize Telegram BOT
24+
#define BOTtoken "245123272:AAF0si333T4uO9b8soU7sC1nNy2ksmEdqFU" //token of test2
25+
26+
WiFiClientSecure client;
27+
UniversalTelegramBot bot(BOTtoken, client);
28+
29+
int Bot_mtbs = 1000; //mean time between scan messages
30+
long Bot_lasttime; //last time messages' scan has been done
31+
32+
33+
void setup() {
34+
35+
Serial.begin(115200);
36+
37+
Serial.print("Initializing SD card...");
38+
39+
if (!SD.begin(D0)) {
40+
Serial.println("initialization failed!");
41+
return;
42+
}
43+
Serial.println("initialization done.");
44+
45+
46+
// Set WiFi to station mode and disconnect from an AP if it was Previously
47+
// connected
48+
WiFi.mode(WIFI_STA);
49+
WiFi.disconnect();
50+
delay(100);
51+
52+
// Attempt to connect to Wifi network:
53+
Serial.print("Connecting Wifi: ");
54+
Serial.println(ssid);
55+
WiFi.begin(ssid, password);
56+
while (WiFi.status() != WL_CONNECTED) {
57+
Serial.print(".");
58+
delay(500);
59+
}
60+
Serial.println("");
61+
Serial.println("WiFi connected");
62+
Serial.println("IP address: ");
63+
IPAddress ip = WiFi.localIP();
64+
Serial.println(ip);
65+
66+
//ESP.wdtEnable(15000000); // make the watch dog timeout longer
67+
68+
}
69+
70+
void loop() {
71+
72+
if (millis() > Bot_lasttime + Bot_mtbs) {
73+
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
74+
while(numNewMessages) {
75+
Serial.println("got response");
76+
77+
DynamicJsonBuffer jsonBuffer;
78+
JsonObject& payload = jsonBuffer.createObject();
79+
payload["chat_id"] = bot.messages[0].chat_id;
80+
81+
File myFile = SD.open("box.jpg");
82+
if (myFile) {
83+
Serial.println("box.jpg:");
84+
85+
bot.sendImageFromFileToTelegram(&myFile, bot.messages[0].chat_id);
86+
// close the file:
87+
myFile.close();
88+
} else {
89+
// if the file didn't open, print an error:
90+
Serial.println("error opening image.jpg");
91+
}
92+
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
93+
}
94+
Bot_lasttime = millis();
95+
}
96+
}

src/UniversalTelegramBot.cpp

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -125,38 +125,60 @@ String UniversalTelegramBot::sendImageFromFileToTelegram(File* file, String chat
125125
// Connect with api.telegram.org
126126
if (client->connect(HOST, SSL_PORT)) {
127127
// POST URI
128-
client->print("POST /sendPhoto?chat_id="+chat_id); client->println(" HTTP/1.1");
128+
client->print("POST /bot"+_token+"/sendPhoto"); client->println(" HTTP/1.1");
129129
// Host header
130130
client->print("Host:"); client->println(HOST);
131+
client->println("User-Agent: arduino/1.0");
132+
client->println("Accept: */*");
133+
client->println("Expect: 100-continue");
131134
// JSON content type
132-
client->println("Content-Type: multipart/form-data, boundary=UH3xBZZtzewr09oPP");
133-
client->print("Content-Length:"); client->println(file->size() + 175);
135+
//client->print("Content-Length:"); client->println(file->size() + 225);
136+
//175753396
137+
Serial.println("Content-Length: " + String(file->size() + 317 + chat_id.length()));
138+
Serial.println("File-Length: " + String(file->size()));
139+
client->print("Content-Length: "); client->println(file->size() + 317 + chat_id.length());
140+
client->println("Content-Type: multipart/form-data, boundary=boundary=------------------------fa174948e0da42aa");
141+
client->println(); //
142+
delay(500);
143+
while (client->available()) {
144+
char c = client->read();
145+
response=response+c;
146+
}
147+
148+
Serial.println("Resp:" + response);
149+
response = "";
150+
151+
client->println("--------------------------fa174948e0da42aa"); //42 + 2
152+
client->println("content-disposition: form-data; name=\"chat_id\""); //48 + 2
134153
client->println(); //2
135-
client->println("--UH3xBZZtzewr09oPP"); //19 + 2
154+
client->println(chat_id); //2 + chat_id length
155+
client->println("--------------------------fa174948e0da42aa"); //42 + 2
136156
client->println("content-disposition: form-data; name=\"photo\"; filename=\"img.jpg\""); //64 + 2
157+
158+
//client->println("Content-Type: application/octet-stream"); //38 + 2
159+
137160
client->println("Content-Type: image/jpeg"); //24 + 2
138161
client->println("Content-Transfer-Encoding: binary"); //33 + 2
139-
client->println(); //2
162+
client->println(); //
140163

141164

142165
Serial.println("data");
143166
int count = 0;
144167
char ch;
145168
while (file->available()) {
146169
ch = file->read();
147-
client->println(ch);
148-
//Serial.println(ch);
170+
client->print(ch);
171+
Serial.print(ch);
149172
count++;
150-
if(count > 50){
173+
if(count > 100){
151174
ESP.wdtFeed();
152-
Serial.println("feed");
175+
//Serial.println("feed");
153176
count = 0;
154177
}
155178
}
156179

157-
158180
client->println(); //2
159-
client->println("--UH3xBZZtzewr09oPP--"); // 21 + 2
181+
client->println("--------------------------fa174948e0da42aa--"); // 44 + 2
160182
// // Content length
161183
// //int length = payload.measureLength();
162184
// client->print("Content-Length:"); client->println(file->size());

0 commit comments

Comments
 (0)