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

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

View File

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

View File

@@ -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");
}

View File

@@ -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);
}