2020-06-11 16:30:33 +01:00
/*
xdrv_40_telegram . ino - telegram for Tasmota
2021-01-01 12:44:04 +00:00
Copyright ( C ) 2021 Theo Arends
2020-06-11 16:30:33 +01:00
This program is free software : you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
# ifdef USE_TELEGRAM
/*********************************************************************************************\
* Telegram bot
*
* Supported commands :
2020-06-11 16:52:10 +01:00
* TmToken < token > - Add your BotFather created bot token ( default none )
* TmChatId < chat_id > - Add your BotFather created bot chat id ( default none )
* TmPoll < seconds > - Telegram receive poll time ( default 10 seconds )
* TmState 0 - Disable telegram sending ( default )
2020-10-12 12:44:32 +01:00
* TmState 1 - Enable telegram sending ( needs valid TmChatId )
2020-06-11 16:52:10 +01:00
* TmState 2 - Disable telegram listener ( default )
* TmState 3 - Enable telegram listener
* TmState 4 - Disable telegram response echo ( default )
* TmState 5 - Enable telegram response echo
* TmSend < data > - If telegram sending is enabled AND a chat id is present then send data
2020-06-11 16:30:33 +01:00
*
* Tested with defines
* # define USE_TELEGRAM // Support for Telegram protocol
* # define USE_TELEGRAM_FINGERPRINT " \xB2 \x72 \x47 \xA6 \x69 \x8C \x3C \x69 \xF9 \x58 \x6C \xF3 \x60 \x02 \xFB \x83 \xFA \x8B \x1F \x23 " // Telegram api.telegram.org TLS public key fingerpring
\ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# define XDRV_40 40
2020-10-12 12:44:32 +01:00
# ifndef TELEGRAM_LOOP_WAIT
2020-06-11 16:30:33 +01:00
# define TELEGRAM_LOOP_WAIT 10 // Seconds
2020-10-12 12:44:32 +01:00
# endif
2020-06-11 16:30:33 +01:00
2020-10-12 12:44:32 +01:00
# define TELEGRAM_SEND_RETRY 4 // Retries
2020-10-11 17:13:13 +01:00
# define TELEGRAM_MAX_MESSAGES 2
2020-06-11 16:30:33 +01:00
# ifdef USE_MQTT_TLS_CA_CERT
static const uint32_t tls_rx_size = 2048 ; // since Telegram CA is bigger than 1024 bytes, we need to increase rx buffer
static const uint32_t tls_tx_size = 1024 ;
# else
static const uint32_t tls_rx_size = 1024 ;
static const uint32_t tls_tx_size = 1024 ;
# endif
# include "WiFiClientSecureLightBearSSL.h"
BearSSL : : WiFiClientSecure_light * telegramClient = nullptr ;
static const uint8_t Telegram_Fingerprint [ ] PROGMEM = USE_TELEGRAM_FINGERPRINT ;
2020-10-12 12:44:32 +01:00
typedef struct {
String text ;
2021-04-10 14:04:50 +01:00
String chat_id ;
2020-10-12 12:44:32 +01:00
// String from_first_name;
// String from_last_name;
// uint32_t from_id = 0;
uint32_t update_id = 0 ;
} TelegramMessage ;
2020-06-11 16:30:33 +01:00
struct {
2020-10-12 12:44:32 +01:00
TelegramMessage message [ TELEGRAM_MAX_MESSAGES ] ;
uint32_t next_update_id = 0 ;
uint8_t message_count = 0 ; // Amount of messages read per time
2020-06-11 16:30:33 +01:00
uint8_t state = 0 ;
uint8_t index = 0 ;
uint8_t retry = 0 ;
uint8_t poll = TELEGRAM_LOOP_WAIT ;
uint8_t wait = 0 ;
bool recv_busy = false ;
2020-10-12 12:44:32 +01:00
bool skip = true ; // Skip first telegram if restarted
2020-06-11 16:30:33 +01:00
} Telegram ;
bool TelegramInit ( void ) {
bool init_done = false ;
if ( strlen ( SettingsText ( SET_TELEGRAM_TOKEN ) ) ) {
if ( ! telegramClient ) {
telegramClient = new BearSSL : : WiFiClientSecure_light ( tls_rx_size , tls_tx_size ) ;
# ifdef USE_MQTT_TLS_CA_CERT
2020-09-23 07:45:14 +01:00
telegramClient - > setTrustAnchor ( & GoDaddyCAG2_TA , 1 ) ;
2020-06-11 16:30:33 +01:00
# else
telegramClient - > setPubKeyFingerprint ( Telegram_Fingerprint , Telegram_Fingerprint , false ) ; // check server fingerprint
# endif
2020-10-12 12:44:32 +01:00
Telegram . message_count = 0 ; // Number of received messages
Telegram . next_update_id = 0 ; // Code of last read Message
Telegram . message [ 0 ] . text = " " ;
2020-06-11 16:30:33 +01:00
2021-01-23 15:26:23 +00:00
AddLog ( LOG_LEVEL_INFO , PSTR ( " TGM: Started " ) ) ;
2020-06-11 16:30:33 +01:00
}
init_done = true ;
}
return init_done ;
}
2021-06-24 17:29:12 +01:00
String TelegramConnectToTelegram ( const String & command ) {
// AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("TGM: Cmnd '%s'"), command.c_str());
2020-06-11 16:30:33 +01:00
if ( ! TelegramInit ( ) ) { return " " ; }
2021-06-24 17:29:12 +01:00
String host = F ( " api.telegram.org " ) ;
2020-06-11 16:30:33 +01:00
String response = " " ;
uint32_t tls_connect_time = millis ( ) ;
2021-06-24 17:29:12 +01:00
if ( telegramClient - > connect ( host . c_str ( ) , 443 ) ) {
2020-10-12 12:44:32 +01:00
2021-01-23 15:26:23 +00:00
// AddLog(LOG_LEVEL_DEBUG, PSTR("TGM: Connected in %d ms, max ThunkStack used %d"), millis() - tls_connect_time, telegramClient->getMaxThunkStackUse());
2020-06-11 16:30:33 +01:00
2021-06-24 17:29:12 +01:00
// telegramClient->println("GET /"+command); // Fails after 20210621
String request = " GET / " + command + " HTTP/1.1 \r \n Host: " + host + " \r \n Connection: close \r \n \r \n " ;
2021-06-24 09:14:07 +01:00
telegramClient - > print ( request ) ;
2021-06-24 17:29:12 +01:00
/*
Response before 20210621 :
{ " ok " : true , " result " : [ ] }
Response after 20210621 :
HTTP / 1.1 200 OK
Server : nginx / 1.18 .0
Date : Thu , 24 Jun 2021 15 : 26 : 20 GMT
Content - Type : application / json
Content - Length : 23
Connection : close
Strict - Transport - Security : max - age = 31536000 ; includeSubDomains ; preload
Access - Control - Allow - Origin : *
Access - Control - Allow - Methods : GET , POST , OPTIONS
Access - Control - Expose - Headers : Content - Length , Content - Type , Date , Server , Connection
{ " ok " : true , " result " : [ ] }
*/
2020-06-11 16:30:33 +01:00
char c ;
2021-06-24 17:29:12 +01:00
bool available = false ;
2020-06-11 16:30:33 +01:00
uint32_t now = millis ( ) ;
2021-06-24 17:29:12 +01:00
while ( ! available & & ( millis ( ) - now < 1500 ) ) {
2020-06-11 16:30:33 +01:00
while ( telegramClient - > available ( ) ) {
2021-06-24 17:29:12 +01:00
c = telegramClient - > read ( ) ;
if ( c = = ' { ' ) {
available = true ; // Skip headers (+-400 bytes) and start response at first JSON
}
if ( available ) {
response + = c ;
if ( response . length ( ) > 800 ) { // Allow up to two messages
break ;
2021-06-24 09:14:07 +01:00
}
2020-06-11 16:30:33 +01:00
}
}
}
telegramClient - > stop ( ) ;
}
return response ;
}
2020-10-11 17:13:13 +01:00
void TelegramGetUpdates ( uint32_t offset ) {
2021-01-23 15:26:23 +00:00
AddLog ( LOG_LEVEL_DEBUG_MORE , PSTR ( " TGM: getUpdates " ) ) ;
2020-06-11 16:30:33 +01:00
if ( ! TelegramInit ( ) ) { return ; }
String _token = SettingsText ( SET_TELEGRAM_TOKEN ) ;
2020-10-11 17:13:13 +01:00
String command = " bot " + _token + " /getUpdates?offset= " + String ( offset ) ;
2020-06-11 16:30:33 +01:00
String response = TelegramConnectToTelegram ( command ) ; //recieve reply from telegram.org
// {"ok":true,"result":[]}
// or
// {"ok":true,"result":[
// {"update_id":973125394,
// "message":{"message_id":25,
// "from":{"id":139920293,"is_bot":false,"first_name":"Theo","last_name":"Arends","username":"tjatja","language_code":"nl"},
// "chat":{"id":139920293,"first_name":"Theo","last_name":"Arends","username":"tjatja","type":"private"},
// "date":1591877503,
// "text":"M1"
// }
// },
// {"update_id":973125395,
// "message":{"message_id":26,
// "from":{"id":139920293,"is_bot":false,"first_name":"Theo","last_name":"Arends","username":"tjatja","language_code":"nl"},
// "chat":{"id":139920293,"first_name":"Theo","last_name":"Arends","username":"tjatja","type":"private"},
// "date":1591877508,
// "text":"M2"
// }
// }
// ]}
// or
// {"ok":true,"result":[
// {"update_id":973125396,
// "message":{"message_id":29,
// "from":{"id":139920293,"is_bot":false,"first_name":"Theo","last_name":"Arends","username":"tjatja","language_code":"nl"},
// "chat":{"id":139920293,"first_name":"Theo","last_name":"Arends","username":"tjatja","type":"private"},
// "date":1591879753,
// "text":"/power toggle",
// "entities":[{"offset":0,"length":6,"type":"bot_command"}]
// }
// }
// ]}
2020-10-11 17:13:13 +01:00
// or
// {"ok":true,"result":[
// {"update_id":14354460,
// "message":{"message_id":164,
// "from":{"id":139920293,"is_bot":false,"first_name":"Theo","last_name":"Arends","username":"tjatja","language_code":"nl"},
// "chat":{"id":139920293,"first_name":"Theo","last_name":"Arends","username":"tjatja","type":"private"},
// "date":1602428727,
// "text":"Status 1"
// }
// },
// {"update_id":14354461,
// "message":{"message_id":165,
// "from":{"id":139920293,"is_bot":false,"first_name":"Theo","last_name":"Arends","username":"tjatja","language_code":"nl"},
// "chat":{"id":139920293,"first_name":"Theo","last_name":"Arends","username":"tjatja","type":"private"},
// "date":1602428731,
// "text":"Status 2"
// }
// }
// ]}
2020-06-11 16:30:33 +01:00
2021-06-24 17:29:12 +01:00
AddLog ( LOG_LEVEL_DEBUG_MORE , PSTR ( " TGM: Response '%s' " ) , response . c_str ( ) ) ;
2020-06-11 16:30:33 +01:00
2020-10-11 17:13:13 +01:00
JsonParser parser ( ( char * ) response . c_str ( ) ) ;
JsonParserObject root = parser . getRootObject ( ) ;
if ( root ) {
2020-06-11 16:30:33 +01:00
2021-06-05 10:47:09 +01:00
// AddLog(LOG_LEVEL_DEBUG, PSTR("TGM: Sent Update request messages up to %s"), offset.c_str());
2020-06-11 16:30:33 +01:00
2020-10-11 17:13:13 +01:00
JsonParserArray arr = root [ PSTR ( " result " ) ] ;
uint32_t max_updates = arr . size ( ) ;
// if (max_updates > TELEGRAM_MAX_MESSAGES) { max_updates = TELEGRAM_MAX_MESSAGES; }
if ( max_updates > 1 ) { max_updates = 1 ; } // Cannot handle more than one for now
Telegram . message_count = 0 ; // Returns how many messages are in the array
if ( max_updates ) {
for ( uint32_t i = 0 ; i < max_updates ; i + + ) {
2020-10-12 12:44:32 +01:00
Telegram . message [ i ] . text = " " ; // Reset command
2020-10-11 17:13:13 +01:00
JsonParserObject result = arr [ i ] . getObject ( ) ;
if ( result ) {
// {"update_id":14354450,
// "message":{"message_id":141,
// "from":{"id":139920293,"is_bot":false,"first_name":"Theo","last_name":"Arends","username":"tjatja","language_code":"nl"},
// "chat":{"id":139920293,"first_name":"Theo","last_name":"Arends","username":"tjatja","type":"private"},
// "date":1602346120,
// "text":"Status 1"}}
Telegram . message_count + + ; // Returns how many messages are in the array
2020-10-12 12:44:32 +01:00
Telegram . message [ i ] . update_id = result [ " update_id " ] . getUInt ( ) ;
// Telegram.message[i].from_id = result["message"].getObject()["from"].getObject()["id"].getUInt();
// Telegram.message[i].from_first_name = result["message"].getObject()["from"].getObject()["first_name"].getStr();
// Telegram.message[i].from_last_name = result["message"].getObject()["from"].getObject()["last_name"].getStr();
2021-04-10 14:04:50 +01:00
Telegram . message [ i ] . chat_id = result [ " message " ] . getObject ( ) [ " chat " ] . getObject ( ) [ " id " ] . getStr ( ) ;
2020-10-12 12:44:32 +01:00
Telegram . message [ i ] . text = result [ " message " ] . getObject ( ) [ " text " ] . getStr ( ) ;
2020-06-11 16:30:33 +01:00
}
2020-10-12 12:44:32 +01:00
Telegram . next_update_id = Telegram . message [ i ] . update_id + 1 ; // Write id of last read message
2020-10-11 17:13:13 +01:00
2021-06-05 10:47:09 +01:00
AddLog ( LOG_LEVEL_DEBUG_MORE , PSTR ( " TGM: Parsed update_id %d, chat_id %s, text \" %s \" " ) , Telegram . message [ i ] . update_id , Telegram . message [ i ] . chat_id . c_str ( ) , Telegram . message [ i ] . text . c_str ( ) ) ;
2020-06-11 16:30:33 +01:00
}
2020-10-11 17:13:13 +01:00
} else {
2021-01-23 15:26:23 +00:00
// AddLog(LOG_LEVEL_DEBUG, PSTR("TGM: No new messages"));
2020-06-11 16:30:33 +01:00
}
} else {
2021-01-23 15:26:23 +00:00
// AddLog(LOG_LEVEL_DEBUG, PSTR("TGM: Failed to update"));
2020-06-11 16:30:33 +01:00
}
}
2021-06-24 17:29:12 +01:00
bool TelegramSendMessage ( const String & chat_id , const String & text ) {
2021-01-23 15:26:23 +00:00
AddLog ( LOG_LEVEL_DEBUG_MORE , PSTR ( " TGM: sendMessage " ) ) ;
2020-06-11 16:30:33 +01:00
if ( ! TelegramInit ( ) ) { return false ; }
bool sent = false ;
if ( text ! = " " ) {
String _token = SettingsText ( SET_TELEGRAM_TOKEN ) ;
2021-04-10 14:04:50 +01:00
String command = " bot " + _token + " /sendMessage?chat_id= " + chat_id + " &text= " + UrlEncode ( text ) ;
2020-06-11 16:30:33 +01:00
String response = TelegramConnectToTelegram ( command ) ;
2021-06-05 10:47:09 +01:00
// AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("TGM: Response %s"), response.c_str());
2020-06-11 16:30:33 +01:00
if ( response . startsWith ( " { \" ok \" :true " ) ) {
2021-01-23 15:26:23 +00:00
// AddLog(LOG_LEVEL_DEBUG, PSTR("TGM: Message sent"));
2020-06-11 16:30:33 +01:00
sent = true ;
}
}
return sent ;
}
/*
void TelegramSendGetMe ( void ) {
2021-01-23 15:26:23 +00:00
AddLog ( LOG_LEVEL_DEBUG_MORE , PSTR ( " TGM: getMe " ) ) ;
2020-06-11 16:30:33 +01:00
if ( ! TelegramInit ( ) ) { return ; }
String _token = SettingsText ( SET_TELEGRAM_TOKEN ) ;
String command = " bot " + _token + " /getMe " ;
String response = TelegramConnectToTelegram ( command ) ;
// {"ok":true,"result":{"id":1179906608,"is_bot":true,"first_name":"Tasmota","username":"tasmota_bot","can_join_groups":true,"can_read_all_group_messages":false,"supports_inline_queries":false}}
2021-06-05 10:47:09 +01:00
// AddLog(LOG_LEVEL_DEBUG_MORE, PSTR("TGM: Response %s"), response.c_str());
2020-06-11 16:30:33 +01:00
}
*/
String TelegramExecuteCommand ( const char * svalue ) {
String response = " " ;
2020-12-18 15:30:37 +00:00
uint32_t curridx = TasmotaGlobal . log_buffer_pointer ;
2020-12-22 14:26:07 +00:00
TasmotaGlobal . templog_level = LOG_LEVEL_INFO ;
2020-06-11 16:30:33 +01:00
ExecuteCommand ( svalue , SRC_CHAT ) ;
2020-12-22 14:26:07 +00:00
response = F ( " { " ) ;
bool cflg = false ;
uint32_t index = curridx ;
char * line ;
size_t len ;
while ( GetLog ( TasmotaGlobal . templog_level , & index , & line , & len ) ) {
// [14:49:36.123 MQTT: stat/wemos5/RESULT = {"POWER":"OFF"}] > [{"POWER":"OFF"}]
char * JSON = ( char * ) memchr ( line , ' { ' , len ) ;
if ( JSON ) { // Is it a JSON message (and not only [15:26:08 MQT: stat/wemos5/POWER = O])
2021-05-30 21:32:37 +01:00
if ( cflg ) { response + = F ( " , " ) ; } // Add a comma
2021-05-30 17:47:39 +01:00
2021-05-30 21:32:37 +01:00
// size_t JSONlen = len - (JSON - line);
// response.concat(JSON +1, JSONlen -3); // Add terminating '\0' - Not supported on ESP32
len - = 2 ; // Skip last '}'
char save_log_char = line [ len ] ;
2021-05-30 17:47:39 +01:00
line [ len ] = ' \0 ' ; // Add terminating \'0'
2021-05-30 21:32:37 +01:00
response . concat ( JSON + 1 ) ; // Skip first '{'
line [ len ] = save_log_char ;
2021-05-30 17:47:39 +01:00
2020-12-22 14:26:07 +00:00
cflg = true ;
2020-12-19 16:29:14 +00:00
}
2020-06-11 16:30:33 +01:00
}
2020-12-22 14:26:07 +00:00
response + = F ( " } " ) ;
TasmotaGlobal . templog_level = 0 ;
2020-06-11 16:30:33 +01:00
return response ;
}
void TelegramLoop ( void ) {
2021-06-11 17:14:12 +01:00
if ( ! TasmotaGlobal . global_state . network_down & & ( Settings - > sbflag1 . telegram_recv_enable | | Settings - > sbflag1 . telegram_echo_enable ) ) {
2020-06-11 16:30:33 +01:00
switch ( Telegram . state ) {
case 0 :
TelegramInit ( ) ;
Telegram . state + + ;
break ;
case 1 :
2020-10-12 12:44:32 +01:00
TelegramGetUpdates ( Telegram . next_update_id ) ; // Launch API GetUpdates up to xxx message
2020-10-11 17:13:13 +01:00
Telegram . index = 0 ;
2020-06-11 16:30:33 +01:00
Telegram . retry = TELEGRAM_SEND_RETRY ;
Telegram . state + + ;
break ;
case 2 :
2021-06-11 17:14:12 +01:00
if ( Settings - > sbflag1 . telegram_echo_enable ) {
2020-10-11 17:13:13 +01:00
if ( Telegram . retry & & ( Telegram . index < Telegram . message_count ) ) {
2020-10-12 12:44:32 +01:00
if ( TelegramSendMessage ( Telegram . message [ Telegram . index ] . chat_id , Telegram . message [ Telegram . index ] . text ) ) {
2020-06-11 16:30:33 +01:00
Telegram . index + + ;
Telegram . retry = TELEGRAM_SEND_RETRY ;
} else {
Telegram . retry - - ;
}
} else {
2020-10-11 17:13:13 +01:00
Telegram . message_count = 0 ; // All messages have been replied - reset new messages
2020-06-11 16:30:33 +01:00
Telegram . wait = Telegram . poll ;
Telegram . state + + ;
}
} else {
2020-07-22 08:36:18 +01:00
if ( Telegram . skip ) { // Skip first update after restart as it may be a restart (again)
2020-07-22 08:34:35 +01:00
Telegram . skip = false ;
} else {
2020-10-12 12:44:32 +01:00
if ( Telegram . message_count & & ( Telegram . message [ Telegram . index ] . text . length ( ) > 0 ) ) {
String logging = TelegramExecuteCommand ( Telegram . message [ Telegram . index ] . text . c_str ( ) ) ;
2020-07-22 08:34:35 +01:00
if ( logging . length ( ) > 0 ) {
2020-10-12 12:44:32 +01:00
TelegramSendMessage ( Telegram . message [ Telegram . index ] . chat_id , logging ) ;
2020-07-22 08:34:35 +01:00
}
2020-06-11 16:30:33 +01:00
}
}
2020-10-11 17:13:13 +01:00
Telegram . message_count = 0 ; // All messages have been replied - reset new messages
2020-06-11 16:30:33 +01:00
Telegram . wait = Telegram . poll ;
Telegram . state + + ;
}
break ;
case 3 :
if ( Telegram . wait ) {
Telegram . wait - - ;
} else {
Telegram . state = 1 ;
}
}
}
}
/*********************************************************************************************\
* Commands
\ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2020-06-11 16:52:10 +01:00
# define D_CMND_TMSTATE "State"
# define D_CMND_TMPOLL "Poll"
# define D_CMND_TMSEND "Send"
# define D_CMND_TMTOKEN "Token"
# define D_CMND_TMCHATID "ChatId"
2020-06-11 16:30:33 +01:00
2020-06-11 16:52:10 +01:00
const char kTelegramCommands [ ] PROGMEM = " Tm| " // Prefix
D_CMND_TMSTATE " | " D_CMND_TMPOLL " | " D_CMND_TMTOKEN " | " D_CMND_TMCHATID " | " D_CMND_TMSEND ;
2020-06-11 16:30:33 +01:00
void ( * const TelegramCommand [ ] ) ( void ) PROGMEM = {
2020-06-11 16:52:10 +01:00
& CmndTmState , & CmndTmPoll , & CmndTmToken , & CmndTmChatId , & CmndTmSend } ;
2020-06-11 16:30:33 +01:00
2020-06-11 16:52:10 +01:00
void CmndTmState ( void ) {
2020-06-11 16:30:33 +01:00
if ( XdrvMailbox . data_len > 0 ) {
if ( ( XdrvMailbox . payload > = 0 ) & & ( XdrvMailbox . payload < = 6 ) ) {
switch ( XdrvMailbox . payload ) {
case 0 : // Off
case 1 : // On
2021-06-11 17:14:12 +01:00
Settings - > sbflag1 . telegram_send_enable = XdrvMailbox . payload & 1 ;
2020-06-11 16:30:33 +01:00
break ;
case 2 : // Off
case 3 : // On
2021-06-11 17:14:12 +01:00
Settings - > sbflag1 . telegram_recv_enable = XdrvMailbox . payload & 1 ;
2020-06-11 16:30:33 +01:00
break ;
case 4 : // Off
case 5 : // On
2021-06-11 17:14:12 +01:00
Settings - > sbflag1 . telegram_echo_enable = XdrvMailbox . payload & 1 ;
2020-06-11 16:30:33 +01:00
break ;
}
}
}
2021-05-23 15:50:17 +01:00
Response_P ( PSTR ( " { \" %s \" :{ \" Send \" : \" %s \" , \" Receive \" : \" %s \" , \" Echo \" : \" %s \" }} " ) ,
XdrvMailbox . command ,
2021-06-11 17:14:12 +01:00
GetStateText ( Settings - > sbflag1 . telegram_send_enable ) ,
GetStateText ( Settings - > sbflag1 . telegram_recv_enable ) ,
GetStateText ( Settings - > sbflag1 . telegram_echo_enable ) ) ;
2020-06-11 16:30:33 +01:00
}
2020-06-11 16:52:10 +01:00
void CmndTmPoll ( void ) {
2020-06-11 16:30:33 +01:00
if ( ( XdrvMailbox . payload > = 4 ) & & ( XdrvMailbox . payload < = 300 ) ) {
Telegram . poll = XdrvMailbox . payload ;
if ( Telegram . poll < Telegram . wait ) {
Telegram . wait = Telegram . poll ;
}
}
ResponseCmndNumber ( Telegram . poll ) ;
}
2020-06-11 16:52:10 +01:00
void CmndTmToken ( void ) {
2020-06-11 16:30:33 +01:00
if ( XdrvMailbox . data_len > 0 ) {
SettingsUpdateText ( SET_TELEGRAM_TOKEN , ( ' " ' = = XdrvMailbox . data [ 0 ] ) ? " " : XdrvMailbox . data ) ;
}
ResponseCmndChar ( SettingsText ( SET_TELEGRAM_TOKEN ) ) ;
}
2020-06-11 16:52:10 +01:00
void CmndTmChatId ( void ) {
2020-06-11 16:30:33 +01:00
if ( XdrvMailbox . data_len > 0 ) {
SettingsUpdateText ( SET_TELEGRAM_CHATID , ( ' " ' = = XdrvMailbox . data [ 0 ] ) ? " " : XdrvMailbox . data ) ;
}
ResponseCmndChar ( SettingsText ( SET_TELEGRAM_CHATID ) ) ;
}
2020-06-11 16:52:10 +01:00
void CmndTmSend ( void ) {
2021-06-11 17:14:12 +01:00
if ( ! Settings - > sbflag1 . telegram_send_enable | | ! strlen ( SettingsText ( SET_TELEGRAM_CHATID ) ) ) {
2021-02-16 15:21:46 +00:00
ResponseCmndFailed ( ) ;
2020-06-11 16:30:33 +01:00
return ;
}
if ( XdrvMailbox . data_len > 0 ) {
String message = XdrvMailbox . data ;
String chat_id = SettingsText ( SET_TELEGRAM_CHATID ) ;
2021-04-10 14:04:50 +01:00
if ( ! TelegramSendMessage ( chat_id , message ) ) {
2021-02-16 15:21:46 +00:00
ResponseCmndFailed ( ) ;
2020-06-11 16:30:33 +01:00
return ;
}
}
ResponseCmndDone ( ) ;
}
/*********************************************************************************************\
* Interface
\ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
bool Xdrv40 ( uint8_t function )
{
bool result = false ;
switch ( function ) {
case FUNC_EVERY_SECOND :
TelegramLoop ( ) ;
break ;
case FUNC_COMMAND :
result = DecodeCommand ( kTelegramCommands , TelegramCommand ) ;
break ;
}
return result ;
}
# endif // USE_TELEGRAM