diff --git a/src/AsyncTelegram2.cpp b/src/AsyncTelegram2.cpp index 19bd884..5dfe8d5 100644 --- a/src/AsyncTelegram2.cpp +++ b/src/AsyncTelegram2.cpp @@ -38,9 +38,8 @@ bool AsyncTelegram2::checkConnection() log_debug("Start handshaking..."); if (!telegramClient->connect(TELEGRAM_HOST, TELEGRAM_PORT)) - { - Serial.printf("\n\nUnable to connect to Telegram server\n"); - reset(); + { + Serial.println("\n\nUnable to connect to Telegram server"); } #if DEBUG_ENABLE else @@ -532,7 +531,7 @@ bool AsyncTelegram2::forwardMessage(const TBMessage &msg, const int64_t to_chati { char payload[BUFFER_SMALL]; snprintf(payload, BUFFER_SMALL, - "{\"chat_id\":%lld,\"from_chat_id\":%lld,\"message_id\":%d}", + "{\"chat_id\":%lld,\"from_chat_id\":%lld,\"message_id\":%ld}", to_chatid, msg.chatId, msg.messageID); bool result = sendCommand("forwardMessage", payload); @@ -692,8 +691,8 @@ bool AsyncTelegram2::sendStream(int64_t chat_id, const char *cmd, const char *ty telegramClient->print(formData); uint8_t data[BLOCK_SIZE]; - int n_block = trunc(size / BLOCK_SIZE); - int lastBytes = size - (n_block * BLOCK_SIZE); + uint16_t n_block = trunc(size / BLOCK_SIZE); + uint16_t lastBytes = size - (n_block * BLOCK_SIZE); for (uint16_t pos = 0; pos < n_block; pos++) { @@ -742,8 +741,8 @@ bool AsyncTelegram2::sendBuffer(int64_t chat_id, const char *cmd, const char *ty // Serial.println(telegramClient->write((const uint8_t *) data, size)); uint16_t pos = 0; - int n_block = trunc(size / BLOCK_SIZE); - int lastBytes = size - (n_block * BLOCK_SIZE); + uint16_t n_block = trunc(size / BLOCK_SIZE); + uint16_t lastBytes = size - (n_block * BLOCK_SIZE); for (pos = 0; pos < n_block; pos++) { diff --git a/src/AsyncTelegram2.h b/src/AsyncTelegram2.h index 3e3d9ca..619f805 100644 --- a/src/AsyncTelegram2.h +++ b/src/AsyncTelegram2.h @@ -96,7 +96,8 @@ ReYNnyicsbkqWletNw+vHX/bvZ8= class AsyncTelegram2 { - using SentCallback = std::function; + //using SentCallback = std::function; + typedef void(*SentCallback)(bool sent); public: // default constructor diff --git a/src/InlineKeyboard.h b/src/InlineKeyboard.h index d1ca2ed..9078f26 100644 --- a/src/InlineKeyboard.h +++ b/src/InlineKeyboard.h @@ -5,7 +5,7 @@ #define ARDUINOJSON_USE_LONG_LONG 1 #define ARDUINOJSON_DECODE_UNICODE 1 #include -#include +// #include #include "DataStructures.h" enum InlineKeyboardButtonType { @@ -16,8 +16,8 @@ enum InlineKeyboardButtonType { class InlineKeyboard { - -using CallbackType = std::function; + typedef void(*CallbackType)(const TBMessage &msg); +//using CallbackType = std::function; struct InlineButton{ char *btnName; diff --git a/src/ReplyKeyboard.cpp b/src/ReplyKeyboard.cpp index f149e69..787c32c 100644 --- a/src/ReplyKeyboard.cpp +++ b/src/ReplyKeyboard.cpp @@ -2,79 +2,188 @@ ReplyKeyboard::ReplyKeyboard() { + m_json.reserve(BUFFER_MEDIUM); m_json = "{\"keyboard\":[[]]}\""; } ReplyKeyboard::~ReplyKeyboard() {} - bool ReplyKeyboard::addRow() { - if(m_jsonSize < BUFFER_MEDIUM) m_jsonSize = BUFFER_MEDIUM; - DynamicJsonDocument doc(m_jsonSize + 128); // Current size + space for new row (empty) +#if defined(ESP8266) + DynamicJsonDocument doc(ESP.getMaxFreeBlockSize() - BUFFER_MEDIUM); +#elif defined(ESP32) + DynamicJsonDocument doc(ESP.getMaxAllocHeap() - BUFFER_BIG); +#else + DynamicJsonDocument doc(BUFFER_BIG); +#endif + + DeserializationError err = deserializeJson(doc, m_json); + if (err) + { + log_error("deserializeJson() failed: %s\n", err.c_str()); + return false; + } - deserializeJson(doc, m_json); JsonArray rows = doc["keyboard"]; rows.createNestedArray(); m_json = ""; serializeJson(doc, m_json); - doc.shrinkToFit(); - m_jsonSize = doc.memoryUsage(); return true; } - -bool ReplyKeyboard::addButton(const char* text, ReplyKeyboardButtonType buttonType) +bool ReplyKeyboard::addButton(const char *text, ReplyKeyboardButtonType buttonType) { if ((buttonType != KeyboardButtonContact) && - (buttonType != KeyboardButtonLocation) && - (buttonType != KeyboardButtonSimple)) + (buttonType != KeyboardButtonLocation) && + (buttonType != KeyboardButtonSimple)) + { + Serial.println("no button type"); + return false; + } + +#if defined(ESP8266) + DynamicJsonDocument doc(ESP.getMaxFreeBlockSize() - BUFFER_MEDIUM); +#elif defined(ESP32) + DynamicJsonDocument doc(ESP.getMaxAllocHeap() - BUFFER_BIG); +#else + DynamicJsonDocument doc(BUFFER_BIG); +#endif + + DeserializationError err = deserializeJson(doc, m_json); + if (err) + { + log_error("deserializeJson() failed: %s\n", err.c_str()); return false; - // As reccomended use local JsonDocument instead global - // inline keyboard json structure will be stored in a String var - if(m_jsonSize < BUFFER_MEDIUM) m_jsonSize = BUFFER_MEDIUM; - DynamicJsonDocument doc(m_jsonSize + 256); // Current size + space for new object (button) - deserializeJson(doc, m_json); + } - JsonArray rows = doc["keyboard"]; - JsonObject button = rows[rows.size()-1].createNestedObject(); + JsonArray rows = doc["keyboard"]; + JsonObject button = rows[rows.size() - 1].createNestedObject(); button["text"] = text; - switch (buttonType){ - case KeyboardButtonContact: - button["request_contact"] = true; - break; - case KeyboardButtonLocation: - button["request_location"] = true; - break; - default: - break; + switch (buttonType) + { + case KeyboardButtonContact: + button["request_contact"] = true; + break; + case KeyboardButtonLocation: + button["request_location"] = true; + break; + default: + break; } // Store inline keyboard json structure m_json = ""; serializeJson(doc, m_json); - doc.shrinkToFit(); - m_jsonSize = doc.memoryUsage(); return true; } +// bool ReplyKeyboard::addRow() +// { +// if(m_jsonSize < BUFFER_MEDIUM) m_jsonSize = BUFFER_MEDIUM; +// DynamicJsonDocument doc(m_jsonSize + 128); // Current size + space for new row (empty) + +// deserializeJson(doc, m_json); +// JsonArray rows = doc["keyboard"]; +// rows.createNestedArray(); +// m_json = ""; +// serializeJson(doc, m_json); +// doc.shrinkToFit(); +// m_jsonSize = doc.memoryUsage(); +// return true; +// } + +// bool ReplyKeyboard::addButton(const char* text, ReplyKeyboardButtonType buttonType) +// { +// if ((buttonType != KeyboardButtonContact) && +// (buttonType != KeyboardButtonLocation) && +// (buttonType != KeyboardButtonSimple)) +// return false; +// // As reccomended use local JsonDocument instead global +// // inline keyboard json structure will be stored in a String var +// if(m_jsonSize < BUFFER_MEDIUM) m_jsonSize = BUFFER_MEDIUM; +// DynamicJsonDocument doc(m_jsonSize + 256); // Current size + space for new object (button) +// deserializeJson(doc, m_json); + +// JsonArray rows = doc["keyboard"]; +// JsonObject button = rows[rows.size()-1].createNestedObject(); + +// button["text"] = text; +// switch (buttonType){ +// case KeyboardButtonContact: +// button["request_contact"] = true; +// break; +// case KeyboardButtonLocation: +// button["request_location"] = true; +// break; +// default: +// break; +// } + +// // Store inline keyboard json structure +// m_json = ""; +// serializeJson(doc, m_json); +// doc.shrinkToFit(); +// m_jsonSize = doc.memoryUsage(); +// return true; +// } void ReplyKeyboard::enableResize() { - if(m_jsonSize < BUFFER_MEDIUM) m_jsonSize = BUFFER_MEDIUM; - DynamicJsonDocument doc(m_jsonSize + 128); // Current size + space for new field - deserializeJson(doc, m_json); + // if (m_jsonSize < BUFFER_MEDIUM) + // m_jsonSize = BUFFER_MEDIUM; + // DynamicJsonDocument doc(m_jsonSize + 128); // Current size + space for new field + // deserializeJson(doc, m_json); + // doc["resize_keyboard"] = true; + // m_json = ""; + // serializeJson(doc, m_json); + + +#if defined(ESP8266) + DynamicJsonDocument doc(ESP.getMaxFreeBlockSize() - BUFFER_MEDIUM); +#elif defined(ESP32) + DynamicJsonDocument doc(ESP.getMaxAllocHeap() - BUFFER_BIG); +#else + DynamicJsonDocument doc(BUFFER_BIG); +#endif + DeserializationError err = deserializeJson(doc, m_json); + if (err) + { + log_error("deserializeJson() failed: %s\n", err.c_str()); + } + doc["resize_keyboard"] = true; m_json = ""; serializeJson(doc, m_json); + + Serial.println(m_json); } void ReplyKeyboard::enableOneTime() { - if(m_jsonSize < BUFFER_MEDIUM) m_jsonSize = BUFFER_MEDIUM; - DynamicJsonDocument doc(m_jsonSize + 128); // Current size + space for new field - deserializeJson(doc, m_json); + // if (m_jsonSize < BUFFER_MEDIUM) + // m_jsonSize = BUFFER_MEDIUM; + // DynamicJsonDocument doc(m_jsonSize + 128); // Current size + space for new field + // deserializeJson(doc, m_json); + // doc["one_time_keyboard"] = true; + // m_json = ""; + // serializeJson(doc, m_json); + + +#if defined(ESP8266) + DynamicJsonDocument doc(ESP.getMaxFreeBlockSize() - BUFFER_MEDIUM); +#elif defined(ESP32) + DynamicJsonDocument doc(ESP.getMaxAllocHeap() - BUFFER_BIG); +#else + DynamicJsonDocument doc(BUFFER_BIG); +#endif + DeserializationError err = deserializeJson(doc, m_json); + if (err) + { + log_error("deserializeJson() failed: %s\n", err.c_str()); + } + doc["one_time_keyboard"] = true; m_json = ""; serializeJson(doc, m_json); @@ -82,9 +191,28 @@ void ReplyKeyboard::enableOneTime() void ReplyKeyboard::enableSelective() { - if(m_jsonSize < BUFFER_MEDIUM) m_jsonSize = BUFFER_MEDIUM; - DynamicJsonDocument doc(m_jsonSize + 128); // Current size + space for new field - deserializeJson(doc, m_json); + // if (m_jsonSize < BUFFER_MEDIUM) + // m_jsonSize = BUFFER_MEDIUM; + // DynamicJsonDocument doc(m_jsonSize + 128); // Current size + space for new field + // deserializeJson(doc, m_json); + // doc["selective"] = true; + // m_json = ""; + // serializeJson(doc, m_json); + + +#if defined(ESP8266) + DynamicJsonDocument doc(ESP.getMaxFreeBlockSize() - BUFFER_MEDIUM); +#elif defined(ESP32) + DynamicJsonDocument doc(ESP.getMaxAllocHeap() - BUFFER_BIG); +#else + DynamicJsonDocument doc(BUFFER_BIG); +#endif + DeserializationError err = deserializeJson(doc, m_json); + if (err) + { + log_error("deserializeJson() failed: %s\n", err.c_str()); + } + doc["selective"] = true; m_json = ""; serializeJson(doc, m_json); @@ -97,14 +225,30 @@ String ReplyKeyboard::getJSON() const String ReplyKeyboard::getJSONPretty() const { - uint16_t jsonSize; - if(m_jsonSize < BUFFER_MEDIUM) jsonSize = BUFFER_MEDIUM; - DynamicJsonDocument doc(jsonSize + 128); // Current size + space for new lines - deserializeJson(doc, m_json); + // uint16_t jsonSize; + // if (m_jsonSize < BUFFER_MEDIUM) + // jsonSize = BUFFER_MEDIUM; + // DynamicJsonDocument doc(jsonSize + 128); // Current size + space for new lines + // deserializeJson(doc, m_json); + + // String serialized; + // serializeJsonPretty(doc, serialized); + // return serialized; + +#if defined(ESP8266) + DynamicJsonDocument doc(ESP.getMaxFreeBlockSize() - BUFFER_MEDIUM); +#elif defined(ESP32) + DynamicJsonDocument doc(ESP.getMaxAllocHeap() - BUFFER_BIG); +#else + DynamicJsonDocument doc(BUFFER_BIG); +#endif + DeserializationError err = deserializeJson(doc, m_json); + if (err) + { + log_error("deserializeJson() failed: %s\n", err.c_str()); + } String serialized; serializeJsonPretty(doc, serialized); return serialized; } - - diff --git a/src/ReplyKeyboard.h b/src/ReplyKeyboard.h index eb2a835..4dc506e 100644 --- a/src/ReplyKeyboard.h +++ b/src/ReplyKeyboard.h @@ -6,6 +6,7 @@ #define ARDUINOJSON_DECODE_UNICODE 1 #include #include "DataStructures.h" +#include "serial_log.h" enum ReplyKeyboardButtonType { KeyboardButtonSimple = 1, @@ -56,7 +57,7 @@ class ReplyKeyboard inline void clear() { - m_json = ""; + m_json = "{\"keyboard\":[[]]}\""; } };