Skip to content

Commit 282a19a

Browse files
committed
Created BulkMessages example, that send message to all users who /start (subscribed) using your telegram bot.
1 parent e871e27 commit 282a19a

File tree

2 files changed

+204
-0
lines changed

2 files changed

+204
-0
lines changed
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
/*******************************************************************
2+
* An example of how to use a custom reply keyboard. *
3+
* *
4+
* *
5+
* written by Brian Lough *
6+
*******************************************************************/
7+
#include <ESP8266WiFi.h>
8+
#include <WiFiClientSecure.h>
9+
#include <UniversalTelegramBot.h>
10+
#include <FS.h>
11+
#include <ArduinoJson.h>
12+
13+
// Initialize Wifi connection to the router
14+
char ssid[] = "1a3496"; // your network SSID (name)
15+
char password[] = "278833924"; // your network key
16+
17+
// Initialize Telegram BOT
18+
#define BOTtoken "326858637:AAGDZGkaKP_Vrhzs8mkd--0BnGQQ6AioF4M" // your Bot Token (Get off Botfather)
19+
20+
WiFiClientSecure client;
21+
UniversalTelegramBot bot(BOTtoken, client);
22+
23+
int Bot_mtbs = 1000; //mean time between scan messages
24+
long Bot_lasttime; //last time messages' scan has been done
25+
26+
int bulk_messages_mtbs = 1500; //mean time between send messages, 1.5 seconds
27+
int messages_limit_per_second = 25; // Telegram API have limit for bulk messages ~30 messages in 1 second
28+
29+
StaticJsonBuffer<200> jsonBuffer;
30+
JsonObject& subscribed_users = jsonBuffer.createObject();
31+
32+
void handleNewMessages(int numNewMessages) {
33+
Serial.println("handleNewMessages");
34+
Serial.println(String(numNewMessages));
35+
36+
for(int i=0; i<numNewMessages; i++) {
37+
String chat_id = String(bot.messages[i].chat_id);
38+
String text = bot.messages[i].text;
39+
40+
if (text == "/start") {
41+
addSubscribedUser(chat_id);
42+
bot.sendMessage(chat_id, "Welcome", "");
43+
loadSubscribedUsersFile();
44+
}
45+
46+
if (text == "/stop") {
47+
bot.sendMessage(chat_id, "Thank you, we always waiting you back", "");
48+
removeSubscribedUser(chat_id);
49+
loadSubscribedUsersFile();
50+
}
51+
52+
if (text == "/testbulkmessage") {
53+
sendMessageToAllSubscribedUsers("Current temperature is 0.0C");
54+
}
55+
}
56+
}
57+
58+
bool loadSubscribedUsersFile() {
59+
File subscribedUsersFile = SPIFFS.open("/subscribed_users.json", "r");
60+
61+
if (!subscribedUsersFile) {
62+
Serial.println("Failed to open subscribed users file");
63+
return false;
64+
}
65+
66+
size_t size = subscribedUsersFile.size();
67+
68+
if (size > 1024) {
69+
Serial.println("Subscribed users file is too large");
70+
return false;
71+
}
72+
73+
// Allocate a buffer to store contents of the file.
74+
std::unique_ptr<char[]> buf(new char[size]);
75+
76+
// We don't use String here because ArduinoJson library requires the input
77+
// buffer to be mutable. If you don't use ArduinoJson, you may as well
78+
// use configFile.readString instead.
79+
subscribedUsersFile.readBytes(buf.get(), size);
80+
81+
StaticJsonBuffer<200> jsonBuffer;
82+
JsonObject& subscribed_users = jsonBuffer.parseObject(buf.get());
83+
84+
if (!subscribed_users.success()) {
85+
Serial.println("Failed to parse subscribed users file");
86+
return false;
87+
}
88+
89+
subscribedUsersFile.close();
90+
91+
subscribed_users.printTo(Serial);
92+
93+
return true;
94+
}
95+
96+
bool addSubscribedUser(String chat_id) {
97+
File subscribedUsersFile = SPIFFS.open("/subscribed_users.json", "w");
98+
99+
if (!subscribedUsersFile) {
100+
Serial.println("Failed to open subscribed users file for writing");
101+
return false;
102+
}
103+
104+
subscribed_users[chat_id] = 1;
105+
106+
subscribed_users.printTo(subscribedUsersFile);
107+
108+
subscribedUsersFile.close();
109+
110+
return true;
111+
}
112+
113+
bool removeSubscribedUser(String chat_id) {
114+
File subscribedUsersFile = SPIFFS.open("/subscribed_users.json", "w");
115+
116+
if (!subscribedUsersFile) {
117+
Serial.println("Failed to open subscribed users file for writing");
118+
return false;
119+
}
120+
121+
subscribed_users.remove(chat_id);
122+
123+
subscribed_users.printTo(subscribedUsersFile);
124+
125+
subscribedUsersFile.close();
126+
127+
return true;
128+
}
129+
130+
void sendMessageToAllSubscribedUsers(String message) {
131+
int users_processed = 0;
132+
133+
for(JsonObject::iterator it=subscribed_users.begin(); it!=subscribed_users.end(); ++it)
134+
{
135+
users_processed++;
136+
137+
if (users_processed < messages_limit_per_second) {
138+
const char* chat_id = it->key;
139+
bot.sendMessage(chat_id, message, "");
140+
} else {
141+
delay(bulk_messages_mtbs);
142+
users_processed = 0;
143+
}
144+
}
145+
}
146+
147+
void setup() {
148+
Serial.begin(9600);
149+
150+
if (!SPIFFS.begin()) {
151+
Serial.println("Failed to mount file system");
152+
return;
153+
}
154+
155+
loadSubscribedUsersFile();
156+
157+
// Set WiFi to station mode and disconnect from an AP if it was Previously
158+
// connected
159+
WiFi.disconnect();
160+
delay(100);
161+
// attempt to connect to Wifi network:
162+
Serial.print("Connecting Wifi: ");
163+
Serial.println(ssid);
164+
WiFi.begin(ssid, password);
165+
166+
while (WiFi.status() != WL_CONNECTED) {
167+
Serial.print(".");
168+
delay(500);
169+
}
170+
171+
Serial.println("");
172+
Serial.println("WiFi connected");
173+
Serial.println("IP address: ");
174+
IPAddress ip = WiFi.localIP();
175+
Serial.println(ip);
176+
}
177+
178+
void loop() {
179+
if (millis() > Bot_lasttime + Bot_mtbs) {
180+
int numNewMessages = bot.getUpdates(bot.last_message_recived + 1);
181+
182+
while(numNewMessages) {
183+
Serial.println("got response");
184+
handleNewMessages(numNewMessages);
185+
numNewMessages = bot.getUpdates(bot.last_message_recived + 1);
186+
}
187+
188+
Bot_lasttime = millis();
189+
}
190+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
You may copy, distribute and modify the software provided that modifications are described and licensed for free under [LGPL-3](http://www.gnu.org/licenses/lgpl-3.0.html). Derivatives works (including modifications or anything statically linked to the library) can only be redistributed under [LGPL-3](http://www.gnu.org/licenses/lgpl-3.0.html), but applications that use the library don't have to be.

0 commit comments

Comments
 (0)