Why C++ for a Telegram bot? It's not madness
\nWhen it comes to creating Telegram bots, Python, Node.js, or Go are the first things that come to mind. They are simple, have thousands of ready-made libraries, and huge communities. But what if your project requires maximum performance? Or are you writing a high-load parser? Or maybe you're just a C++ fan and want to do everything "the hardcore way"?
\nWriting a Telegram bot in C++ is a perfectly feasible task. Yes, it's not the most popular path, but it has serious advantages: speed, low memory consumption, and full control over every byte. In this guide, we'll go from installing libraries to a finished bot that responds to commands and parses data. Let's go!
\n\nWhat do we need?
\nFor this work, we will need:
\n- \n
- C++17/20 compiler — g++ (Linux/macOS) or MSVC (Windows). \n
- CMake — build system (version 3.10+). \n
- tgbot-cpp library — the most popular C++ wrapper for the Telegram Bot API. \n
- libcurl — for HTTP requests (included with tgbot-cpp). \n
- nlohmann/json — for working with JSON (also included as a dependency). \n
- Bot token — get it from @BotFather in Telegram. \n
We will write all the code under Linux (Ubuntu 22.04), but it can be easily adapted for Windows via vcpkg or Conan.
\n\nStep 1: Installing libraries
\nFirst, let's install the dependencies. In the terminal, run:
\nsudo apt update\nsudo apt install git cmake g++ libcurl4-openssl-dev libssl-dev nlohmann-json3-dev\nNow clone and build tgbot-cpp:
\ngit clone https://github.com/reo7sp/tgbot-cpp.git\ncd tgbot-cpp\nmkdir build && cd build\ncmake ..\nmake -j4\nsudo make install\nIf everything went well, the library is installed on the system. You can check with the command ls /usr/local/include/tgbot/ — you will see the header files.
Step 2: Project structure and CMakeLists.txt
\nCreate a folder for the project:
\nmkdir my_telegram_bot && cd my_telegram_bot\ntouch main.cpp CMakeLists.txt\nIn CMakeLists.txt, write the build configuration:
\ncmake_minimum_required(VERSION 3.10)\nproject(MyTelegramBot)\n\nset(CMAKE_CXX_STANDARD 17)\n\nfind_package(TgBot REQUIRED)\nfind_package(OpenSSL REQUIRED)\nfind_package(CURL REQUIRED)\n\nadd_executable(my_bot main.cpp)\ntarget_link_libraries(my_bot TgBot::TgBot OpenSSL::SSL OpenSSL::Crypto CURL::libcurl)\nNote: we are linking OpenSSL and libcurl — they are needed for encryption and HTTP requests.
\n\nStep 3: Writing the bot core in C++
\nOpen main.cpp and write the minimal structure:
\n#include <tgbot/tgbot.h>\n#include <iostream>\n\nint main() {\n // Replace with your token\n std::string token = "1234567890:ABCdefGHIjklMNOpqrsTUVwxyz";\n\n TgBot::Bot bot(token);\n\n // Handler for the /start command\n bot.getEvents().onCommand("start", [&bot](TgBot::Message::Ptr message) {\n bot.getApi().sendMessage(message->chat->id, "Hello! I am a bot in C++. How can I help you?");\n });\n\n // Handler for any text message\n bot.getEvents().onAnyMessage([&bot](TgBot::Message::Ptr message) {\n if (message->text == "/start") return; // Ignore duplicates\n bot.getApi().sendMessage(message->chat->id, "You wrote: " + message->text);\n });\n\n try {\n std::cout << "Bot started. Waiting for messages..." << std::endl;\n TgBot::TgLongPoll longPoll(bot);\n while (true) {\n longPoll.start();\n }\n } catch (TgBot::TgException& e) {\n std::cerr << "Error: " << e.what() << std::endl;\n }\n\n