2022-09-05 16:30:59 +03:00
|
|
|
#include "Global.h"
|
|
|
|
|
#include "classes/IoTItem.h"
|
|
|
|
|
|
2022-12-10 02:13:23 +01:00
|
|
|
class TelegramLT : public IoTItem {
|
|
|
|
|
public:
|
2022-09-05 17:06:37 +03:00
|
|
|
String _prevMsg = "";
|
|
|
|
|
String _token;
|
2022-10-18 10:03:43 +03:00
|
|
|
String _chatID;
|
2022-09-05 17:06:37 +03:00
|
|
|
|
2022-12-10 02:13:23 +01:00
|
|
|
TelegramLT(String parameters) : IoTItem(parameters) {
|
2022-09-05 16:30:59 +03:00
|
|
|
jsonRead(parameters, "token", _token);
|
|
|
|
|
jsonRead(parameters, "chatID", _chatID);
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-10 02:13:23 +01:00
|
|
|
void sendTelegramMsg(bool often, String msg) {
|
2023-09-03 19:33:40 +03:00
|
|
|
if (isNetworkActive() && (often || !often && _prevMsg != msg)) {
|
2022-09-05 16:30:59 +03:00
|
|
|
WiFiClient client;
|
|
|
|
|
HTTPClient http;
|
|
|
|
|
http.begin(client, "http://live-control.com/iotm/telegram.php");
|
|
|
|
|
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
|
2022-10-18 10:03:43 +03:00
|
|
|
String httpRequestData = "url=https://api.telegram.org/bot" + _token + "/sendmessage?chat_id=" + _chatID + "&text=" + msg;
|
2022-09-05 16:30:59 +03:00
|
|
|
int httpResponseCode = http.POST(httpRequestData);
|
|
|
|
|
String payload = http.getString();
|
2022-10-18 10:03:43 +03:00
|
|
|
SerialPrint("<-", F("Telegram"), "chat ID: " + _chatID + ", msg: " + msg);
|
|
|
|
|
SerialPrint("->", F("Telegram"), "chat ID: " + _chatID + ", server: " + httpResponseCode);
|
2022-09-05 16:30:59 +03:00
|
|
|
|
2022-12-10 02:13:23 +01:00
|
|
|
if (!strstr(payload.c_str(), "{\"ok\":true")) {
|
2022-10-20 10:09:16 +03:00
|
|
|
value.valD = 0;
|
2022-09-05 16:30:59 +03:00
|
|
|
Serial.printf("Telegram error, msg from server: %s\n", payload.c_str());
|
|
|
|
|
regEvent(value.valD, payload);
|
2022-12-10 02:13:23 +01:00
|
|
|
} else {
|
2022-10-20 10:09:16 +03:00
|
|
|
value.valD = 1;
|
|
|
|
|
regEvent(value.valD, payload);
|
2022-09-05 16:30:59 +03:00
|
|
|
}
|
|
|
|
|
http.end();
|
|
|
|
|
_prevMsg = msg;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-10 02:13:23 +01:00
|
|
|
IoTValue execute(String command, std::vector<IoTValue> ¶m) {
|
|
|
|
|
if (param.size() == 1) {
|
2022-09-05 16:30:59 +03:00
|
|
|
String strTmp;
|
|
|
|
|
if (param[0].isDecimal && param[0].valS == "")
|
|
|
|
|
strTmp = param[0].valD;
|
|
|
|
|
else
|
|
|
|
|
strTmp = param[0].valS;
|
|
|
|
|
|
2022-12-10 02:13:23 +01:00
|
|
|
if (command == "sendMsg") {
|
|
|
|
|
if (param.size()) {
|
2022-09-05 16:30:59 +03:00
|
|
|
sendTelegramMsg(false, strTmp);
|
|
|
|
|
}
|
2022-12-10 02:13:23 +01:00
|
|
|
} else if (command == "sendOftenMsg") {
|
|
|
|
|
if (param.size()) {
|
2022-09-05 16:30:59 +03:00
|
|
|
sendTelegramMsg(true, strTmp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~TelegramLT(){};
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-10 02:13:23 +01:00
|
|
|
void *getAPI_TelegramLT(String subtype, String param) {
|
|
|
|
|
if (subtype == F("TelegramLT")) {
|
2022-09-05 16:30:59 +03:00
|
|
|
return new TelegramLT(param);
|
2022-12-10 02:13:23 +01:00
|
|
|
} else {
|
2022-09-05 16:30:59 +03:00
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|