Skip to content

Commit 429309a

Browse files
committed
Update ESP8266 examples
1 parent 63ed4a5 commit 429309a

File tree

30 files changed

+1591
-1413
lines changed

30 files changed

+1591
-1413
lines changed

.travis.yml

+17-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
language: python
22
python:
3-
- "2.7"
3+
- "3.7"
44

55
# Cache PlatformIO packages using Travis CI container-based infrastructure
66
sudo: false
@@ -10,20 +10,21 @@ cache:
1010

1111
env:
1212
# ESP8266
13+
- SCRIPT=platformioSingle EXAMPLE_NAME=BulkMessages EXAMPLE_FOLDER=/ BOARDTYPE=ESP8266 BOARD=d1_mini
14+
- SCRIPT=platformioSingle EXAMPLE_NAME=ChannelPost EXAMPLE_FOLDER=/ BOARDTYPE=ESP8266 BOARD=d1_mini
15+
- SCRIPT=platformioSingle EXAMPLE_NAME=ChatAction EXAMPLE_FOLDER=/ BOARDTYPE=ESP8266 BOARD=d1_mini
16+
- SCRIPT=platformioSingle EXAMPLE_NAME=InlineKeyboardMarkup EXAMPLE_FOLDER=/CustomKeyboard/ BOARDTYPE=ESP8266 BOARD=d1_mini
17+
- SCRIPT=platformioSingle EXAMPLE_NAME=ReplyKeyboardMarkup EXAMPLE_FOLDER=/CustomKeyboard/ BOARDTYPE=ESP8266 BOARD=d1_mini
1318
- SCRIPT=platformioSingle EXAMPLE_NAME=EchoBot EXAMPLE_FOLDER=/ BOARDTYPE=ESP8266 BOARD=d1_mini
14-
#- SCRIPT=platformioSingle EXAMPLE_NAME=ReplyKeyboardMarkup EXAMPLE_FOLDER=/CustomKeyboard/ BOARDTYPE=ESP8266 BOARD=d1_mini
15-
#- SCRIPT=platformioSingle EXAMPLE_NAME=InlineKeyboardMarkup EXAMPLE_FOLDER=/CustomKeyboard/ BOARDTYPE=ESP8266 BOARD=d1_mini
1619
- SCRIPT=platformioSingle EXAMPLE_NAME=FlashLED EXAMPLE_FOLDER=/ BOARDTYPE=ESP8266 BOARD=d1_mini
20+
- SCRIPT=platformioSingle EXAMPLE_NAME=Location EXAMPLE_FOLDER=/ BOARDTYPE=ESP8266 BOARD=d1_mini
21+
- SCRIPT=platformioSingle EXAMPLE_NAME=LongPoll EXAMPLE_FOLDER=/ BOARDTYPE=ESP8266 BOARD=d1_mini
22+
- SCRIPT=platformioSingle EXAMPLE_NAME=PhotoFromFileID EXAMPLE_FOLDER=/SendPhoto/ BOARDTYPE=ESP8266 BOARD=d1_mini
23+
- SCRIPT=platformioSingle EXAMPLE_NAME=PhotoFromSD EXAMPLE_FOLDER=/SendPhoto/ BOARDTYPE=ESP8266 BOARD=d1_mini
24+
- SCRIPT=platformioSingle EXAMPLE_NAME=PhotoFromSerial EXAMPLE_FOLDER=/SendPhoto/ BOARDTYPE=ESP8266 BOARD=d1_mini
25+
- SCRIPT=platformioSingle EXAMPLE_NAME=PhotoFromURL EXAMPLE_FOLDER=/SendPhoto/ BOARDTYPE=ESP8266 BOARD=d1_mini
26+
- SCRIPT=platformioSingle EXAMPLE_NAME=SetMyCommands EXAMPLE_FOLDER=/ BOARDTYPE=ESP8266 BOARD=d1_mini
1727
#- SCRIPT=platformioSingle EXAMPLE_NAME=UsingWiFiManager EXAMPLE_FOLDER=/ BOARDTYPE=ESP8266 BOARD=d1_mini
18-
#- SCRIPT=platformioSingle EXAMPLE_NAME=BulkMessages EXAMPLE_FOLDER=/ BOARDTYPE=ESP8266 BOARD=d1_mini
19-
#- SCRIPT=platformioSingle EXAMPLE_NAME=PhotoFromSD EXAMPLE_FOLDER=/SendPhoto/ BOARDTYPE=ESP8266 BOARD=d1_mini
20-
#- SCRIPT=platformioSingle EXAMPLE_NAME=PhotoFromURL EXAMPLE_FOLDER=/SendPhoto/ BOARDTYPE=ESP8266 BOARD=d1_mini
21-
#- SCRIPT=platformioSingle EXAMPLE_NAME=PhotoFromFileID EXAMPLE_FOLDER=/SendPhoto/ BOARDTYPE=ESP8266 BOARD=d1_mini
22-
#- SCRIPT=platformioSingle EXAMPLE_NAME=Location EXAMPLE_FOLDER=/ BOARDTYPE=ESP8266 BOARD=d1_mini
23-
#- SCRIPT=platformioSingle EXAMPLE_NAME=ChannelPost EXAMPLE_FOLDER=/ BOARDTYPE=ESP8266 BOARD=d1_mini
24-
#- SCRIPT=platformioSingle EXAMPLE_NAME=ChatAction EXAMPLE_FOLDER=/ BOARDTYPE=ESP8266 BOARD=d1_mini
25-
#- SCRIPT=platformioSingle EXAMPLE_NAME=LongPoll EXAMPLE_FOLDER=/ BOARDTYPE=ESP8266 BOARD=d1_mini
26-
- SCRIPT=platformioSingle EXAMPLE_NAME=SetMyCommand EXAMPLE_FOLDER=/ BOARDTYPE=ESP8266 BOARD=d1_mini
2728

2829
# ESP32
2930
- SCRIPT=platformioSingle EXAMPLE_NAME=EchoBot EXAMPLE_FOLDER=/ BOARDTYPE=ESP32 BOARD=esp32dev
@@ -39,11 +40,12 @@ env:
3940

4041
install:
4142
- pip install -U platformio
43+
- platformio update
4244
#
4345
# Libraries from PlatformIO Library Registry:
4446
#
4547
# http://platformio.org/lib/show/64/ArduinoJson
4648
- platformio lib -g install 64
47-
# http://platformio.org/lib/show/567/WifiManager
48-
- platformio lib -g install 567
49-
script: scripts/travis/$SCRIPT.sh
49+
50+
script:
51+
- scripts/travis/$SCRIPT.sh
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
/*******************************************************************
2+
* An example of how to use a bulk messages to subscribed users. *
3+
* *
4+
* written by Vadim Sinitski *
5+
*******************************************************************/
6+
#include <ESP8266WiFi.h>
7+
#include <WiFiClientSecure.h>
8+
#include <UniversalTelegramBot.h>
9+
#include <ArduinoJson.h>
10+
#include <LittleFS.h>
11+
12+
// Wifi network station credentials
13+
#define WIFI_SSID "YOUR_SSID"
14+
#define WIFI_PASSWORD "YOUR_PASSWORD"
15+
// Telegram BOT Token (Get from Botfather)
16+
#define BOT_TOKEN "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
17+
18+
const char *SUBSCRIBED_USERS_FILENAME = "/subscribed_users.json"; // Filename for local storage
19+
const unsigned long BULK_MESSAGES_MTBS = 1500; // Mean time between send messages, 1.5 seconds
20+
const unsigned int MESSAGES_LIMIT_PER_SECOND = 25; // Telegram API have limit for bulk messages ~30 messages per second
21+
const unsigned long BOT_MTBS = 1000; // Mean time between scan messages
22+
23+
WiFiClientSecure secured_client;
24+
X509List cert(TELEGRAM_CERTIFICATE_ROOT);
25+
UniversalTelegramBot bot(BOT_TOKEN, secured_client);
26+
DynamicJsonDocument usersDoc(1500);
27+
unsigned long bot_lasttime; // last time messages' scan has been done
28+
29+
JsonObject getSubscribedUsers()
30+
{
31+
File subscribedUsersFile = LittleFS.open(SUBSCRIBED_USERS_FILENAME, "r");
32+
JsonObject users;
33+
34+
// no file
35+
if (!subscribedUsersFile)
36+
{
37+
Serial.println("Failed to open subscribed users file");
38+
// Create empty file (w+ not working as expect)
39+
File f = LittleFS.open(SUBSCRIBED_USERS_FILENAME, "w");
40+
f.close();
41+
return users;
42+
}
43+
44+
// too large file
45+
size_t size = subscribedUsersFile.size();
46+
if (size > 1500)
47+
{
48+
subscribedUsersFile.close();
49+
Serial.println("Subscribed users file is too large");
50+
return users;
51+
}
52+
53+
String file_content = subscribedUsersFile.readString();
54+
subscribedUsersFile.close();
55+
DeserializationError error = deserializeJson(usersDoc, file_content);
56+
if (error)
57+
{
58+
Serial.println("Failed to parse subscribed users file");
59+
return users;
60+
}
61+
users = usersDoc.as<JsonObject>();
62+
63+
return users;
64+
}
65+
66+
bool addSubscribedUser(String chat_id, String from_name)
67+
{
68+
JsonObject users = getSubscribedUsers();
69+
users[chat_id] = from_name;
70+
71+
File subscribedUsersFile = LittleFS.open(SUBSCRIBED_USERS_FILENAME, "w+");
72+
// file not available
73+
if (!subscribedUsersFile)
74+
{
75+
subscribedUsersFile.close();
76+
Serial.println("Failed to open subscribed users file for writing");
77+
return false;
78+
}
79+
80+
//users.getOrCreateMember(chat_id, from_name);
81+
serializeJson(users, subscribedUsersFile);
82+
subscribedUsersFile.close();
83+
return true;
84+
}
85+
86+
bool removeSubscribedUser(String chat_id)
87+
{
88+
JsonObject users = getSubscribedUsers();
89+
users.remove(chat_id);
90+
91+
File subscribedUsersFile = LittleFS.open(SUBSCRIBED_USERS_FILENAME, "w");
92+
// file not available
93+
if (!subscribedUsersFile)
94+
{
95+
subscribedUsersFile.close();
96+
Serial.println("Failed to open subscribed users file for writing");
97+
return false;
98+
}
99+
100+
serializeJson(users, subscribedUsersFile);
101+
subscribedUsersFile.close();
102+
return true;
103+
}
104+
105+
void sendMessageToAllSubscribedUsers(String message)
106+
{
107+
JsonObject users = getSubscribedUsers();
108+
unsigned int users_processed = 0;
109+
110+
for (JsonObject::iterator it = users.begin(); it != users.end(); ++it)
111+
{
112+
users_processed++;
113+
if (users_processed < MESSAGES_LIMIT_PER_SECOND)
114+
{
115+
const char *chat_id = it->key().c_str();
116+
bot.sendMessage(chat_id, message, "");
117+
}
118+
else
119+
{
120+
delay(BULK_MESSAGES_MTBS);
121+
users_processed = 0;
122+
}
123+
}
124+
}
125+
126+
void handleNewMessages(int numNewMessages)
127+
{
128+
Serial.print("handleNewMessages: ");
129+
Serial.println(numNewMessages);
130+
131+
for (int i = 0; i < numNewMessages; i++)
132+
{
133+
String chat_id = bot.messages[i].chat_id;
134+
String text = bot.messages[i].text;
135+
String from_name = bot.messages[i].from_name;
136+
if (from_name == "")
137+
from_name = "Guest";
138+
139+
if (text == "/start")
140+
{
141+
if (addSubscribedUser(chat_id, from_name))
142+
{
143+
String welcome = "Welcome to Universal Arduino Telegram Bot library.\n";
144+
welcome += "This is Bulk Messages example.\n\n";
145+
welcome += "You, " + from_name + ", have been subscribed.\n";
146+
welcome += "/showallusers : show all subscribed users\n";
147+
welcome += "/testbulkmessage : send test message to subscribed users\n";
148+
welcome += "/removeallusers : remove all subscribed users\n";
149+
welcome += "/stop : unsubscribe from bot\n";
150+
bot.sendMessage(chat_id, welcome, "Markdown");
151+
}
152+
else
153+
{
154+
bot.sendMessage(chat_id, "Something wrong, please try again (later?)", "");
155+
}
156+
}
157+
158+
if (text == "/stop")
159+
{
160+
if (removeSubscribedUser(chat_id))
161+
{
162+
bot.sendMessage(chat_id, "Thank you, " + from_name + ", we always waiting you back", "");
163+
}
164+
else
165+
{
166+
bot.sendMessage(chat_id, "Something wrong, please try again (later?)", "");
167+
}
168+
}
169+
170+
if (text == "/testbulkmessage")
171+
{
172+
sendMessageToAllSubscribedUsers("ATTENTION, this is bulk message for all subscribed users!");
173+
}
174+
175+
if (text == "/showallusers")
176+
{
177+
File subscribedUsersFile = LittleFS.open(SUBSCRIBED_USERS_FILENAME, "r");
178+
// no file
179+
if (!subscribedUsersFile)
180+
{
181+
bot.sendMessage(chat_id, "No subscription file", "");
182+
return;
183+
}
184+
size_t size = subscribedUsersFile.size();
185+
if (size > 1024)
186+
{
187+
bot.sendMessage(chat_id, "Subscribed users file is too large", "");
188+
}
189+
else
190+
{
191+
String file_content = subscribedUsersFile.readString();
192+
bot.sendMessage(chat_id, file_content, "");
193+
}
194+
}
195+
196+
if (text == "/removeallusers")
197+
{
198+
if (LittleFS.remove(SUBSCRIBED_USERS_FILENAME))
199+
{
200+
bot.sendMessage(chat_id, "All users removed", "");
201+
}
202+
else
203+
{
204+
bot.sendMessage(chat_id, "Something wrong, please try again (later?)", "");
205+
}
206+
}
207+
}
208+
}
209+
210+
void setup()
211+
{
212+
Serial.begin(115200);
213+
Serial.println();
214+
215+
if (!LittleFS.begin())
216+
{
217+
Serial.println("Failed to mount file system");
218+
return;
219+
}
220+
221+
// attempt to connect to Wifi network:
222+
configTime(0, 0, "pool.ntp.org"); // get UTC time via NTP
223+
secured_client.setTrustAnchors(&cert); // Add root certificate for api.telegram.org
224+
Serial.print("Connecting to Wifi SSID ");
225+
Serial.print(WIFI_SSID);
226+
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
227+
while (WiFi.status() != WL_CONNECTED)
228+
{
229+
Serial.print(".");
230+
delay(500);
231+
}
232+
Serial.print("\nWiFi connected. IP address: ");
233+
Serial.println(WiFi.localIP());
234+
235+
// Check NTP/Time, usually it is instantaneous and you can delete the code below.
236+
Serial.print("Retrieving time: ");
237+
time_t now = time(nullptr);
238+
while (now < 24 * 3600)
239+
{
240+
Serial.print(".");
241+
delay(100);
242+
now = time(nullptr);
243+
}
244+
Serial.println(now);
245+
}
246+
247+
void loop()
248+
{
249+
if (millis() - bot_lasttime > BOT_MTBS)
250+
{
251+
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
252+
253+
while (numNewMessages)
254+
{
255+
Serial.println("got response");
256+
handleNewMessages(numNewMessages);
257+
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
258+
}
259+
260+
bot_lasttime = millis();
261+
}
262+
}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ESP8266 - Bulk Messages (Send Message to all users who /start using your Telegram bot)
2+
3+
This is a basic example of how to use UniversalTelegramBot on ESP8266 based boards.
4+
5+
The application will echo bulk messages to subscribed users.
6+
7+
NOTE: You will need to enter your SSID, password and Bot token for the example to work.
8+
9+
Tested on 5 subscribed users. I don't know what will be with 10 000 users, but 10 000 users better work with DB or something :)
10+
Just for little community, for example send some data from your Arduino device to all your family or friends who using your Telegram bot.
11+
12+
## License
13+
14+
![License](https://img.shields.io/github/license/witnessmenow/Universal-Arduino-Telegram-Bot)
15+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

0 commit comments

Comments
 (0)