Files
IoTManager/src/Telegram.cpp

56 lines
1.9 KiB
C++
Raw Normal View History

2020-11-04 23:48:21 +03:00
#include "Telegram.h"
#include "Global.h"
CTBot* myBot{ nullptr };
void telegramInit() {
2020-11-05 02:23:08 +03:00
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");
}
2020-11-04 23:48:21 +03:00
}
}
void handleTelegram() {
2020-11-05 02:23:08 +03:00
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));
}
}
}
}
}
2020-11-04 23:48:21 +03:00
2020-11-05 02:23:08 +03:00
void sendTelegramMsg() {
String msg = sCmd.next();
msg.replace("#"," ");
myBot->sendMessage(jsonReadInt(configSetupJson, "chatId"), msg);
SerialPrint("<-", "Telegram", "chat ID: " + String(jsonReadInt(configSetupJson, "chatId")) + ", msg: " + msg);
}
2020-11-04 23:48:21 +03:00
2020-11-05 02:23:08 +03:00
bool isTelegramEnabled() {
return jsonReadBool(configSetupJson, "telegonof");
2020-11-04 23:48:21 +03:00
}