From 1f69019bb33a196923fb96f66ea0f8152da31729 Mon Sep 17 00:00:00 2001 From: Dmitry Borisenko <49808844+DmitryBorisenko33@users.noreply.github.com> Date: Thu, 5 Nov 2020 02:23:08 +0300 Subject: [PATCH] 265 Telegram support added --- data/config.json | 1 + data/set.device.json | 2 +- data/set.telegram.json | 18 ++++++++++-- include/Consts.h | 4 +-- include/Global.h | 1 + include/Telegram.h | 6 ++-- src/BufferExecute.cpp | 1 + src/Global.cpp | 1 + src/Telegram.cpp | 67 +++++++++++++++++++++++++++--------------- src/Web.cpp | 11 +++++-- 10 files changed, 79 insertions(+), 33 deletions(-) diff --git a/data/config.json b/data/config.json index 3782f65d..48676490 100644 --- a/data/config.json +++ b/data/config.json @@ -14,6 +14,7 @@ "mqttPass": "23ri22se32", "scen": "1", "telegramApi": "1425283609:AAFf3YO8ouCJF23kU8CMiU7XXy21MFhbn9w", + "telegonof": "0", "weblogin": "admin", "webpass": "admin", "udponoff": "1", diff --git a/data/set.device.json b/data/set.device.json index 9dd95abc..2b24c230 100644 --- a/data/set.device.json +++ b/data/set.device.json @@ -43,7 +43,7 @@ }, { "type": "h4", - "title": "LittleFS version: 264" + "title": "LittleFS version: 265" }, { "type": "hr" diff --git a/data/set.telegram.json b/data/set.telegram.json index bdeef290..e4a22bb7 100644 --- a/data/set.telegram.json +++ b/data/set.telegram.json @@ -19,16 +19,26 @@ { "type": "hr" }, + { + "type": "checkbox", + "name": "tel", + "title": "Включить телеграм", + "action": "/set?telegonof=[[tel]]", + "state": "{{telegonof}}" + }, + { + "type": "hr" + }, { "type": "h4", - "style": "width:60%;float:left;", + "style": "width:40%;float:left;", "title": "Telegram API token:" }, { "type": "input", "title": "", "name": "telegramApi-arg", - "style": "width:40%;float:right", + "style": "width:60%;float:right", "state": "{{telegramApi}}" }, { @@ -40,6 +50,10 @@ }, { "type": "hr" + }, + { + "type": "text", + "title": "

После настройки telegram перезагрузите устройство что бы изменения вступили в силу

" } ] } \ No newline at end of file diff --git a/include/Consts.h b/include/Consts.h index cdfcc4f6..89f366ba 100644 --- a/include/Consts.h +++ b/include/Consts.h @@ -3,11 +3,11 @@ //===========Firmware============================================================================================================================================= #ifdef ESP8266 #define FIRMWARE_NAME "esp8266-iotm" -#define FIRMWARE_VERSION 264 +#define FIRMWARE_VERSION 265 #endif #ifdef ESP32 #define FIRMWARE_NAME "esp32-iotm" -#define FIRMWARE_VERSION 264 +#define FIRMWARE_VERSION 265 #endif #define FLASH_4MB true diff --git a/include/Global.h b/include/Global.h index 3b1d295a..11de486a 100644 --- a/include/Global.h +++ b/include/Global.h @@ -47,6 +47,7 @@ extern Bounce* buttons; //Global vars extern boolean just_load; +extern boolean telegramInitBeen; // Json extern String configSetupJson; //все настройки diff --git a/include/Telegram.h b/include/Telegram.h index d5645454..1524373e 100644 --- a/include/Telegram.h +++ b/include/Telegram.h @@ -1,6 +1,6 @@ #pragma once - - +extern void sendTelegramMsg(); extern void telegramInit(); -extern void handleTelegram(); \ No newline at end of file +extern void handleTelegram(); +extern bool isTelegramEnabled(); \ No newline at end of file diff --git a/src/BufferExecute.cpp b/src/BufferExecute.cpp index 84873f93..ecba8a91 100644 --- a/src/BufferExecute.cpp +++ b/src/BufferExecute.cpp @@ -93,6 +93,7 @@ void csvCmdExecute(String& cmdStr) { else if (order == F("impuls-out")) { sCmd.addCommand(order.c_str(), impuls); } + sCmd.readStr(buf); diff --git a/src/Global.cpp b/src/Global.cpp index a6d8ae9e..75203c97 100644 --- a/src/Global.cpp +++ b/src/Global.cpp @@ -18,6 +18,7 @@ DallasTemperature sensors; */ boolean just_load = true; +boolean telegramInitBeen = false; // Json String configSetupJson = "{}"; diff --git a/src/Telegram.cpp b/src/Telegram.cpp index 9e88dbf6..a79524c2 100644 --- a/src/Telegram.cpp +++ b/src/Telegram.cpp @@ -1,35 +1,56 @@ #include "Telegram.h" #include "Global.h" - CTBot* myBot{ nullptr }; void telegramInit() { - String token = jsonReadStr(configSetupJson, "telegramApi"); - if (!myBot) { - myBot = new CTBot(); - } - myBot->setTelegramToken(token); - if (myBot->testConnection()) { - SerialPrint("I", "Telegram", "Connected"); - } - else { - SerialPrint("E", "Telegram", "Not connected"); + if (isTelegramEnabled()) { + telegramInitBeen = true; + sCmd.addCommand("telegram", sendTelegramMsg); + String token = jsonReadStr(configSetupJson, "telegramApi"); + if (!myBot) { + myBot = new CTBot(); + } + myBot->setTelegramToken(token); + myBot->enableUTF8Encoding(true); + if (myBot->testConnection()) { + SerialPrint("I", "Telegram", "Connected"); + } + else { + SerialPrint("E", "Telegram", "Not connected"); + } } } void handleTelegram() { - - TBMessage msg; - - static unsigned long prevMillis; - unsigned long currentMillis = millis(); - unsigned long difference = currentMillis - prevMillis; - if (difference >= 1000) { - prevMillis = millis(); - if (myBot->getNewMessage(msg)) { - Serial.println(msg.text); - myBot->sendMessage(msg.sender.id, msg.text); - } + if (telegramInitBeen) { + if (isTelegramEnabled()) { + TBMessage msg; + static unsigned long prevMillis; + unsigned long currentMillis = millis(); + unsigned long difference = currentMillis - prevMillis; + if (difference >= 1000) { + prevMillis = millis(); + if (myBot->getNewMessage(msg)) { + SerialPrint("->", "Telegram", "chat ID: " + String(msg.sender.id) + ", msg: " + String(msg.text)); + jsonWriteInt(configSetupJson, "chatId", msg.sender.id); + saveConfig(); + orderBuf += String(msg.text) + ","; + myBot->sendMessage(jsonReadInt(configSetupJson, "chatId"), msg.text); + SerialPrint("<-", "Telegram", "chat ID: " + String(jsonReadInt(configSetupJson, "chatId")) + ", msg: " + String(msg.text)); + } + } + } } +} + +void sendTelegramMsg() { + String msg = sCmd.next(); + msg.replace("#"," "); + myBot->sendMessage(jsonReadInt(configSetupJson, "chatId"), msg); + SerialPrint("<-", "Telegram", "chat ID: " + String(jsonReadInt(configSetupJson, "chatId")) + ", msg: " + msg); +} + +bool isTelegramEnabled() { + return jsonReadBool(configSetupJson, "telegonof"); } \ No newline at end of file diff --git a/src/Web.cpp b/src/Web.cpp index 55a94df4..d2f53825 100644 --- a/src/Web.cpp +++ b/src/Web.cpp @@ -1,10 +1,10 @@ #include "Web.h" - #include "Class/NotAsync.h" #include "Global.h" #include "Init.h" #include "ItemsList.h" #include "items/LoggingClass.h" +#include "Telegram.h" bool parseRequestForPreset(AsyncWebServerRequest* request, uint8_t& preset) { if (request->hasArg("preset")) { @@ -196,7 +196,14 @@ void web_init() { //==============================push settings============================================= if (request->hasArg("telegramApi")) { jsonWriteStr(configSetupJson, "telegramApi", request->getParam("telegramApi")->value()); - + //telegramInit(); + saveConfig(); + request->send(200); + } + if (request->hasArg("telegonof")) { + bool value = request->getParam("telegonof")->value().toInt(); + jsonWriteBool(configSetupJson, "telegonof", value); + //telegramInit(); saveConfig(); request->send(200); }