reo7sp / tgbot-cpp

C++ library for Telegram bot API
http://reo7sp.github.io/tgbot-cpp
MIT License
1.02k stars 247 forks source link

Api: callback button spinning circle #89

Open teterevlev opened 5 years ago

teterevlev commented 5 years ago

When I use the sample "inline-keyboard" on Android, the tapped button gets a spinning circle (as though waiting for answer). All everything else is ok. image

Here the code:

#include <exception>
#include <tgbot/tgbot.h>
using namespace std;
using namespace TgBot;
int main() {
    Bot bot(token);
    InlineKeyboardMarkup::Ptr keyboard(new InlineKeyboardMarkup);
    vector<InlineKeyboardButton::Ptr> row0;
    InlineKeyboardButton::Ptr checkButton(new InlineKeyboardButton);
    checkButton->text = "check";
    checkButton->callbackData = "check";
    row0.push_back(checkButton);
    keyboard->inlineKeyboard.push_back(row0);
    bot.getEvents().onCommand("start", [&bot, &keyboard](Message::Ptr message) {
        bot.getApi().sendMessage(message->chat->id, "ok", false, 0, keyboard);
    });
    bot.getEvents().onCallbackQuery([&bot, &keyboard](CallbackQuery::Ptr query) {
        if (StringTools::startsWith(query->data, "check")) {
            bot.getApi().sendMessage(query->message->chat->id, "ok", false, 0, keyboard);
        }
    });
    try {
        printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str());
        TgLongPoll longPoll(bot);
        while (true) {
            longPoll.start();
        }
    } catch (TgException& e) {
        printf("error: %s\n", e.what());
    }

    return 0;
}
Sil3ntStorm commented 5 years ago

You have to call answerCallbackQuery (even if you don't want to give any user feedback) when reacting on Keyboard Callbacks. See Telegram Bot API documentation, especially the Note there. Upon calling that function the spinning circle will disappear, without calling the function it will only disappear after a few seconds.