-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
47273a8
commit 14e29e3
Showing
9 changed files
with
198 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
examples/Buttons/README.md → ...es/Buttons/InlineKeyboardButton/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file was deleted.
Oops, something went wrong.