265 Telegram support added

This commit is contained in:
Dmitry Borisenko
2020-11-05 02:23:08 +03:00
parent 2cad740fc6
commit 1f69019bb3
10 changed files with 79 additions and 33 deletions

View File

@@ -14,6 +14,7 @@
"mqttPass": "23ri22se32", "mqttPass": "23ri22se32",
"scen": "1", "scen": "1",
"telegramApi": "1425283609:AAFf3YO8ouCJF23kU8CMiU7XXy21MFhbn9w", "telegramApi": "1425283609:AAFf3YO8ouCJF23kU8CMiU7XXy21MFhbn9w",
"telegonof": "0",
"weblogin": "admin", "weblogin": "admin",
"webpass": "admin", "webpass": "admin",
"udponoff": "1", "udponoff": "1",

View File

@@ -43,7 +43,7 @@
}, },
{ {
"type": "h4", "type": "h4",
"title": "LittleFS version: 264" "title": "LittleFS version: 265"
}, },
{ {
"type": "hr" "type": "hr"

View File

@@ -19,16 +19,26 @@
{ {
"type": "hr" "type": "hr"
}, },
{
"type": "checkbox",
"name": "tel",
"title": "Включить телеграм",
"action": "/set?telegonof=[[tel]]",
"state": "{{telegonof}}"
},
{
"type": "hr"
},
{ {
"type": "h4", "type": "h4",
"style": "width:60%;float:left;", "style": "width:40%;float:left;",
"title": "Telegram API token:" "title": "Telegram API token:"
}, },
{ {
"type": "input", "type": "input",
"title": "", "title": "",
"name": "telegramApi-arg", "name": "telegramApi-arg",
"style": "width:40%;float:right", "style": "width:60%;float:right",
"state": "{{telegramApi}}" "state": "{{telegramApi}}"
}, },
{ {
@@ -40,6 +50,10 @@
}, },
{ {
"type": "hr" "type": "hr"
},
{
"type": "text",
"title": "<div style='margin-top:10px;margin-bottom:10px;'><font color='black'><p style='border: 1px solid #DCDCDC; border-radius: 3px; background-color: #F5F5F5; padding: 10px;'>После настройки telegram перезагрузите устройство что бы изменения вступили в силу</p></font></div>"
} }
] ]
} }

View File

@@ -3,11 +3,11 @@
//===========Firmware============================================================================================================================================= //===========Firmware=============================================================================================================================================
#ifdef ESP8266 #ifdef ESP8266
#define FIRMWARE_NAME "esp8266-iotm" #define FIRMWARE_NAME "esp8266-iotm"
#define FIRMWARE_VERSION 264 #define FIRMWARE_VERSION 265
#endif #endif
#ifdef ESP32 #ifdef ESP32
#define FIRMWARE_NAME "esp32-iotm" #define FIRMWARE_NAME "esp32-iotm"
#define FIRMWARE_VERSION 264 #define FIRMWARE_VERSION 265
#endif #endif
#define FLASH_4MB true #define FLASH_4MB true

View File

@@ -47,6 +47,7 @@ extern Bounce* buttons;
//Global vars //Global vars
extern boolean just_load; extern boolean just_load;
extern boolean telegramInitBeen;
// Json // Json
extern String configSetupJson; //все настройки extern String configSetupJson; //все настройки

View File

@@ -1,6 +1,6 @@
#pragma once #pragma once
extern void sendTelegramMsg();
extern void telegramInit(); extern void telegramInit();
extern void handleTelegram(); extern void handleTelegram();
extern bool isTelegramEnabled();

View File

@@ -93,6 +93,7 @@ void csvCmdExecute(String& cmdStr) {
else if (order == F("impuls-out")) { else if (order == F("impuls-out")) {
sCmd.addCommand(order.c_str(), impuls); sCmd.addCommand(order.c_str(), impuls);
} }
sCmd.readStr(buf); sCmd.readStr(buf);

View File

@@ -18,6 +18,7 @@ DallasTemperature sensors;
*/ */
boolean just_load = true; boolean just_load = true;
boolean telegramInitBeen = false;
// Json // Json
String configSetupJson = "{}"; String configSetupJson = "{}";

View File

@@ -1,35 +1,56 @@
#include "Telegram.h" #include "Telegram.h"
#include "Global.h" #include "Global.h"
CTBot* myBot{ nullptr }; CTBot* myBot{ nullptr };
void telegramInit() { void telegramInit() {
String token = jsonReadStr(configSetupJson, "telegramApi"); if (isTelegramEnabled()) {
if (!myBot) { telegramInitBeen = true;
myBot = new CTBot(); sCmd.addCommand("telegram", sendTelegramMsg);
} String token = jsonReadStr(configSetupJson, "telegramApi");
myBot->setTelegramToken(token); if (!myBot) {
if (myBot->testConnection()) { myBot = new CTBot();
SerialPrint("I", "Telegram", "Connected"); }
} myBot->setTelegramToken(token);
else { myBot->enableUTF8Encoding(true);
SerialPrint("E", "Telegram", "Not connected"); if (myBot->testConnection()) {
SerialPrint("I", "Telegram", "Connected");
}
else {
SerialPrint("E", "Telegram", "Not connected");
}
} }
} }
void handleTelegram() { void handleTelegram() {
if (telegramInitBeen) {
TBMessage msg; if (isTelegramEnabled()) {
TBMessage msg;
static unsigned long prevMillis; static unsigned long prevMillis;
unsigned long currentMillis = millis(); unsigned long currentMillis = millis();
unsigned long difference = currentMillis - prevMillis; unsigned long difference = currentMillis - prevMillis;
if (difference >= 1000) { if (difference >= 1000) {
prevMillis = millis(); prevMillis = millis();
if (myBot->getNewMessage(msg)) { if (myBot->getNewMessage(msg)) {
Serial.println(msg.text); SerialPrint("->", "Telegram", "chat ID: " + String(msg.sender.id) + ", msg: " + String(msg.text));
myBot->sendMessage(msg.sender.id, 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");
} }

View File

@@ -1,10 +1,10 @@
#include "Web.h" #include "Web.h"
#include "Class/NotAsync.h" #include "Class/NotAsync.h"
#include "Global.h" #include "Global.h"
#include "Init.h" #include "Init.h"
#include "ItemsList.h" #include "ItemsList.h"
#include "items/LoggingClass.h" #include "items/LoggingClass.h"
#include "Telegram.h"
bool parseRequestForPreset(AsyncWebServerRequest* request, uint8_t& preset) { bool parseRequestForPreset(AsyncWebServerRequest* request, uint8_t& preset) {
if (request->hasArg("preset")) { if (request->hasArg("preset")) {
@@ -196,7 +196,14 @@ void web_init() {
//==============================push settings============================================= //==============================push settings=============================================
if (request->hasArg("telegramApi")) { if (request->hasArg("telegramApi")) {
jsonWriteStr(configSetupJson, "telegramApi", request->getParam("telegramApi")->value()); 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(); saveConfig();
request->send(200); request->send(200);
} }