AsyncTelegram2

Loading...
Searching...
No Matches
ReplyKeyboard.cpp
Go to the documentation of this file.
1#include "ReplyKeyboard.h"
2
3 ReplyKeyboard::ReplyKeyboard()
4{
5 m_json.reserve(BUFFER_MEDIUM);
6 m_json = "{\"keyboard\":[[]]}\"";
7}
8
9 ReplyKeyboard::~ReplyKeyboard() {}
10
11 bool ReplyKeyboard::addRow()
12{
13#if defined(ESP8266)
14 DynamicJsonDocument doc(ESP.getMaxFreeBlockSize() - BUFFER_MEDIUM);
15#elif defined(ESP32)
16 DynamicJsonDocument doc(ESP.getMaxAllocHeap() - BUFFER_BIG);
17#else
18 DynamicJsonDocument doc(BUFFER_BIG);
19#endif
20
21 DeserializationError err = deserializeJson(doc, m_json);
22 if (err)
23 {
24 log_debug("deserializeJson() failed: %s\n", err.c_str());
25 return false;
26 }
27
28 JsonArray rows = doc["keyboard"];
29 rows.createNestedArray();
30 m_json = "";
31 serializeJson(doc, m_json);
32 return true;
33}
34
35 bool ReplyKeyboard::addButton(const char *text, ReplyKeyboardButtonType buttonType)
36{
37 if ((buttonType != KeyboardButtonContact) &&
38 (buttonType != KeyboardButtonLocation) &&
39 (buttonType != KeyboardButtonSimple))
40 {
41 Serial.println("no button type");
42 return false;
43 }
44
45#if defined(ESP8266)
46 DynamicJsonDocument doc(ESP.getMaxFreeBlockSize() - BUFFER_MEDIUM);
47#elif defined(ESP32)
48 DynamicJsonDocument doc(ESP.getMaxAllocHeap() - BUFFER_BIG);
49#else
50 DynamicJsonDocument doc(BUFFER_BIG);
51#endif
52
53 DeserializationError err = deserializeJson(doc, m_json);
54 if (err)
55 {
56 log_debug("deserializeJson() failed: %s\n", err.c_str());
57 return false;
58 }
59
60 JsonArray rows = doc["keyboard"];
61 JsonObject button = rows[rows.size() - 1].createNestedObject();
62
63 button["text"] = text;
64 switch (buttonType)
65 {
66 case KeyboardButtonContact:
67 button["request_contact"] = true;
68 break;
69 case KeyboardButtonLocation:
70 button["request_location"] = true;
71 break;
72 default:
73 break;
74 }
75
76 // Store inline keyboard json structure
77 m_json = "";
78 serializeJson(doc, m_json);
79 return true;
80}
81
82// bool ReplyKeyboard::addRow()
83// {
84// if(m_jsonSize < BUFFER_MEDIUM) m_jsonSize = BUFFER_MEDIUM;
85// DynamicJsonDocument doc(m_jsonSize + 128); // Current size + space for new row (empty)
86
87// deserializeJson(doc, m_json);
88// JsonArray rows = doc["keyboard"];
89// rows.createNestedArray();
90// m_json = "";
91// serializeJson(doc, m_json);
92// doc.shrinkToFit();
93// m_jsonSize = doc.memoryUsage();
94// return true;
95// }
96
97// bool ReplyKeyboard::addButton(const char* text, ReplyKeyboardButtonType buttonType)
98// {
99// if ((buttonType != KeyboardButtonContact) &&
100// (buttonType != KeyboardButtonLocation) &&
101// (buttonType != KeyboardButtonSimple))
102// return false;
103// // As reccomended use local JsonDocument instead global
104// // inline keyboard json structure will be stored in a String var
105// if(m_jsonSize < BUFFER_MEDIUM) m_jsonSize = BUFFER_MEDIUM;
106// DynamicJsonDocument doc(m_jsonSize + 256); // Current size + space for new object (button)
107// deserializeJson(doc, m_json);
108
109// JsonArray rows = doc["keyboard"];
110// JsonObject button = rows[rows.size()-1].createNestedObject();
111
112// button["text"] = text;
113// switch (buttonType){
114// case KeyboardButtonContact:
115// button["request_contact"] = true;
116// break;
117// case KeyboardButtonLocation:
118// button["request_location"] = true;
119// break;
120// default:
121// break;
122// }
123
124// // Store inline keyboard json structure
125// m_json = "";
126// serializeJson(doc, m_json);
127// doc.shrinkToFit();
128// m_jsonSize = doc.memoryUsage();
129// return true;
130// }
131
132 void ReplyKeyboard::enableResize()
133{
134 // if (m_jsonSize < BUFFER_MEDIUM)
135 // m_jsonSize = BUFFER_MEDIUM;
136 // DynamicJsonDocument doc(m_jsonSize + 128); // Current size + space for new field
137 // deserializeJson(doc, m_json);
138 // doc["resize_keyboard"] = true;
139 // m_json = "";
140 // serializeJson(doc, m_json);
141
142
143#if defined(ESP8266)
144 DynamicJsonDocument doc(ESP.getMaxFreeBlockSize() - BUFFER_MEDIUM);
145#elif defined(ESP32)
146 DynamicJsonDocument doc(ESP.getMaxAllocHeap() - BUFFER_BIG);
147#else
148 DynamicJsonDocument doc(BUFFER_BIG);
149#endif
150 DeserializationError err = deserializeJson(doc, m_json);
151 if (err)
152 {
153 log_debug("deserializeJson() failed: %s\n", err.c_str());
154 }
155
156 doc["resize_keyboard"] = true;
157 m_json = "";
158 serializeJson(doc, m_json);
159
160 Serial.println(m_json);
161}
162
163 void ReplyKeyboard::enableOneTime()
164{
165 // if (m_jsonSize < BUFFER_MEDIUM)
166 // m_jsonSize = BUFFER_MEDIUM;
167 // DynamicJsonDocument doc(m_jsonSize + 128); // Current size + space for new field
168 // deserializeJson(doc, m_json);
169 // doc["one_time_keyboard"] = true;
170 // m_json = "";
171 // serializeJson(doc, m_json);
172
173
174#if defined(ESP8266)
175 DynamicJsonDocument doc(ESP.getMaxFreeBlockSize() - BUFFER_MEDIUM);
176#elif defined(ESP32)
177 DynamicJsonDocument doc(ESP.getMaxAllocHeap() - BUFFER_BIG);
178#else
179 DynamicJsonDocument doc(BUFFER_BIG);
180#endif
181 DeserializationError err = deserializeJson(doc, m_json);
182 if (err)
183 {
184 log_debug("deserializeJson() failed: %s\n", err.c_str());
185 }
186
187 doc["one_time_keyboard"] = true;
188 m_json = "";
189 serializeJson(doc, m_json);
190}
191
192 void ReplyKeyboard::enableSelective()
193{
194 // if (m_jsonSize < BUFFER_MEDIUM)
195 // m_jsonSize = BUFFER_MEDIUM;
196 // DynamicJsonDocument doc(m_jsonSize + 128); // Current size + space for new field
197 // deserializeJson(doc, m_json);
198 // doc["selective"] = true;
199 // m_json = "";
200 // serializeJson(doc, m_json);
201
202
203#if defined(ESP8266)
204 DynamicJsonDocument doc(ESP.getMaxFreeBlockSize() - BUFFER_MEDIUM);
205#elif defined(ESP32)
206 DynamicJsonDocument doc(ESP.getMaxAllocHeap() - BUFFER_BIG);
207#else
208 DynamicJsonDocument doc(BUFFER_BIG);
209#endif
210 DeserializationError err = deserializeJson(doc, m_json);
211 if (err)
212 {
213 log_debug("deserializeJson() failed: %s\n", err.c_str());
214 }
215
216 doc["selective"] = true;
217 m_json = "";
218 serializeJson(doc, m_json);
219}
220
221String ReplyKeyboard::getJSON() const
222{
223 return m_json;
224}
225
226String ReplyKeyboard::getJSONPretty() const
227{
228 // uint16_t jsonSize;
229 // if (m_jsonSize < BUFFER_MEDIUM)
230 // jsonSize = BUFFER_MEDIUM;
231 // DynamicJsonDocument doc(jsonSize + 128); // Current size + space for new lines
232 // deserializeJson(doc, m_json);
233
234 // String serialized;
235 // serializeJsonPretty(doc, serialized);
236 // return serialized;
237
238#if defined(ESP8266)
239 DynamicJsonDocument doc(ESP.getMaxFreeBlockSize() - BUFFER_MEDIUM);
240#elif defined(ESP32)
241 DynamicJsonDocument doc(ESP.getMaxAllocHeap() - BUFFER_BIG);
242#else
243 DynamicJsonDocument doc(BUFFER_BIG);
244#endif
245 DeserializationError err = deserializeJson(doc, m_json);
246 if (err)
247 {
248 log_debug("deserializeJson() failed: %s\n", err.c_str());
249 }
250
251 String serialized;
252 serializeJsonPretty(doc, serialized);
253 return serialized;
254}
BUFFER_MEDIUM
#define BUFFER_MEDIUM
Definition: DataStructures.h:6
BUFFER_BIG
#define BUFFER_BIG
Definition: DataStructures.h:5
ReplyKeyboard.h
ReplyKeyboardButtonType
ReplyKeyboardButtonType
Definition: ReplyKeyboard.h:11
KeyboardButtonLocation
@ KeyboardButtonLocation
Definition: ReplyKeyboard.h:14
KeyboardButtonContact
@ KeyboardButtonContact
Definition: ReplyKeyboard.h:13
KeyboardButtonSimple
@ KeyboardButtonSimple
Definition: ReplyKeyboard.h:12
ReplyKeyboard::getJSON
String getJSON(void) const
Definition: ReplyKeyboard.cpp:221
ReplyKeyboard::enableOneTime
void enableOneTime(void)
Definition: ReplyKeyboard.cpp:163
ReplyKeyboard::enableSelective
void enableSelective(void)
Definition: ReplyKeyboard.cpp:192
ReplyKeyboard::~ReplyKeyboard
~ReplyKeyboard()
Definition: ReplyKeyboard.cpp:9
ReplyKeyboard::enableResize
void enableResize(void)
Definition: ReplyKeyboard.cpp:132
ReplyKeyboard::ReplyKeyboard
ReplyKeyboard()
Definition: ReplyKeyboard.cpp:3
ReplyKeyboard::getJSONPretty
String getJSONPretty() const
Definition: ReplyKeyboard.cpp:226
ReplyKeyboard::addRow
bool addRow(void)
Definition: ReplyKeyboard.cpp:11
ReplyKeyboard::addButton
bool addButton(const char *text, ReplyKeyboardButtonType buttonType=KeyboardButtonSimple)
Definition: ReplyKeyboard.cpp:35
log_debug
#define log_debug(format,...)
Definition: serial_log.h:44