From 49f11841be00ffcb367e7aa5178d98b6e8bb65b9 Mon Sep 17 00:00:00 2001 From: Dmitry Borisenko <49808844+DmitryBorisenko33@users.noreply.github.com> Date: Wed, 2 Dec 2020 04:50:29 +0300 Subject: [PATCH] Telegram --- data/config.json | 1 + data/set.telegram.json | 31 +++++++++++++++++++++++++------ include/Telegram.h | 1 + src/Telegram.cpp | 33 ++++++++++++++++++++------------- src/Web.cpp | 13 +++++++++++-- 5 files changed, 58 insertions(+), 21 deletions(-) diff --git a/data/config.json b/data/config.json index d5ebf98f..20cb7760 100644 --- a/data/config.json +++ b/data/config.json @@ -15,6 +15,7 @@ "scen": "1", "telegramApi": "1416711569:AAEI0j83GmXqwzb_gnK1B0Am0gDwZoJt5xo", "telegonof": "0", + "teleginput":"0", "weblogin": "admin", "webpass": "admin", "snaUdp": "0", diff --git a/data/set.telegram.json b/data/set.telegram.json index 76560093..43db41e8 100644 --- a/data/set.telegram.json +++ b/data/set.telegram.json @@ -21,30 +21,49 @@ }, { "type": "checkbox", - "name": "tel", + "name": "telegonof", "title": "Включить телеграм", - "action": "/set?telegonof=[[tel]]", + "action": "/set?telegonof=[[telegonof]]", "state": "{{telegonof}}" }, { "type": "hr" }, + { + "type": "checkbox", + "name": "teleginput", + "title": "Включить прием входящих сообщений", + "action": "/set?teleginput=[[teleginput]]", + "state": "{{teleginput}}" + }, + { + "type": "hr" + }, { "type": "h4", - "style": "width:40%;float:left;", - "title": "Telegram API token:" + "title": "Telegram chat ID" + }, + { + "type": "input", + "title": "", + "name": "chatId-arg", + "state": "{{chatId}}" + }, + + { + "type": "h4", + "title": "Telegram API token" }, { "type": "input", "title": "", "name": "telegramApi-arg", - "style": "width:60%;float:right", "state": "{{telegramApi}}" }, { "type": "button", "title": "{{ButSave}}", - "action": "set?telegramApi=[[telegramApi-arg]]", + "action": "set?telegramApi=[[telegramApi-arg]]&chatId=[[chatId-arg]]", "class": "btn btn-block btn-default", "style": "width:100%;display:inline" }, diff --git a/include/Telegram.h b/include/Telegram.h index 9fd18857..0571fd40 100644 --- a/include/Telegram.h +++ b/include/Telegram.h @@ -5,5 +5,6 @@ extern void sendTelegramMsg(); extern void telegramInit(); extern void handleTelegram(); extern bool isTelegramEnabled(); +extern bool isTelegramInputOn(); extern void telegramMsgParse(String msg); extern String returnListOfParams(); \ No newline at end of file diff --git a/src/Telegram.cpp b/src/Telegram.cpp index bd478505..cc560330 100644 --- a/src/Telegram.cpp +++ b/src/Telegram.cpp @@ -25,17 +25,19 @@ void telegramInit() { void handleTelegram() { if (telegramInitBeen) { if (isTelegramEnabled()) { - TBMessage msg; - static unsigned long prevMillis; - unsigned long currentMillis = millis(); - unsigned long difference = currentMillis - prevMillis; - if (difference >= 5000) { - 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(); - telegramMsgParse(String(msg.text)); + if (isTelegramInputOn()) { + TBMessage msg; + static unsigned long prevMillis; + unsigned long currentMillis = millis(); + unsigned long difference = currentMillis - prevMillis; + if (difference >= 10000) { + 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(); + telegramMsgParse(String(msg.text)); + } } } } @@ -76,16 +78,21 @@ void sendTelegramMsg() { myBot->sendMessage(jsonReadInt(configSetupJson, "chatId"), msg); SerialPrint("<-", "Telegram", "chat ID: " + String(jsonReadInt(configSetupJson, "chatId")) + ", msg: " + msg); } - } else if (type == "2") { + } + else if (type == "2") { myBot->sendMessage(jsonReadInt(configSetupJson, "chatId"), msg); SerialPrint("<-", "Telegram", "chat ID: " + String(jsonReadInt(configSetupJson, "chatId")) + ", msg: " + msg); - } + } } bool isTelegramEnabled() { return jsonReadBool(configSetupJson, "telegonof"); } +bool isTelegramInputOn() { + return jsonReadBool(configSetupJson, "teleginput"); +} + String returnListOfParams() { String cmdStr = readFile(DEVICE_CONFIG_FILE, 4096); diff --git a/src/Web.cpp b/src/Web.cpp index d3b68397..c571ccaf 100644 --- a/src/Web.cpp +++ b/src/Web.cpp @@ -229,14 +229,23 @@ void web_init() { //==============================push settings============================================= if (request->hasArg("telegramApi")) { jsonWriteStr(configSetupJson, "telegramApi", request->getParam("telegramApi")->value()); - //telegramInit(); + saveConfig(); + request->send(200); + } + if (request->hasArg("chatId")) { + jsonWriteStr(configSetupJson, "chatId", request->getParam("chatId")->value()); saveConfig(); request->send(200); } if (request->hasArg("telegonof")) { bool value = request->getParam("telegonof")->value().toInt(); jsonWriteBool(configSetupJson, "telegonof", value); - //telegramInit(); + saveConfig(); + request->send(200); + } + if (request->hasArg("teleginput")) { + bool value = request->getParam("teleginput")->value().toInt(); + jsonWriteBool(configSetupJson, "teleginput", value); saveConfig(); request->send(200); }