diff options
author | DeRobyJ <giakka96@hotmail.it> | 2020-06-29 22:54:41 +0200 |
---|---|---|
committer | DeRobyJ <giakka96@hotmail.it> | 2020-06-29 22:54:41 +0200 |
commit | 5a8c49a19e73fbb6251cf1229b3d5cf8ee9a9330 (patch) | |
tree | 26f711c8d220125a73d4fcc939f4dc94acbc2742 /samples | |
parent | 6436825328cd3bf4a1c964bc84638bc93cfaff1e (diff) |
reply-keyboard sample
Diffstat (limited to 'samples')
-rw-r--r-- | samples/reply-keyboard/src/main.cpp | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/samples/reply-keyboard/src/main.cpp b/samples/reply-keyboard/src/main.cpp new file mode 100644 index 0000000..9478368 --- /dev/null +++ b/samples/reply-keyboard/src/main.cpp @@ -0,0 +1,89 @@ +#include <csignal> +#include <cstdio> +#include <cstdlib> +#include <exception> +#include <string> +#include <vector> + +#include <tgbot/tgbot.h> + +using namespace std; +using namespace TgBot; + +void createOneColumnKeyboard(const vector<string>& buttonStrings, ReplyKeyboardMarkup::Ptr& kb) +{ + for (size_t i = 0; i < buttonStrings.size(); ++i) { + vector<KeyboardButton::Ptr> row; + KeyboardButton::Ptr button(new KeyboardButton); + button->text = buttonStrings[i]; + row.push_back(button); + kb->keyboard.push_back(row); + } +} + +void createKeyboard(const vector<vector<string>>& buttonLayout, ReplyKeyboardMarkup::Ptr& kb) +{ + for (size_t i = 0; i < buttonLayout.size(); ++i) { + vector<KeyboardButton::Ptr> 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", false, 0, 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", false, 0, 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; +} |