mirror of
https://github.com/IoTManagerProject/IoTManager.git
synced 2026-03-27 06:32:19 +03:00
265 Telegram support added
This commit is contained in:
@@ -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");
|
||||
}
|
||||
Reference in New Issue
Block a user