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

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