Closed xlettera closed 1 year ago
ok, thanks for your valuable feedback. I will do some more in-depth tests as soon as possible so that I have a clear idea of what is going on
Perfect. This will be a big step forward for this library. Regards Roberto
So, I checked and actually I made a mistake in resetting the variable. I had to reinitialize using an empty - but valid - keyboard JSON instead using only an empty string.
However, I still believe this way of proceeding is not the best possible.
I would make sure that the functions defineMyKbdStart()
and defineMyKbdOption()
return a String
, to be able to use a local variable only when needed and avoid the risk of causing excessive fragmentation of the heap.
String defineMyKbdStart()
{
isKeyboardActive = false;
ReplyKeyboard kbd;
// add a button that send a message with "Simple button" text
kbd.addButton("Button1");
kbd.addButton("Button2");
kbd.addButton("Button3");
// add a new empty button row
kbd.addRow();
// add another button that send the user position (location)
kbd.addButton("Send Location", KeyboardButtonLocation);
// add another button that send the user contact
kbd.addButton("Send contact", KeyboardButtonContact);
// add a new empty button row
kbd.addRow();
// add a button that send a message with "Hide replyKeyboard" text
// (it will be used to hide the reply keyboard)
kbd.addButton("/hide_keyboard");
// resize the keyboard to fit only the needed space
kbd.enableResize();
return kbd.getJSON();
}
String defineMyKbdOption()
{
isKeyboardActive = false;
ReplyKeyboard kbd;
// add a button that send a message with "Simple button" text
kbd.addButton("Option1");
kbd.addButton("Option2");
kbd.addButton("Option3");
// add a new empty button row
kbd.addRow();
kbd.addButton("/hide_keyboard");
// resize the keyboard to fit only the needed space
kbd.enableResize();
return kbd.getJSON();
}
if (msgText.equalsIgnoreCase("/reply_keyboard1")) {
// the user is asking to show the reply keyboard --> show it
myBot.sendMessage(msg, "This is reply keyboard1:", defineMyKbdStart());
isKeyboardActive = true;
}
else if (msgText.equalsIgnoreCase("/reply_keyboard2")) {
// the user is asking to show the reply keyboard --> show it
myBot.sendMessage(msg, "This is reply keyboard2:", defineMyKbdOption());
isKeyboardActive = true;
}
Dear Tolentino, The clear() method now works perfectly. I will follow your advices. Thank you. Regards Roberto
Dear Tolentino, First of all thank you for having added clear method. Maybe I am using it in a wrong way, or I am doing something wrong. But the keyboard does not show up.
The general idea is to define just one keyboard with the reply keyboard object helper, then with clear method I would like to empty it and then I would like to redefine and reuse it with another layout or with other labels for example.
I have modified your keyboards sketch to show you this issue. If you remove myReplyKbd.clear(); from the code and you type _/replykeyboard1 you will get keyboard1 but if you type _/replykeyboard2 you will have two keyboards together. If I you put myReplyKbd.clear(); the keyboard does not show up. Only the right keyboard should appear on the screen.
I hope I was clear.
Thank you for your help. Roberto