AsyncTelegram2

Loading...
Searching...
No Matches
InlineKeyboard.cpp
Go to the documentation of this file.
1#include "InlineKeyboard.h"
2
3
4 InlineKeyboard::InlineKeyboard()
5{
6 m_json = "{\"inline_keyboard\":[[]]}\"";
7}
8
9 InlineKeyboard::~InlineKeyboard(){}
10
11 bool InlineKeyboard::addRow()
12{
13 DynamicJsonDocument doc(BUFFER_BIG+BUFFER_MEDIUM);
14 deserializeJson(doc, m_json);
15
16 // Deserialize the JSON document
17 DeserializationError error = deserializeJson(doc, m_json);
18
19 // Test if parsing succeeds.
20 if (error) {
21 Serial.print(F("deserializeJson() failed: "));
22 Serial.println(error.f_str());
23 return false;
24 }
25
26 JsonArray rows = doc["inline_keyboard"];
27 rows.createNestedArray();
28 m_json = "";
29 doc.shrinkToFit();
30 serializeJson(doc, m_json);
31
32 return true;
33}
34
35 bool InlineKeyboard::addButton(const char* text, const char* command, InlineKeyboardButtonType buttonType, CallbackType onClick)
36{
37 if ((buttonType != KeyboardButtonURL) && (buttonType != KeyboardButtonQuery))
38 return false;
39
40 InlineButton *inlineButton = new InlineButton();
41 if (_firstButton == nullptr)
42 _firstButton = inlineButton;
43 else
44 _lastButton->nextButton = inlineButton;
45 inlineButton->argCallback = onClick;
46 inlineButton->btnName = (char*)command;
47 _lastButton = inlineButton;
48 m_buttonsCounter++;
49
50 DynamicJsonDocument doc(BUFFER_BIG+BUFFER_MEDIUM);
51 DeserializationError error = deserializeJson(doc, m_json);
52
53 // Test if parsing succeeds.
54 if (error) {
55 Serial.print(F("deserializeJson() failed: "));
56 Serial.println(error.f_str());
57 return false;
58 }
59
60 JsonArray rows = doc["inline_keyboard"];
61 JsonObject button = rows[rows.size()-1].createNestedObject();
62
63 button["text"] = text ;
64 if(KeyboardButtonURL == buttonType)
65 button["url"] = command;
66 else if (KeyboardButtonQuery == buttonType)
67 button["callback_data"] = command;
68
69 // Store inline keyboard json structure
70 m_json = "";
71 doc.shrinkToFit();
72 serializeJson(doc, m_json);
73 return true;
74}
75
76// Check if a callback function has to be called for this button query message
77 void InlineKeyboard::checkCallback( const TBMessage &msg) {
78 for(InlineButton *_button = _firstButton; _button != nullptr; _button = _button->nextButton){
79 if( msg.callbackQueryData.equals(_button->btnName) && _button->argCallback != nullptr){
80 _button->argCallback(msg);
81 }
82 }
83}
84
85// Get total number of keyboard buttons
86 int InlineKeyboard::getButtonsNumber()
87{
88 return m_buttonsCounter;
89}
90
91String InlineKeyboard::getJSON() const
92{
93 return m_json;
94}
95
96String InlineKeyboard::getJSONPretty() const
97{
98 StaticJsonDocument<BUFFER_MEDIUM> doc;
99 deserializeJson(doc, m_json);
100
101 String serialized;
102 serializeJsonPretty(doc, serialized);
103 return serialized;
104}
105
BUFFER_MEDIUM
#define BUFFER_MEDIUM
Definition: DataStructures.h:6
BUFFER_BIG
#define BUFFER_BIG
Definition: DataStructures.h:5
InlineKeyboard.h
InlineKeyboardButtonType
InlineKeyboardButtonType
Definition: InlineKeyboard.h:11
KeyboardButtonQuery
@ KeyboardButtonQuery
Definition: InlineKeyboard.h:13
KeyboardButtonURL
@ KeyboardButtonURL
Definition: InlineKeyboard.h:12
InlineKeyboard::addButton
bool addButton(const char *text, const char *command, InlineKeyboardButtonType buttonType, CallbackType onClick=nullptr)
Definition: InlineKeyboard.cpp:35
InlineKeyboard::~InlineKeyboard
~InlineKeyboard()
Definition: InlineKeyboard.cpp:9
InlineKeyboard::addRow
bool addRow(void)
Definition: InlineKeyboard.cpp:11
InlineKeyboard::InlineKeyboard
InlineKeyboard()
Definition: InlineKeyboard.cpp:4
InlineKeyboard::getButtonsNumber
int getButtonsNumber()
Definition: InlineKeyboard.cpp:86
InlineKeyboard::getJSON
String getJSON(void) const
Definition: InlineKeyboard.cpp:91
InlineKeyboard::getJSONPretty
String getJSONPretty(void) const
Definition: InlineKeyboard.cpp:96
TBMessage
Definition: DataStructures.h:51
TBMessage::callbackQueryData
String callbackQueryData
Definition: DataStructures.h:67