Files
IoTManager/src/Telegram.cpp

123 lines
4.4 KiB
C++
Raw Normal View History

2020-12-16 19:28:44 +01:00
#include "Consts.h"
#ifdef telegramEnable
2020-11-04 23:48:21 +03:00
#include "Telegram.h"
CTBot* myBot{ nullptr };
void telegramInit() {
2020-11-05 02:23:08 +03:00
if (isTelegramEnabled()) {
telegramInitBeen = true;
2020-12-16 19:28:44 +01:00
sCmd.addCommand("telegramEnable", sendTelegramMsg);
2020-11-05 02:23:08 +03:00
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");
}
2020-12-10 19:12:15 +03:00
SerialPrint("I", F("Telegram"), F("Telegram Init"));
2020-11-04 23:48:21 +03:00
}
}
void handleTelegram() {
2020-11-05 02:23:08 +03:00
if (telegramInitBeen) {
if (isTelegramEnabled()) {
2020-12-02 04:50:29 +03:00
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));
}
2020-11-05 02:23:08 +03:00
}
}
2020-11-05 17:27:18 +03:00
}
}
}
void telegramMsgParse(String msg) {
2020-11-06 20:17:49 +03:00
if (msg.indexOf("set") != -1) {
msg = deleteBeforeDelimiter(msg, "_");
msg.replace("_", " ");
2020-11-05 17:27:18 +03:00
orderBuf += String(msg) + ",";
myBot->sendMessage(jsonReadInt(configSetupJson, "chatId"), "order done");
2020-11-06 20:17:49 +03:00
SerialPrint("<-", "Telegram", "chat ID: " + String(jsonReadInt(configSetupJson, "chatId")) + ", msg: " + String(msg));
2020-11-05 17:27:18 +03:00
}
else if (msg.indexOf("get") != -1) {
2020-11-06 20:17:49 +03:00
msg = deleteBeforeDelimiter(msg, "_");
2020-11-19 04:14:52 +03:00
myBot->sendMessage(jsonReadInt(configSetupJson, "chatId"), getValue(msg)); //jsonReadStr(configLiveJson , msg));
2020-11-06 20:17:49 +03:00
SerialPrint("<-", "Telegram", "chat ID: " + String(jsonReadInt(configSetupJson, "chatId")) + ", msg: " + String(msg));
}
else if (msg.indexOf("all") != -1) {
String list = returnListOfParams();
myBot->sendMessage(jsonReadInt(configSetupJson, "chatId"), list);
SerialPrint("<-", "Telegram", "chat ID: " + String(jsonReadInt(configSetupJson, "chatId")) + "\n" + list);
2020-11-05 17:27:18 +03:00
}
else {
2020-11-17 01:01:42 +03:00
myBot->sendMessage(jsonReadInt(configSetupJson, "chatId"), F("Wrong order, use /all to get all values, /get_id to get value, or /set_id_value to set value"));
2020-11-05 02:23:08 +03:00
}
}
2020-11-04 23:48:21 +03:00
2020-11-05 02:23:08 +03:00
void sendTelegramMsg() {
String msg = sCmd.next();
2020-11-06 20:17:49 +03:00
String type = sCmd.next();
2020-11-05 17:27:18 +03:00
msg.replace("#", " ");
2020-11-06 20:17:49 +03:00
if (type == "1") {
static String prevMsg;
if (prevMsg != msg) {
prevMsg = msg;
2020-12-15 22:10:40 +01:00
if (msg != "na") {
myBot->sendMessage(jsonReadInt(configSetupJson, "chatId"), msg);
}
2020-11-06 20:17:49 +03:00
SerialPrint("<-", "Telegram", "chat ID: " + String(jsonReadInt(configSetupJson, "chatId")) + ", msg: " + msg);
}
2020-12-02 04:50:29 +03:00
}
else if (type == "2") {
2020-11-06 20:17:49 +03:00
myBot->sendMessage(jsonReadInt(configSetupJson, "chatId"), msg);
SerialPrint("<-", "Telegram", "chat ID: " + String(jsonReadInt(configSetupJson, "chatId")) + ", msg: " + msg);
2020-12-02 04:50:29 +03:00
}
2020-11-05 02:23:08 +03:00
}
2020-11-04 23:48:21 +03:00
2020-11-05 02:23:08 +03:00
bool isTelegramEnabled() {
return jsonReadBool(configSetupJson, "telegonof");
2020-11-06 20:17:49 +03:00
}
2020-12-02 04:50:29 +03:00
bool isTelegramInputOn() {
return jsonReadBool(configSetupJson, "teleginput");
}
2020-11-06 20:17:49 +03:00
String returnListOfParams() {
String cmdStr = readFile(DEVICE_CONFIG_FILE, 4096);
cmdStr += "\r\n";
cmdStr.replace("\r\n", "\n");
cmdStr.replace("\r", "\n");
int count = 0;
String out;
while (cmdStr.length()) {
String buf = selectToMarker(cmdStr, "\n");
count++;
if (count > 1) {
String id = selectFromMarkerToMarker(buf, ";", 2);
2020-11-19 04:14:52 +03:00
String value = getValue(id); //jsonReadStr(configLiveJson , id);
2020-11-06 20:17:49 +03:00
String page = selectFromMarkerToMarker(buf, ";", 4);
page.replace("#", " ");
String name = selectFromMarkerToMarker(buf, ";", 5);
name.replace("#", " ");
out += page + " " + " " + name + " " + value + "\n";
}
cmdStr = deleteBeforeDelimiter(cmdStr, "\n");
}
return out;
2020-12-16 13:59:01 +01:00
}
#endif