Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
cotestatnt authored Dec 19, 2022
1 parent 28b7fa9 commit 185dfb8
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 56 deletions.
15 changes: 7 additions & 8 deletions src/AsyncTelegram2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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++)
{
Expand Down Expand Up @@ -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++)
{
Expand Down
3 changes: 2 additions & 1 deletion src/AsyncTelegram2.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ ReYNnyicsbkqWletNw+vHX/bvZ8=
class AsyncTelegram2
{

using SentCallback = std::function<void(bool sent)>;
//using SentCallback = std::function<void(bool sent)>;
typedef void(*SentCallback)(bool sent);

public:
// default constructor
Expand Down
6 changes: 3 additions & 3 deletions src/InlineKeyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#define ARDUINOJSON_USE_LONG_LONG 1
#define ARDUINOJSON_DECODE_UNICODE 1
#include <ArduinoJson.h>
#include <functional>
// #include <functional>
#include "DataStructures.h"

enum InlineKeyboardButtonType {
Expand All @@ -16,8 +16,8 @@ enum InlineKeyboardButtonType {

class InlineKeyboard
{

using CallbackType = std::function<void(const TBMessage &msg)>;
typedef void(*CallbackType)(const TBMessage &msg);
//using CallbackType = std::function<void(const TBMessage &msg)>;

struct InlineButton{
char *btnName;
Expand Down
230 changes: 187 additions & 43 deletions src/ReplyKeyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,89 +2,217 @@

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

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


3 changes: 2 additions & 1 deletion src/ReplyKeyboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#define ARDUINOJSON_DECODE_UNICODE 1
#include <ArduinoJson.h>
#include "DataStructures.h"
#include "serial_log.h"

enum ReplyKeyboardButtonType {
KeyboardButtonSimple = 1,
Expand Down Expand Up @@ -56,7 +57,7 @@ class ReplyKeyboard


inline void clear() {
m_json = "";
m_json = "{\"keyboard\":[[]]}\"";
}
};

Expand Down

0 comments on commit 185dfb8

Please sign in to comment.