2020-11-04 23:48:21 +03:00
# include "Telegram.h"
2020-11-05 17:27:18 +03:00
2020-11-04 23:48:21 +03:00
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 ;
2020-11-05 17:27:18 +03:00
if ( difference > = 5000 ) {
2020-11-05 02:23:08 +03:00
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 ( ) ;
2020-11-05 17:27:18 +03:00
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-05 17:27:18 +03:00
myBot - > sendMessage ( jsonReadInt ( configSetupJson , " chatId " ) , 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 ;
myBot - > sendMessage ( jsonReadInt ( configSetupJson , " chatId " ) , msg ) ;
SerialPrint ( " <- " , " Telegram " , " chat ID: " + String ( jsonReadInt ( configSetupJson , " chatId " ) ) + " , msg: " + msg ) ;
}
} else if ( type = = " 2 " ) {
myBot - > sendMessage ( jsonReadInt ( configSetupJson , " chatId " ) , msg ) ;
SerialPrint ( " <- " , " Telegram " , " chat ID: " + String ( jsonReadInt ( configSetupJson , " chatId " ) ) + " , msg: " + msg ) ;
}
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
}
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 ) ;
String value = jsonReadStr ( configLiveJson , id ) ;
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-11-04 23:48:21 +03:00
}