Skip to content

Commit 0cf304a

Browse files
committed
Fixing up Echo example for ESP32 and adding send photo by URL
1 parent 6950785 commit 0cf304a

File tree

3 files changed

+160
-10
lines changed

3 files changed

+160
-10
lines changed

examples/ESP32/EchoBot/EchoBot.ino

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,43 @@
11
/*******************************************************************
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-
*******************************************************************/
2+
A telegram bot for your ESP32 that responds
3+
with whatever message you send it.
4+
5+
Parts:
6+
ESP32 D1 Mini stlye Dev board* - http://s.click.aliexpress.com/e/C6ds4my
7+
(or any ESP32 board)
8+
9+
= Affilate
10+
11+
If you find what I do useful and would like to support me,
12+
please consider becoming a sponsor on Github
13+
https://github.com/sponsors/witnessmenow/
14+
15+
16+
Written by Brian Lough
17+
YouTube: https://www.youtube.com/brianlough
18+
Tindie: https://www.tindie.com/stores/brianlough/
19+
Twitter: https://twitter.com/witnessmenow
20+
*******************************************************************/
21+
22+
// ----------------------------
23+
// Standard ESP32 Libraries
24+
// ----------------------------
25+
726
#include <WiFi.h>
827
#include <WiFiClientSecure.h>
28+
29+
// ----------------------------
30+
// Additional Libraries - each one of these will need to be installed.
31+
// ----------------------------
32+
933
#include <UniversalTelegramBot.h>
1034

35+
#include <ArduinoJson.h>
36+
// Library used for parsing Json from the API responses
37+
38+
// Search for "Arduino Json" in the Arduino Library manager
39+
// https://github.com/bblanchon/ArduinoJson
40+
1141
// Initialize Wifi connection to the router
1242
char ssid[] = "XXXXXX"; // your network SSID (name)
1343
char password[] = "YYYYYY"; // your network key
@@ -18,8 +48,9 @@ char password[] = "YYYYYY"; // your network key
1848
WiFiClientSecure client;
1949
UniversalTelegramBot bot(BOTtoken, client);
2050

21-
int Bot_mtbs = 1000; //mean time between scan messages
22-
long Bot_lasttime; //last time messages' scan has been done
51+
//Checks for new messages every 1 second.
52+
int botRequestDelay = 1000;
53+
unsigned long lastTimeBotRan;
2354

2455
void setup() {
2556
Serial.begin(115200);
@@ -45,7 +76,7 @@ void setup() {
4576
}
4677

4778
void loop() {
48-
if (millis() > Bot_lasttime + Bot_mtbs) {
79+
if (millis() > lastTimeBotRan + botRequestDelay) {
4980
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
5081

5182
while(numNewMessages) {
@@ -56,6 +87,6 @@ void loop() {
5687
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
5788
}
5889

59-
Bot_lasttime = millis();
90+
lastTimeBotRan = millis();
6091
}
6192
}
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*******************************************************************
2+
A telegram bot for your ESP32 that demonstrates sending an image
3+
from URL.
4+
5+
Parts:
6+
ESP32 D1 Mini stlye Dev board* - http://s.click.aliexpress.com/e/C6ds4my
7+
(or any ESP32 board)
8+
9+
= Affilate
10+
11+
If you find what I do useful and would like to support me,
12+
please consider becoming a sponsor on Github
13+
https://github.com/sponsors/witnessmenow/
14+
15+
Example originally written by Vadim Sinitski
16+
17+
Library written by Brian Lough
18+
YouTube: https://www.youtube.com/brianlough
19+
Tindie: https://www.tindie.com/stores/brianlough/
20+
Twitter: https://twitter.com/witnessmenow
21+
*******************************************************************/
22+
23+
// ----------------------------
24+
// Standard ESP32 Libraries
25+
// ----------------------------
26+
27+
#include <WiFi.h>
28+
#include <WiFiClientSecure.h>
29+
30+
// ----------------------------
31+
// Additional Libraries - each one of these will need to be installed.
32+
// ----------------------------
33+
34+
#include <UniversalTelegramBot.h>
35+
36+
#include <ArduinoJson.h>
37+
// Library used for parsing Json from the API responses
38+
39+
// Search for "Arduino Json" in the Arduino Library manager
40+
// https://github.com/bblanchon/ArduinoJson
41+
42+
// Initialize Wifi connection to the router
43+
char ssid[] = "XXXXXX"; // your network SSID (name)
44+
char password[] = "YYYYYY"; // your network key
45+
46+
// Initialize Telegram BOT
47+
#define BOTtoken "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get from Botfather)
48+
49+
WiFiClientSecure client;
50+
UniversalTelegramBot bot(BOTtoken, client);
51+
52+
//Checks for new messages every 1 second.
53+
int botRequestDelay = 1000;
54+
unsigned long lastTimeBotRan;
55+
56+
String test_photo_url = "https://www.arduino.cc/en/uploads/Trademark/ArduinoCommunityLogo.png";
57+
58+
void handleNewMessages(int numNewMessages) {
59+
Serial.println("handleNewMessages");
60+
Serial.println(String(numNewMessages));
61+
62+
for (int i=0; i<numNewMessages; i++) {
63+
String chat_id = String(bot.messages[i].chat_id);
64+
String text = bot.messages[i].text;
65+
66+
String from_name = bot.messages[i].from_name;
67+
if (from_name == "") from_name = "Guest";
68+
69+
if (text == "/get_test_photo") {
70+
bot.sendPhoto(chat_id, test_photo_url, "Caption is optional, you may not use photo caption");
71+
}
72+
73+
if (text == "/start") {
74+
String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
75+
welcome += "This is Send Image From URL example.\n\n";
76+
welcome += "/get_test_photo : getting test photo\n";
77+
78+
bot.sendMessage(chat_id, welcome, "");
79+
}
80+
}
81+
}
82+
83+
void setup() {
84+
Serial.begin(115200);
85+
86+
// Set WiFi to station mode and disconnect from an AP if it was Previously connected
87+
WiFi.mode(WIFI_STA);
88+
WiFi.disconnect();
89+
delay(100);
90+
91+
// attempt to connect to Wifi network:
92+
Serial.print("Connecting Wifi: ");
93+
Serial.println(ssid);
94+
WiFi.begin(ssid, password);
95+
96+
while (WiFi.status() != WL_CONNECTED) {
97+
Serial.print(".");
98+
delay(500);
99+
}
100+
101+
Serial.println("");
102+
Serial.println("WiFi connected");
103+
Serial.print("IP address: ");
104+
Serial.println(WiFi.localIP());
105+
}
106+
107+
void loop() {
108+
if (millis() > lastTimeBotRan + botRequestDelay) {
109+
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
110+
111+
while(numNewMessages) {
112+
Serial.println("got response");
113+
handleNewMessages(numNewMessages);
114+
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
115+
}
116+
117+
lastTimeBotRan = millis();
118+
}
119+
}

examples/ESP8266/EchoBot/EchoBot.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
Twitter: https://twitter.com/witnessmenow
2020
*******************************************************************/
2121

22-
// The version of ESP8266 core needs to be 2.5 or higher
22+
// NOTE: The version of ESP8266 core needs to be 2.5 or higher
2323
// or else your bot will not connect.
2424

2525
// ----------------------------

0 commit comments

Comments
 (0)