|
@@ -49,18 +49,17 @@
|
49
|
49
|
#ifdef TELEGRAM_TOKEN
|
50
|
50
|
|
51
|
51
|
#include <WiFiClientSecure.h>
|
52
|
|
-#include <UniversalTelegramBot.h>
|
|
52
|
+#include <AsyncTelegram2.h>
|
53
|
53
|
|
54
|
54
|
//#define TELEGRAM_LOG_TIMINGS
|
55
|
55
|
|
56
|
56
|
#if defined(ARDUINO_ARCH_ESP8266)
|
57
|
|
-X509List cert(TELEGRAM_CERTIFICATE_ROOT);
|
|
57
|
+X509List cert(telegram_cert);
|
58
|
58
|
#endif
|
59
|
59
|
|
60
|
60
|
WiFiClientSecure secured_client;
|
61
|
|
-UniversalTelegramBot bot(TELEGRAM_TOKEN, secured_client);
|
62
|
|
-unsigned long last_telegram_time = 0;
|
63
|
|
-String trusted_chat_ids[] = TRUSTED_IDS;
|
|
61
|
+AsyncTelegram2 bot(secured_client);
|
|
62
|
+int64_t trusted_chat_ids[] = TRUSTED_IDS;
|
64
|
63
|
|
65
|
64
|
enum telegram_state {
|
66
|
65
|
BOT_IDLE,
|
|
@@ -70,7 +69,7 @@ enum telegram_state {
|
70
|
69
|
};
|
71
|
70
|
|
72
|
71
|
enum telegram_state bot_state = BOT_IDLE;
|
73
|
|
-String bot_lock = "";
|
|
72
|
+int64_t bot_lock = 0;
|
74
|
73
|
|
75
|
74
|
#endif // TELEGRAM_TOKEN
|
76
|
75
|
|
|
@@ -146,7 +145,7 @@ void handleGpioTest() {
|
146
|
145
|
void wifi_broadcast_state_change(const char *s) {
|
147
|
146
|
#ifdef TELEGRAM_TOKEN
|
148
|
147
|
for (int n = 0; n < (sizeof(trusted_chat_ids) / sizeof(trusted_chat_ids[0])); n++) {
|
149
|
|
- bot.sendMessage(trusted_chat_ids[n], "New state: " + String(s), "");
|
|
148
|
+ bot.sendTo(trusted_chat_ids[n], "New state: " + String(s));
|
150
|
149
|
}
|
151
|
150
|
#endif // TELEGRAM_TOKEN
|
152
|
151
|
|
|
@@ -158,14 +157,6 @@ void wifi_broadcast_state_change(const char *s) {
|
158
|
157
|
|
159
|
158
|
#ifdef TELEGRAM_TOKEN
|
160
|
159
|
|
161
|
|
-unsigned long telegram_update_interval() {
|
162
|
|
- if (bot_state == BOT_IDLE) {
|
163
|
|
- return TELEGRAM_UPDATE_INTERVAL_SLOW;
|
164
|
|
- } else {
|
165
|
|
- return TELEGRAM_UPDATE_INTERVAL_FAST;
|
166
|
|
- }
|
167
|
|
-}
|
168
|
|
-
|
169
|
160
|
String telegram_help() {
|
170
|
161
|
String s = "Usage:\n";
|
171
|
162
|
s += "Send /auto and follow prompts.\n";
|
|
@@ -175,68 +166,68 @@ String telegram_help() {
|
175
|
166
|
return s;
|
176
|
167
|
}
|
177
|
168
|
|
178
|
|
-void telegram_handle_message(int message_id) {
|
|
169
|
+void telegram_handle_message(TBMessage &msg) {
|
179
|
170
|
if (!sm_is_idle()) {
|
180
|
171
|
debug.println("Telegram: message while machine in use");
|
181
|
172
|
|
182
|
|
- if (bot.messages[message_id].text == "/abort") {
|
|
173
|
+ if (msg.text == "/abort") {
|
183
|
174
|
sm_bot_abort();
|
184
|
|
- bot.sendMessage(bot.messages[message_id].chat_id, "Aborted current cycle!", "");
|
|
175
|
+ bot.sendTo(msg.chatId, "Aborted current cycle!");
|
185
|
176
|
} else {
|
186
|
|
- bot.sendMessage(bot.messages[message_id].chat_id, "Machine is already in use.\nPlease try again later.", "");
|
|
177
|
+ bot.sendTo(msg.chatId, "Machine is already in use.\nPlease try again later.");
|
187
|
178
|
}
|
188
|
179
|
|
189
|
180
|
return;
|
190
|
181
|
}
|
191
|
182
|
|
192
|
|
- if ((bot_state == BOT_IDLE) && (bot_lock == "")) {
|
193
|
|
- bot_lock = bot.messages[message_id].chat_id;
|
|
183
|
+ if ((bot_state == BOT_IDLE) && (bot_lock == 0)) {
|
|
184
|
+ bot_lock = msg.chatId;
|
194
|
185
|
debug.println("Telegram: locked to " + bot_lock);
|
195
|
186
|
}
|
196
|
187
|
|
197
|
|
- if (bot_lock != bot.messages[message_id].chat_id) {
|
198
|
|
- debug.println("Telegram: bot locked. abort for chat " + bot.messages[message_id].chat_id);
|
199
|
|
- bot.sendMessage(bot.messages[message_id].chat_id, "Bot is already in use.\nPlease try again later.", "");
|
|
188
|
+ if (bot_lock != msg.chatId) {
|
|
189
|
+ debug.println("Telegram: bot locked. abort for chat " + msg.chatId);
|
|
190
|
+ bot.sendTo(msg.chatId, "Bot is already in use.\nPlease try again later.");
|
200
|
191
|
return;
|
201
|
192
|
}
|
202
|
193
|
|
203
|
194
|
if (bot_state == BOT_IDLE) {
|
204
|
|
- if (bot.messages[message_id].text == "/auto") {
|
|
195
|
+ if (msg.text == "/auto") {
|
205
|
196
|
String s = "Please enter fertilizer numbers.\n";
|
206
|
197
|
s += "Valid numbers: 1 to " + String(PUMP_COUNT) + "\n";
|
207
|
198
|
s += "Send /none to skip.\n";
|
208
|
199
|
s += "Send /abort to cancel.";
|
209
|
200
|
bot_ferts.clear();
|
210
|
201
|
bot_state = BOT_ASKED_FERT;
|
211
|
|
- bot.sendMessage(bot.messages[message_id].chat_id, s, "");
|
212
|
|
- } else if (bot.messages[message_id].text == "/abort") {
|
213
|
|
- bot_lock = "";
|
214
|
|
- bot.sendMessage(bot.messages[message_id].chat_id, "Nothing to abort.", "");
|
|
202
|
+ bot.sendTo(msg.chatId, s);
|
|
203
|
+ } else if (msg.text == "/abort") {
|
|
204
|
+ bot_lock = 0;
|
|
205
|
+ bot.sendTo(msg.chatId, "Nothing to abort.");
|
215
|
206
|
} else {
|
216
|
|
- bot_lock = "";
|
217
|
|
- bot.sendMessage(bot.messages[message_id].chat_id, telegram_help(), "");
|
|
207
|
+ bot_lock = 0;
|
|
208
|
+ bot.sendTo(msg.chatId, telegram_help());
|
218
|
209
|
}
|
219
|
210
|
} else if (bot_state == BOT_ASKED_FERT) {
|
220
|
|
- if (bot.messages[message_id].text == "/abort") {
|
|
211
|
+ if (msg.text == "/abort") {
|
221
|
212
|
bot_state = BOT_IDLE;
|
222
|
|
- bot_lock = "";
|
223
|
|
- bot.sendMessage(bot.messages[message_id].chat_id, "Aborted.", "");
|
|
213
|
+ bot_lock = 0;
|
|
214
|
+ bot.sendTo(msg.chatId, "Aborted.");
|
224
|
215
|
return;
|
225
|
|
- } else if (bot.messages[message_id].text != "/none") {
|
|
216
|
+ } else if (msg.text != "/none") {
|
226
|
217
|
String buff;
|
227
|
|
- for (int i = 0; i < bot.messages[message_id].text.length() + 1; i++) {
|
228
|
|
- if ((i == bot.messages[message_id].text.length()) || (bot.messages[message_id].text[i] == ' ')) {
|
|
218
|
+ for (int i = 0; i < msg.text.length() + 1; i++) {
|
|
219
|
+ if ((i == msg.text.length()) || (msg.text[i] == ' ')) {
|
229
|
220
|
if (buff.length() > 0) {
|
230
|
221
|
int n = buff.toInt() - 1;
|
231
|
222
|
buff = "";
|
232
|
223
|
bot_ferts.set(n);
|
233
|
224
|
}
|
234
|
|
- } else if ((bot.messages[message_id].text[i] >= '0') && (bot.messages[message_id].text[i] <= '9')) {
|
235
|
|
- buff += bot.messages[message_id].text[i];
|
|
225
|
+ } else if ((msg.text[i] >= '0') && (msg.text[i] <= '9')) {
|
|
226
|
+ buff += msg.text[i];
|
236
|
227
|
} else {
|
237
|
228
|
bot_state = BOT_IDLE;
|
238
|
|
- bot_lock = "";
|
239
|
|
- bot.sendMessage(bot.messages[message_id].chat_id, "Invalid input.\nAborted.", "");
|
|
229
|
+ bot_lock = 0;
|
|
230
|
+ bot.sendTo(msg.chatId, "Invalid input.\nAborted.");
|
240
|
231
|
return;
|
241
|
232
|
}
|
242
|
233
|
}
|
|
@@ -247,45 +238,45 @@ void telegram_handle_message(int message_id) {
|
247
|
238
|
s += "Send /abort to cancel.";
|
248
|
239
|
bot_plants.clear();
|
249
|
240
|
bot_state = BOT_ASKED_PLANTS;
|
250
|
|
- bot.sendMessage(bot.messages[message_id].chat_id, s, "");
|
|
241
|
+ bot.sendTo(msg.chatId, s);
|
251
|
242
|
} else if (bot_state == BOT_ASKED_PLANTS) {
|
252
|
|
- if (bot.messages[message_id].text == "/abort") {
|
|
243
|
+ if (msg.text == "/abort") {
|
253
|
244
|
bot_state = BOT_IDLE;
|
254
|
|
- bot_lock = "";
|
255
|
|
- bot.sendMessage(bot.messages[message_id].chat_id, "Aborted.", "");
|
|
245
|
+ bot_lock = 0;
|
|
246
|
+ bot.sendTo(msg.chatId, "Aborted.");
|
256
|
247
|
return;
|
257
|
248
|
}
|
258
|
249
|
|
259
|
250
|
String buff;
|
260
|
|
- for (int i = 0; i < bot.messages[message_id].text.length() + 1; i++) {
|
261
|
|
- if ((i == bot.messages[message_id].text.length()) || (bot.messages[message_id].text[i] == ' ')) {
|
|
251
|
+ for (int i = 0; i < msg.text.length() + 1; i++) {
|
|
252
|
+ if ((i == msg.text.length()) || (msg.text[i] == ' ')) {
|
262
|
253
|
if (buff.length() > 0) {
|
263
|
254
|
int n = buff.toInt() - 1;
|
264
|
255
|
buff = "";
|
265
|
256
|
bot_plants.set(n);
|
266
|
257
|
}
|
267
|
|
- } else if ((bot.messages[message_id].text[i] >= '0') && (bot.messages[message_id].text[i] <= '9')) {
|
268
|
|
- buff += bot.messages[message_id].text[i];
|
|
258
|
+ } else if ((msg.text[i] >= '0') && (msg.text[i] <= '9')) {
|
|
259
|
+ buff += msg.text[i];
|
269
|
260
|
} else {
|
270
|
261
|
bot_state = BOT_IDLE;
|
271
|
|
- bot_lock = "";
|
272
|
|
- bot.sendMessage(bot.messages[message_id].chat_id, "Invalid input.\nAborted.", "");
|
|
262
|
+ bot_lock = 0;
|
|
263
|
+ bot.sendTo(msg.chatId, "Invalid input.\nAborted.");
|
273
|
264
|
return;
|
274
|
265
|
}
|
275
|
266
|
}
|
276
|
267
|
|
277
|
268
|
if (bot_plants.countSet() <= 0) {
|
278
|
269
|
bot_state = BOT_IDLE;
|
279
|
|
- bot_lock = "";
|
280
|
|
- bot.sendMessage(bot.messages[message_id].chat_id, "No plants selected.\nAborted.", "");
|
|
270
|
+ bot_lock = 0;
|
|
271
|
+ bot.sendTo(msg.chatId, "No plants selected.\nAborted.");
|
281
|
272
|
return;
|
282
|
273
|
}
|
283
|
274
|
|
284
|
275
|
#ifdef FULLAUTO_MIN_PLANT_COUNT
|
285
|
276
|
if (bot_plants.countSet() < FULLAUTO_MIN_PLANT_COUNT) {
|
286
|
277
|
bot_state = BOT_IDLE;
|
287
|
|
- bot_lock = "";
|
288
|
|
- bot.sendMessage(bot.messages[message_id].chat_id, "Select at least " + String(FULLAUTO_MIN_PLANT_COUNT) + " plants.\nAborted.", "");
|
|
278
|
+ bot_lock = 0;
|
|
279
|
+ bot.sendTo(msg.chatId, "Select at least " + String(FULLAUTO_MIN_PLANT_COUNT) + " plants.\nAborted.");
|
289
|
280
|
return;
|
290
|
281
|
}
|
291
|
282
|
#endif
|
|
@@ -304,46 +295,46 @@ void telegram_handle_message(int message_id) {
|
304
|
295
|
}
|
305
|
296
|
s += "\nOk? Send /begin to start or /abort to cancel.";
|
306
|
297
|
bot_state = BOT_ASKED_CONFIRM;
|
307
|
|
- bot.sendMessage(bot.messages[message_id].chat_id, s, "");
|
|
298
|
+ bot.sendTo(msg.chatId, s);
|
308
|
299
|
} else if (bot_state == BOT_ASKED_CONFIRM) {
|
309
|
|
- if (bot.messages[message_id].text == "/abort") {
|
|
300
|
+ if (msg.text == "/abort") {
|
310
|
301
|
bot_state = BOT_IDLE;
|
311
|
|
- bot_lock = "";
|
312
|
|
- bot.sendMessage(bot.messages[message_id].chat_id, "Aborted.", "");
|
313
|
|
- } else if (bot.messages[message_id].text == "/begin") {
|
|
302
|
+ bot_lock = 0;
|
|
303
|
+ bot.sendTo(msg.chatId, "Aborted.");
|
|
304
|
+ } else if (msg.text == "/begin") {
|
314
|
305
|
bot_state = BOT_IDLE;
|
315
|
|
- bot_lock = "";
|
316
|
|
- bot.sendMessage(bot.messages[message_id].chat_id, "Auto watering cycle started.", "");
|
|
306
|
+ bot_lock = 0;
|
|
307
|
+ bot.sendTo(msg.chatId, "Auto watering cycle started.");
|
317
|
308
|
sm_bot_start_auto(bot_ferts, bot_plants);
|
318
|
309
|
} else {
|
319
|
310
|
bot_state = BOT_IDLE;
|
320
|
|
- bot_lock = "";
|
321
|
|
- bot.sendMessage(bot.messages[message_id].chat_id, "Unknown message.\nAborted.", "");
|
|
311
|
+ bot_lock = 0;
|
|
312
|
+ bot.sendTo(msg.chatId, "Unknown message.\nAborted.");
|
322
|
313
|
}
|
323
|
314
|
} else {
|
324
|
315
|
debug.println("Telegram: invalid state");
|
325
|
316
|
bot_state = BOT_IDLE;
|
326
|
|
- bot_lock = "";
|
327
|
|
- bot.sendMessage(bot.messages[message_id].chat_id, "Internal error.\nPlease try again.", "");
|
|
317
|
+ bot_lock = 0;
|
|
318
|
+ bot.sendTo(msg.chatId, "Internal error.\nPlease try again.");
|
328
|
319
|
}
|
329
|
320
|
}
|
330
|
321
|
|
331
|
|
-void telegram_handler(int message_id) {
|
332
|
|
- debug.println("Telegram: rx " + bot.messages[message_id].chat_id + " \"" + bot.messages[message_id].text + "\"");
|
|
322
|
+void telegram_handler(TBMessage &msg) {
|
|
323
|
+ debug.println("Telegram: rx " + String((long int)msg.chatId) + " \"" + msg.text + "\"");
|
333
|
324
|
|
334
|
325
|
bool found = false;
|
335
|
326
|
for (int n = 0; n < (sizeof(trusted_chat_ids) / sizeof(trusted_chat_ids[0])); n++) {
|
336
|
|
- if (trusted_chat_ids[n] == bot.messages[message_id].chat_id) {
|
|
327
|
+ if (trusted_chat_ids[n] == msg.chatId) {
|
337
|
328
|
found = true;
|
338
|
329
|
break;
|
339
|
330
|
}
|
340
|
331
|
}
|
341
|
332
|
if (!found) {
|
342
|
|
- bot.sendMessage(bot.messages[message_id].chat_id, "Sorry, not authorized!", "");
|
|
333
|
+ bot.sendTo(msg.chatId, "Sorry, not authorized!");
|
343
|
334
|
return;
|
344
|
335
|
}
|
345
|
336
|
|
346
|
|
- telegram_handle_message(message_id);
|
|
337
|
+ telegram_handle_message(msg);
|
347
|
338
|
}
|
348
|
339
|
|
349
|
340
|
void telegram_poll() {
|
|
@@ -351,10 +342,9 @@ void telegram_poll() {
|
351
|
342
|
unsigned long start = millis();
|
352
|
343
|
#endif // TELEGRAM_LOG_TIMINGS
|
353
|
344
|
|
354
|
|
- while (int count = bot.getUpdates(bot.last_message_received + 1)) {
|
355
|
|
- for (int i = 0; i < count; i++) {
|
356
|
|
- telegram_handler(i);
|
357
|
|
- }
|
|
345
|
+ TBMessage msg;
|
|
346
|
+ while (bot.getNewMessage(msg)) {
|
|
347
|
+ telegram_handler(msg);
|
358
|
348
|
}
|
359
|
349
|
|
360
|
350
|
#ifdef TELEGRAM_LOG_TIMINGS
|
|
@@ -365,7 +355,7 @@ void telegram_poll() {
|
365
|
355
|
|
366
|
356
|
void telegram_hello() {
|
367
|
357
|
for (int n = 0; n < (sizeof(trusted_chat_ids) / sizeof(trusted_chat_ids[0])); n++) {
|
368
|
|
- bot.sendMessage(trusted_chat_ids[n], "Giess-o-mat v" FIRMWARE_VERSION " initialized.\nSend /auto to begin.", "");
|
|
358
|
+ bot.sendTo(trusted_chat_ids[n], "Giess-o-mat v" FIRMWARE_VERSION " initialized.\nSend /auto to begin.");
|
369
|
359
|
}
|
370
|
360
|
}
|
371
|
361
|
#endif // TELEGRAM_TOKEN
|
|
@@ -1058,9 +1048,7 @@ void handleRoot() {
|
1058
|
1048
|
message += F("<p>\n");
|
1059
|
1049
|
#ifdef TELEGRAM_TOKEN
|
1060
|
1050
|
message += F("Telegram: ");
|
1061
|
|
- message += TELEGRAM_UPDATE_INTERVAL_SLOW;
|
1062
|
|
- message += F("ms / ");
|
1063
|
|
- message += TELEGRAM_UPDATE_INTERVAL_FAST;
|
|
1051
|
+ message += TELEGRAM_UPDATE_INTERVAL;
|
1064
|
1052
|
message += F("ms\n");
|
1065
|
1053
|
#else
|
1066
|
1054
|
message += F("Telegram bot not enabled!\n");
|
|
@@ -1273,7 +1261,7 @@ void wifi_setup() {
|
1273
|
1261
|
WiFi.begin(WIFI_SSID, WIFI_PW);
|
1274
|
1262
|
|
1275
|
1263
|
#ifdef TELEGRAM_TOKEN
|
1276
|
|
- secured_client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
|
|
1264
|
+ secured_client.setCACert(telegram_cert);
|
1277
|
1265
|
#endif // TELEGRAM_TOKEN
|
1278
|
1266
|
|
1279
|
1267
|
while (((ws = WiFi.status()) != WL_CONNECTED) && (connect_attempts < MAX_WIFI_CONNECT_ATTEMPTS)) {
|
|
@@ -1330,14 +1318,16 @@ void wifi_setup() {
|
1330
|
1318
|
debug.println(" done!");
|
1331
|
1319
|
debug.println("WiFi: time is " + String(now));
|
1332
|
1320
|
|
1333
|
|
- debug.println("WiFi: initializing Telegram");
|
1334
|
|
- const String commands = F("["
|
1335
|
|
- "{\"command\":\"auto\", \"description\":\"Start automatic watering cycle\"},"
|
1336
|
|
- "{\"command\":\"confirm\", \"description\":\"Proceed with any menu inputs\"},"
|
1337
|
|
- "{\"command\":\"none\", \"description\":\"Proceed without menu input\"},"
|
1338
|
|
- "{\"command\":\"abort\", \"description\":\"Cancel any menu inputs\"}"
|
1339
|
|
- "]");
|
1340
|
|
- bot.setMyCommands(commands);
|
|
1321
|
+ debug.print("WiFi: initializing Telegram... ");
|
|
1322
|
+ bot.setUpdateTime(TELEGRAM_UPDATE_INTERVAL);
|
|
1323
|
+ bot.setTelegramToken(TELEGRAM_TOKEN);
|
|
1324
|
+ bot.begin() ? debug.println("Ok") : debug.println("Error");
|
|
1325
|
+
|
|
1326
|
+ bot.setMyCommands("auto", "Start automatic watering cycle");
|
|
1327
|
+ bot.setMyCommands("confirm", "Proceed with any menu inputs");
|
|
1328
|
+ bot.setMyCommands("none", "Proceed without menu input");
|
|
1329
|
+ bot.setMyCommands("abort", "Cancel any menu inputs");
|
|
1330
|
+
|
1341
|
1331
|
telegram_hello();
|
1342
|
1332
|
#endif // TELEGRAM_TOKEN
|
1343
|
1333
|
|
|
@@ -1371,6 +1361,10 @@ void wifi_run() {
|
1371
|
1361
|
#ifdef ARDUINO_ARCH_ESP8266
|
1372
|
1362
|
MDNS.update();
|
1373
|
1363
|
#endif // ARDUINO_ARCH_ESP8266
|
|
1364
|
+
|
|
1365
|
+#ifdef TELEGRAM_TOKEN
|
|
1366
|
+ telegram_poll();
|
|
1367
|
+#endif // TELEGRAM_TOKEN
|
1374
|
1368
|
}
|
1375
|
1369
|
|
1376
|
1370
|
if ((millis() - last_websocket_update_time) >= WEBSOCKET_UPDATE_INTERVAL) {
|
|
@@ -1385,13 +1379,6 @@ void wifi_run() {
|
1385
|
1379
|
}
|
1386
|
1380
|
#endif // ENABLE_GPIO_TEST
|
1387
|
1381
|
|
1388
|
|
-#ifdef TELEGRAM_TOKEN
|
1389
|
|
- if ((millis() - last_telegram_time) >= telegram_update_interval()) {
|
1390
|
|
- telegram_poll();
|
1391
|
|
- last_telegram_time = millis();
|
1392
|
|
- }
|
1393
|
|
-#endif // TELEGRAM_TOKEN
|
1394
|
|
-
|
1395
|
1382
|
#ifdef MQTT_HOST
|
1396
|
1383
|
if (!mqtt.connected() && ((millis() - last_mqtt_reconnect_time) >= MQTT_RECONNECT_INTERVAL)) {
|
1397
|
1384
|
last_mqtt_reconnect_time = millis();
|