mirror of https://github.com/arendst/Tasmota.git
Refactor telegram
This commit is contained in:
parent
82fb25a610
commit
2c6ee9cd95
|
@ -43,6 +43,8 @@
|
||||||
#define TELEGRAM_SEND_RETRY 4 // Retries
|
#define TELEGRAM_SEND_RETRY 4 // Retries
|
||||||
#define TELEGRAM_LOOP_WAIT 10 // Seconds
|
#define TELEGRAM_LOOP_WAIT 10 // Seconds
|
||||||
|
|
||||||
|
#define TELEGRAM_MAX_MESSAGES 2
|
||||||
|
|
||||||
#ifdef USE_MQTT_TLS_CA_CERT
|
#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_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;
|
static const uint32_t tls_tx_size = 1024;
|
||||||
|
@ -57,7 +59,9 @@ BearSSL::WiFiClientSecure_light *telegramClient = nullptr;
|
||||||
static const uint8_t Telegram_Fingerprint[] PROGMEM = USE_TELEGRAM_FINGERPRINT;
|
static const uint8_t Telegram_Fingerprint[] PROGMEM = USE_TELEGRAM_FINGERPRINT;
|
||||||
|
|
||||||
struct {
|
struct {
|
||||||
String message[3][6]; // Amount of messages read per time (update_id, name_id, name, lastname, chat_id, text)
|
String message[TELEGRAM_MAX_MESSAGES][6]; // Amount of messages read per time (update_id, name_id, name, lastname, chat_id, text)
|
||||||
|
uint32_t message_id = 0;
|
||||||
|
uint8_t message_count = 0;
|
||||||
uint8_t state = 0;
|
uint8_t state = 0;
|
||||||
uint8_t index = 0;
|
uint8_t index = 0;
|
||||||
uint8_t retry = 0;
|
uint8_t retry = 0;
|
||||||
|
@ -81,9 +85,9 @@ bool TelegramInit(void) {
|
||||||
telegramClient->setPubKeyFingerprint(Telegram_Fingerprint, Telegram_Fingerprint, false); // check server fingerprint
|
telegramClient->setPubKeyFingerprint(Telegram_Fingerprint, Telegram_Fingerprint, false); // check server fingerprint
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Telegram.message[0][0]="0"; // Number of received messages
|
Telegram.message_count = 0; // Number of received messages
|
||||||
Telegram.message[1][0]="";
|
Telegram.message_id = 0; // Code of last read Message
|
||||||
Telegram.message[0][1]="0"; // Code of last read Message
|
Telegram.message[0][0] = "";
|
||||||
|
|
||||||
AddLog_P2(LOG_LEVEL_INFO, PSTR("TGM: Started"));
|
AddLog_P2(LOG_LEVEL_INFO, PSTR("TGM: Started"));
|
||||||
}
|
}
|
||||||
|
@ -131,13 +135,13 @@ String TelegramConnectToTelegram(String command) {
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TelegramGetUpdates(String offset) {
|
void TelegramGetUpdates(uint32_t offset) {
|
||||||
AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("TGM: getUpdates"));
|
AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("TGM: getUpdates"));
|
||||||
|
|
||||||
if (!TelegramInit()) { return; }
|
if (!TelegramInit()) { return; }
|
||||||
|
|
||||||
String _token = SettingsText(SET_TELEGRAM_TOKEN);
|
String _token = SettingsText(SET_TELEGRAM_TOKEN);
|
||||||
String command = "bot" + _token + "/getUpdates?offset=" + offset;
|
String command = "bot" + _token + "/getUpdates?offset=" + String(offset);
|
||||||
String response = TelegramConnectToTelegram(command); //recieve reply from telegram.org
|
String response = TelegramConnectToTelegram(command); //recieve reply from telegram.org
|
||||||
|
|
||||||
// {"ok":true,"result":[]}
|
// {"ok":true,"result":[]}
|
||||||
|
@ -172,98 +176,69 @@ void TelegramGetUpdates(String offset) {
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
// ]}
|
// ]}
|
||||||
|
// 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"
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// ]}
|
||||||
|
|
||||||
AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("TGM: Response %s"), response.c_str());
|
AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("TGM: Response %s"), response.c_str());
|
||||||
|
|
||||||
// parsing of reply from Telegram into separate received messages
|
JsonParser parser((char*)response.c_str());
|
||||||
int i = 0; //messages received counter
|
JsonParserObject root = parser.getRootObject();
|
||||||
if (response != "") {
|
if (root) {
|
||||||
|
|
||||||
// AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TGM: Sent Update request messages up to %s"), offset.c_str());
|
// AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TGM: Sent Update request messages up to %s"), offset.c_str());
|
||||||
|
|
||||||
String a = "";
|
JsonParserArray arr = root[PSTR("result")];
|
||||||
int ch_count = 0;
|
uint32_t max_updates = arr.size();
|
||||||
String c;
|
// if (max_updates > TELEGRAM_MAX_MESSAGES) { max_updates = TELEGRAM_MAX_MESSAGES; }
|
||||||
for (uint32_t n = 1; n < response.length() +1; n++) { //Search for each message start
|
if (max_updates > 1) { max_updates = 1; } // Cannot handle more than one for now
|
||||||
ch_count++;
|
Telegram.message_count = 0; // Returns how many messages are in the array
|
||||||
c = response.substring(n -1, n);
|
if (max_updates) {
|
||||||
a = a + c;
|
for (uint32_t i = 0; i < max_updates; i++) {
|
||||||
if (ch_count > 8) {
|
Telegram.message[i][5] = ""; // Reset command
|
||||||
if (a.substring(ch_count -9) == "update_id") {
|
JsonParserObject result = arr[i].getObject();
|
||||||
if (i > 1) { break; }
|
if (result) {
|
||||||
Telegram.message[i][0] = a.substring(0, ch_count -11);
|
// {"update_id":14354450,
|
||||||
a = a.substring(ch_count-11);
|
// "message":{"message_id":141,
|
||||||
i++;
|
// "from":{"id":139920293,"is_bot":false,"first_name":"Theo","last_name":"Arends","username":"tjatja","language_code":"nl"},
|
||||||
ch_count = 11;
|
// "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
|
||||||
|
Telegram.message[i][0] = result["update_id"].getStr();
|
||||||
|
// Telegram.message[i][1] = result["message"].getObject()["from"].getObject()["id"].getStr();
|
||||||
|
// Telegram.message[i][2] = result["message"].getObject()["from"].getObject()["first_name"].getStr();
|
||||||
|
// Telegram.message[i][3] = result["message"].getObject()["from"].getObject()["last_name"].getStr();
|
||||||
|
Telegram.message[i][4] = result["message"].getObject()["chat"].getObject()["id"].getStr();
|
||||||
|
Telegram.message[i][5] = result["message"].getObject()["text"].getStr();
|
||||||
}
|
}
|
||||||
}
|
Telegram.message_id = Telegram.message[i][0].toInt() +1; // Write id of last read message
|
||||||
}
|
|
||||||
if (1 == i) {
|
|
||||||
Telegram.message[i][0] = a.substring(0, ch_count -2); //Assign of parsed message into message matrix if only 1 message)
|
|
||||||
}
|
|
||||||
if (i > 1) { i = i -1; }
|
|
||||||
/*
|
|
||||||
String buf = response; // we need to keep a copy of the buffer
|
|
||||||
JsonParser parser((char*)buf.c_str());
|
|
||||||
JsonParserObject root = parser.getRootObject();
|
|
||||||
if (root) {
|
|
||||||
JsonParserArray arr = root["result"];
|
|
||||||
if (arr) {
|
|
||||||
i++;
|
|
||||||
Telegram.message[i][0] = arr[0].getStr();
|
|
||||||
// Telegram.message[0][0] = arr[0].getObject()["update_id"].getStr();
|
|
||||||
|
|
||||||
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TGM: Msg %s"), Telegram.message[i][0].c_str());
|
// for (uint32_t j = 0; j < 6; j++) {
|
||||||
|
// AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("TGM: Parsed%d \"%s\""), j, Telegram.message[i][j].c_str());
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TGM: No new messages"));
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
}
|
|
||||||
//check result of parsing process
|
|
||||||
if (response == "") {
|
|
||||||
// AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TGM: Failed to update"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (0 == i) {
|
|
||||||
// AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TGM: No new messages"));
|
|
||||||
Telegram.message[0][0] = "0";
|
|
||||||
} else {
|
} else {
|
||||||
Telegram.message[0][0] = String(i); //returns how many messages are in the array
|
AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TGM: Failed to update"));
|
||||||
for (int b = 1; b < i+1; b++) {
|
|
||||||
// AddLog_P2(LOG_LEVEL_DEBUG, PSTR("TGM: Msg %d %s"), b, Telegram.message[b][0].c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
TelegramAnalizeMessage();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void TelegramAnalizeMessage(void) {
|
|
||||||
for (uint32_t i = 1; i < Telegram.message[0][0].toInt() +1; i++) {
|
|
||||||
Telegram.message[i][5] = "";
|
|
||||||
|
|
||||||
String buf = Telegram.message[i][0]; // we need to keep a copy of the buffer
|
|
||||||
JsonParser parser((char*)buf.c_str());
|
|
||||||
JsonParserObject root = parser.getRootObject();
|
|
||||||
if (root) {
|
|
||||||
// {"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[i][0] = root["update_id"].getStr();
|
|
||||||
Telegram.message[i][1] = root["message"].getObject()["from"].getObject()["id"].getStr();
|
|
||||||
Telegram.message[i][2] = root["message"].getObject()["from"].getObject()["first_name"].getStr();
|
|
||||||
Telegram.message[i][3] = root["message"].getObject()["from"].getObject()["last_name"].getStr();
|
|
||||||
Telegram.message[i][4] = root["message"].getObject()["chat"].getObject()["id"].getStr();
|
|
||||||
Telegram.message[i][5] = root["message"].getObject()["text"].getStr();
|
|
||||||
}
|
|
||||||
|
|
||||||
int id = Telegram.message[Telegram.message[0][0].toInt()][0].toInt() +1;
|
|
||||||
Telegram.message[0][1] = id; // Write id of last read message
|
|
||||||
|
|
||||||
for (int j = 0; j < 6; j++) {
|
|
||||||
// AddLog_P2(LOG_LEVEL_DEBUG_MORE, PSTR("TGM: Parsed%d \"%s\""), j, Telegram.message[i][j].c_str());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -352,14 +327,14 @@ void TelegramLoop(void) {
|
||||||
Telegram.state++;
|
Telegram.state++;
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
TelegramGetUpdates(Telegram.message[0][1]); // launch API GetUpdates up to xxx message
|
TelegramGetUpdates(Telegram.message_id); // Launch API GetUpdates up to xxx message
|
||||||
Telegram.index = 1;
|
Telegram.index = 0;
|
||||||
Telegram.retry = TELEGRAM_SEND_RETRY;
|
Telegram.retry = TELEGRAM_SEND_RETRY;
|
||||||
Telegram.state++;
|
Telegram.state++;
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
if (Telegram.echo_enable) {
|
if (Telegram.echo_enable) {
|
||||||
if (Telegram.retry && (Telegram.index < Telegram.message[0][0].toInt() + 1)) {
|
if (Telegram.retry && (Telegram.index < Telegram.message_count)) {
|
||||||
if (TelegramSendMessage(Telegram.message[Telegram.index][4], Telegram.message[Telegram.index][5])) {
|
if (TelegramSendMessage(Telegram.message[Telegram.index][4], Telegram.message[Telegram.index][5])) {
|
||||||
Telegram.index++;
|
Telegram.index++;
|
||||||
Telegram.retry = TELEGRAM_SEND_RETRY;
|
Telegram.retry = TELEGRAM_SEND_RETRY;
|
||||||
|
@ -367,7 +342,7 @@ void TelegramLoop(void) {
|
||||||
Telegram.retry--;
|
Telegram.retry--;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Telegram.message[0][0] = ""; // All messages have been replied - reset new messages
|
Telegram.message_count = 0; // All messages have been replied - reset new messages
|
||||||
Telegram.wait = Telegram.poll;
|
Telegram.wait = Telegram.poll;
|
||||||
Telegram.state++;
|
Telegram.state++;
|
||||||
}
|
}
|
||||||
|
@ -375,14 +350,14 @@ void TelegramLoop(void) {
|
||||||
if (Telegram.skip) { // Skip first update after restart as it may be a restart (again)
|
if (Telegram.skip) { // Skip first update after restart as it may be a restart (again)
|
||||||
Telegram.skip = false;
|
Telegram.skip = false;
|
||||||
} else {
|
} else {
|
||||||
if (Telegram.message[0][0].toInt() && (Telegram.message[Telegram.index][5].length() > 0)) {
|
if (Telegram.message_count && (Telegram.message[Telegram.index][5].length() > 0)) {
|
||||||
String logging = TelegramExecuteCommand(Telegram.message[Telegram.index][5].c_str());
|
String logging = TelegramExecuteCommand(Telegram.message[Telegram.index][5].c_str());
|
||||||
if (logging.length() > 0) {
|
if (logging.length() > 0) {
|
||||||
TelegramSendMessage(Telegram.message[Telegram.index][4], logging);
|
TelegramSendMessage(Telegram.message[Telegram.index][4], logging);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Telegram.message[0][0] = ""; // All messages have been replied - reset new messages
|
Telegram.message_count = 0; // All messages have been replied - reset new messages
|
||||||
Telegram.wait = Telegram.poll;
|
Telegram.wait = Telegram.poll;
|
||||||
Telegram.state++;
|
Telegram.state++;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue