Skip to content

Commit

Permalink
Added Buttons examples
Browse files Browse the repository at this point in the history
  • Loading branch information
baderouaich committed Dec 30, 2023
1 parent 47273a8 commit 14e29e3
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 85 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.10)
project(buttons)
project(inline_buttons)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
Expand All @@ -12,4 +12,4 @@ FetchContent_Declare(tgbotxx
FetchContent_MakeAvailable(tgbotxx)

add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} PUBLIC tgbotxx)
target_link_libraries(${PROJECT_NAME} PUBLIC tgbotxx)
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
## Buttons
## Inline Buttons
This example shows how to program a basic Telegram Bot that uses inline keyboard buttons to interact with users.

### Run
```bash
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make -j8
./buttons YOUR_BOT_TOKEN
./inline_buttons YOUR_BOT_TOKEN
```

### How to create a new Bot and obtain its private token ?
Expand Down
File renamed without changes
77 changes: 77 additions & 0 deletions examples/Buttons/InlineKeyboardButton/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include <tgbotxx/tgbotxx.hpp>
#include <iostream>
#include <csignal>

using namespace tgbotxx;

class InlineButtonsBot : public Bot {
public:
InlineButtonsBot(const std::string &token) : Bot(token) {}

private:
void onStart() override {
Ptr<BotCommand> startCmd(new BotCommand());
startCmd->command = "/start";
startCmd->description = "Start the Bot";
api()->setMyCommands({startCmd});

std::cout << "Bot " << api()->getMe()->username << " Started\n";
}

void onCommand(const Ptr<Message> &message) override {
if (message->text == "/start") {
/** === Create inline keyboard buttons === */
/// InlineKeyboardMarkup:
/// Row ->
/// ______________ ______________
/// | Yes | | No | <--- 2x InlineKeyboardButton
/// -------------- --------------
Ptr<InlineKeyboardMarkup> keyboard(new InlineKeyboardMarkup());
std::vector<Ptr<InlineKeyboardButton>> row;
Ptr<InlineKeyboardButton> yesButton(new InlineKeyboardButton());
yesButton->text = "Yes";
yesButton->callbackData = "YesButton"; // will be received by below onCallbackQuery() method if the 'Yes' button was clicked.
row.push_back(yesButton);
Ptr<InlineKeyboardButton> noButton(new InlineKeyboardButton());
noButton->text = "No";
noButton->callbackData = "NoButton"; // will be received by below onCallbackQuery() method if the 'No' button was clicked.
row.push_back(noButton);
keyboard->inlineKeyboard.push_back(row);

api()->sendMessage(message->chat->id, "Hello! Please click a button:", 0, "", {}, false, false, false, 0, false, keyboard);
}
}

void onCallbackQuery(const Ptr<CallbackQuery> &callbackQuery) override {
if (callbackQuery->data == "YesButton") // callbackQuery->data will hold the InlineKeyboardButton::callbackData set above
{
api()->sendMessage(callbackQuery->message->chat->id, "You have clicked the 'Yes' button!");
}
else if (callbackQuery->data == "NoButton") {
api()->sendMessage(callbackQuery->message->chat->id, "You have clicked the 'No' button!");
}
}

void onStop() override {
std::cout << "Bot " << api()->getMe()->username << " Stopped\n";
}
};


int main(int argc, const char *argv[]) {
if (argc < 2) {
std::cerr << "Usage:\ninline_buttons \"BOT_TOKEN\"\n";
return EXIT_FAILURE;
}

static std::unique_ptr<InlineButtonsBot> BOT(new InlineButtonsBot(argv[1]));
std::signal(SIGINT, [](int) { // Graceful Bot exit on CTRL+C
if (BOT) {
std::cout << "Stopping Bot. Please wait...\n";
BOT->stop();
}
std::exit(EXIT_SUCCESS);
});
BOT->start();
return EXIT_SUCCESS;
}
15 changes: 15 additions & 0 deletions examples/Buttons/ReplyKeyboardMarkup/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.10)
project(keyboard_buttons)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(FetchContent)
FetchContent_Declare(tgbotxx
GIT_REPOSITORY "https://github.com/baderouaich/tgbotxx"
GIT_TAG main
)
FetchContent_MakeAvailable(tgbotxx)

add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} PUBLIC tgbotxx)
24 changes: 24 additions & 0 deletions examples/Buttons/ReplyKeyboardMarkup/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## Keyboard Buttons
This example shows how to program a basic Telegram Bot that uses keyboard buttons to interact with users.

Bots are able to interpret free text input from users, but offering specific suggestions is often more intuitive – this is where custom keyboards can be extremely useful.

Whenever your bot sends a message, it can display a special keyboard with predefined reply options (see ReplyKeyboardMarkup). Telegram apps that receive the message will display your keyboard to the user. Using any of the buttons will immediately send the respective text. This way you can drastically simplify and streamline user interaction with your bot.


### Run
```bash
mkdir build && cd build
cmake .. -DCMAKE_BUILD_TYPE=Release
make -j8
./keyboard_buttons YOUR_BOT_TOKEN
```

### How to create a new Bot and obtain its private token ?
1. Open the Telegram mobile app and search BotFather
2. Send BotFather a command /newbot
3. Follow instructions to create a new Bot
4. After you finish the instructions, you will receive a Bot Token, make sure you keep it secured.

### Preview
<img src="img/preview.jpg" alt="Preview" width="300">
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
78 changes: 78 additions & 0 deletions examples/Buttons/ReplyKeyboardMarkup/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#include <tgbotxx/tgbotxx.hpp>
#include <iostream>
#include <csignal>

using namespace tgbotxx;

class KeyboardButtonsBot : public Bot {
public:
KeyboardButtonsBot(const std::string &token) : Bot(token) {}

private:
void onStart() override {
Ptr<BotCommand> startCmd(new BotCommand());
startCmd->command = "/start";
startCmd->description = "Start the Bot";
api()->setMyCommands({startCmd});

std::cout << "Bot " << api()->getMe()->username << " Started\n";
}

void onCommand(const Ptr<Message> &message) override {
if (message->text == "/start") {
/*
Create a reply keyboard buttons:
ReplyKeyboardMarkup
2 Rows ->
button1 button2 <- x2 KeyboardButton
button3 button4 <- x2 KeyboardButton
*/
Ptr<ReplyKeyboardMarkup> replyKeyboardMarkup(new ReplyKeyboardMarkup());
replyKeyboardMarkup->oneTimeKeyboard = true;
replyKeyboardMarkup->resizeKeyboard = true;
std::vector<Ptr<KeyboardButton>> row1, row2;
Ptr<KeyboardButton> button1(new KeyboardButton());
button1->text = "Button1";
row1.push_back(button1);
Ptr<KeyboardButton> button2(new KeyboardButton());
button2->text = "Button2";
row1.push_back(button2);
Ptr<KeyboardButton> button3(new KeyboardButton());
button3->text = "Button3";
row2.push_back(button3);
Ptr<KeyboardButton> button4(new KeyboardButton());
button4->text = "Button4";
row2.push_back(button4);
replyKeyboardMarkup->keyboard.push_back(row1);
replyKeyboardMarkup->keyboard.push_back(row2);

// User will click a button, and we will receive button text as a message:
api()->sendMessage(message->chat->id, "Please answer by clicking one of the buttons:",
0, "", {}, false, false, false, 0, false, replyKeyboardMarkup);

}
}

void onStop() override {
std::cout << "Bot " << api()->getMe()->username << " Stopped\n";
}
};


int main(int argc, const char *argv[]) {
if (argc < 2) {
std::cerr << "Usage:\nkeyboard_buttons \"BOT_TOKEN\"\n";
return EXIT_FAILURE;
}

static std::unique_ptr<KeyboardButtonsBot> BOT(new KeyboardButtonsBot(argv[1]));
std::signal(SIGINT, [](int) { // Graceful Bot exit on CTRL+C
if (BOT) {
std::cout << "Stopping Bot. Please wait...\n";
BOT->stop();
}
std::exit(EXIT_SUCCESS);
});
BOT->start();
return EXIT_SUCCESS;
}
81 changes: 0 additions & 81 deletions examples/Buttons/main.cpp

This file was deleted.

0 comments on commit 14e29e3

Please sign in to comment.