mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-26 22:22:16 +03:00
265 Telegram support added
This commit is contained in:
@@ -14,6 +14,7 @@
|
||||
"mqttPass": "23ri22se32",
|
||||
"scen": "1",
|
||||
"telegramApi": "1425283609:AAFf3YO8ouCJF23kU8CMiU7XXy21MFhbn9w",
|
||||
"telegonof": "0",
|
||||
"weblogin": "admin",
|
||||
"webpass": "admin",
|
||||
"udponoff": "1",
|
||||
|
||||
@@ -43,7 +43,7 @@
|
||||
},
|
||||
{
|
||||
"type": "h4",
|
||||
"title": "LittleFS version: 264"
|
||||
"title": "LittleFS version: 265"
|
||||
},
|
||||
{
|
||||
"type": "hr"
|
||||
|
||||
@@ -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": "<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>"
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -47,6 +47,7 @@ extern Bounce* buttons;
|
||||
|
||||
//Global vars
|
||||
extern boolean just_load;
|
||||
extern boolean telegramInitBeen;
|
||||
|
||||
// Json
|
||||
extern String configSetupJson; //все настройки
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
|
||||
|
||||
extern void sendTelegramMsg();
|
||||
extern void telegramInit();
|
||||
extern void handleTelegram();
|
||||
extern void handleTelegram();
|
||||
extern bool isTelegramEnabled();
|
||||
@@ -93,6 +93,7 @@ void csvCmdExecute(String& cmdStr) {
|
||||
else if (order == F("impuls-out")) {
|
||||
sCmd.addCommand(order.c_str(), impuls);
|
||||
}
|
||||
|
||||
|
||||
|
||||
sCmd.readStr(buf);
|
||||
|
||||
@@ -18,6 +18,7 @@ DallasTemperature sensors;
|
||||
*/
|
||||
|
||||
boolean just_load = true;
|
||||
boolean telegramInitBeen = false;
|
||||
|
||||
// Json
|
||||
String configSetupJson = "{}";
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
11
src/Web.cpp
11
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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user