From 5a8c49a19e73fbb6251cf1229b3d5cf8ee9a9330 Mon Sep 17 00:00:00 2001 From: DeRobyJ Date: Mon, 29 Jun 2020 22:54:41 +0200 Subject: reply-keyboard sample --- samples/reply-keyboard/src/main.cpp | 89 +++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 samples/reply-keyboard/src/main.cpp (limited to 'samples') 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 +#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", 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; +} -- cgit v1.2.3