#include #include #include #include #include #include #include using namespace std; using namespace TgBot; void createOneColumnKeyboard(const vector& buttonStrings, ReplyKeyboardMarkup::Ptr& kb) { for (size_t i = 0; i < buttonStrings.size(); ++i) { vector row; KeyboardButton::Ptr button(new KeyboardButton); button->text = buttonStrings[i]; row.push_back(button); kb->keyboard.push_back(row); } } void createKeyboard(const vector>& buttonLayout, ReplyKeyboardMarkup::Ptr& kb) { for (size_t i = 0; i < buttonLayout.size(); ++i) { vector row; for (size_t j = 0; j < buttonLayout[i].size(); ++j) { KeyboardButton::Ptr button(new KeyboardButton); button->text = buttonLayout[i][j]; row.push_back(button); } kb->keyboard.push_back(row); } } int main() { string token(getenv("TOKEN")); printf("Token: %s\n", token.c_str()); Bot bot(token); ReplyKeyboardMarkup::Ptr keyboardOneCol(new ReplyKeyboardMarkup); createOneColumnKeyboard({"Option 1", "Option 2", "Option 3"}, keyboardOneCol); ReplyKeyboardMarkup::Ptr keyboardWithLayout(new ReplyKeyboardMarkup); createKeyboard({ {"Dog", "Cat", "Mouse"}, {"Green", "White", "Red"}, {"On", "Off"}, {"Back"}, {"Info", "About", "Map", "Etc"} }, keyboardWithLayout); bot.getEvents().onCommand("start", [&bot, &keyboardOneCol](Message::Ptr message) { bot.getApi().sendMessage(message->chat->id, "/start for one column keyboard\n/layout for a more complex keyboard", nullptr, nullptr, keyboardOneCol); }); bot.getEvents().onCommand("layout", [&bot, &keyboardWithLayout](Message::Ptr message) { bot.getApi().sendMessage(message->chat->id, "/start for one column keyboard\n/layout for a more complex keyboard", nullptr, nullptr, keyboardWithLayout); }); bot.getEvents().onAnyMessage([&bot](Message::Ptr message) { printf("User wrote %s\n", message->text.c_str()); if (StringTools::startsWith(message->text, "/start") || StringTools::startsWith(message->text, "/layout")) { return; } bot.getApi().sendMessage(message->chat->id, "Your message is: " + message->text); }); signal(SIGINT, [](int s) { printf("SIGINT got\n"); exit(0); }); try { printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str()); bot.getApi().deleteWebhook(); TgLongPoll longPoll(bot); while (true) { printf("Long poll started\n"); longPoll.start(); } } catch (exception& e) { printf("error: %s\n", e.what()); } return 0; }