This repository contains a Telegram Bot implemented in C++. The bot is designed to handle user commands, enforce rate limits, and interact with the Telegram API to send and receive messages. It is a robust and scalable solution for building custom Telegram bots with advanced features.
- Command Handling: Register and execute custom commands dynamically.
- Rate Limiting: Prevent abuse by limiting user requests within a specified time interval.
- Telegram API Integration: Send and receive messages with optional Markdown formatting.
- Thread Safety: Ensures safe access to shared resources using mutexes.
- JSON Parsing: Processes Telegram API responses using JsonCpp.
- Logging: Provides detailed logs for debugging and monitoring bot activity.
- libcurl: For HTTP requests and URL encoding.
- JsonCpp: For parsing JSON responses.
- C++ Compiler: Supports C++11 or later.
-
Clone the repository:
git clone https://github.com/genyleap/telegram-bot cd telegram-bot
-
Install dependencies:
sudo apt-get install libcurl4-openssl-dev libjsoncpp-dev
-
Build the project:
mkdir build cd build cmake .. make
-
Run the bot:
./TelegramBot YOUR_TELEGRAM_BOT_TOKEN
-
Clone the repository:
git clone https://github.com/genyleap/telegram-bot cd telegram-bot
-
Install dependencies:
- Download and install vcpkg for managing dependencies.
- Install
libcurl
andjsoncpp
using vcpkg:vcpkg install curl jsoncpp
-
Build the project:
- Open the project in Visual Studio or use CMake:
mkdir build cd build cmake -DCMAKE_TOOLCHAIN_FILE=[path-to-vcpkg]/scripts/buildsystems/vcpkg.cmake .. cmake --build .
- Open the project in Visual Studio or use CMake:
-
Run the bot:
.\TelegramBot YOUR_TELEGRAM_BOT_TOKEN
-
Clone the repository:
git clone https://github.com/genyleap/telegram-bot cd telegram-bot
-
Install dependencies:
brew install curl jsoncpp
-
Build the project:
mkdir build cd build cmake .. make
-
Run the bot:
./TelegramBot YOUR_TELEGRAM_BOT_TOKEN
You can register custom commands with the bot:
TelegramBot bot("YOUR_TELEGRAM_BOT_TOKEN");
bot.registerCommand(Command("start", [](const std::string& chatId, const std::string& args, TelegramBot& bot) {
bot.sendMessage(chatId, "Welcome to the bot!");
}));
bot.registerCommand(Command("help", [](const std::string& chatId, const std::string& args, TelegramBot& bot) {
bot.sendMessage(chatId, "Available commands: /start, /help");
}));
bot.start();
The bot can send messages with optional Markdown formatting:
bot.sendMessage(chatId, "Hello, world!", true); // Markdown enabled
-
Rate Limiting: Adjust the rate limit settings in the
RateLimiter
constructor:RateLimiter rateLimiter(5, std::chrono::seconds(10)); // 5 requests per 10 seconds
-
Logging: Customize logging levels and output as needed.
/start
: Welcomes the user./help
: Lists available commands.- Add custom commands to suit your bot's functionality.
Contributions are welcome! Please open an issue or submit a pull request for any improvements, bug fixes, or new features.
This project is licensed under the MIT License. See the LICENSE file for details.
- libcurl: For handling HTTP requests.
- JsonCpp: For parsing JSON responses.
- Telegram API: For providing the bot infrastructure.
Enjoy building your Telegram bot with this C++ implementation! 🚀