tgbot-cpp

repository·master·Indexed 22 days ago

https://github.com/reo7sp/tgbot-cpp

A C++ library for interacting with the Telegram Bot API. It provides high-level abstractions for handling bot events, messages, and commands, utilizing TgBot::Bot for API interaction and TgBot::TgLongPoll for update listening.

Tokens
1.3K
Snippets
6
Records
6
Agent score
29%

What's inside tgbot-cpp

  1. Install tgbot-cpp on MacOS

    master

    Install dependencies using Homebrew:

    brew install gcc cmake boost openssl zlib curl

    After installing dependencies, follow the Linux compilation instructions (clone, cmake, make, make install) to build the library.

  2. Install tgbot-cpp on Linux

    master

    1. Install Dependencies

    On Debian-based distributions, install the required libraries using apt:

    sudo apt install g++ make binutils cmake libboost-system-dev libssl-dev zlib1g-dev libcurl4-openssl-dev

    Optional dependencies for testing and documentation:

    sudo apt install libboost-test-dev doxygen

    2. Compile and Install

    Clone the repository and build using CMake:

    git clone https://github.com/reo7sp/tgbot-cpp
    cd tgbot-cpp
    cmake .
    make -j4
    sudo make install

    3. Docker Alternative

    You can use the pre-built Docker image reo7sp/tgbot-cpp as your base image in your Dockerfile.

  3. Compile your bot with CMake or g++

    master

    Using CMake

    You can use the provided samples/echobot/CMakeLists.txt as a template. If you are on Windows using vcpkg, remember to:

    1. Remove /usr/local/include.
    2. Update the library path (e.g., /usr/local/lib/libTgBot.a) to your vcpkg installation path (e.g., C:/src/vcpkg/installed/x64-windows/lib/TgBot.lib).

    You can also include this repository as a submodule.

    Using g++ (Manual Compilation)

    If not using CMake, compile manually with the following flags:

    g++ telegram_bot.cpp -o telegram_bot --std=c++14 -I/usr/local/include -lTgBot -lboost_system -lssl -lcrypto -lpthread
  4. Install tgbot-cpp on Windows via vcpkg

    master

    1. Setup vcpkg

    Ensure you have Windows 7+, Git, and Visual Studio 2015 Update 3+ (with English language pack) installed.

    Clone and bootstrap vcpkg:

    > git clone https://github.com/microsoft/vcpkg
    > .\vcpkg\bootstrap-vcpkg.bat

    Integrate vcpkg with Visual Studio (requires admin):

    > .\vcpkg\vcpkg integrate install

    2. Install tgbot-cpp

    For x64 Windows:

    > .\vcpkg\vcpkg install tgbot-cpp:x64-windows

    For x86 Windows:

    > .\vcpkg\vcpkg install tgbot-cpp
  5. Create a simple echo bot with tgbot-cpp

    master

    You can create a basic Telegram bot by instantiating a TgBot::Bot object with your API token. Use bot.getEvents() to register event handlers like onCommand for specific commands or onAnyMessage for all incoming messages. To keep the bot running and listening for updates, use TgBot::TgLongPoll in a loop.

    Note: The bot uses bot.getApi() to access the Telegram Bot API methods (e.g., sendMessage).

    #include <stdio.h>
    #include <tgbot/tgbot.h>
    
    int main() {
        TgBot::Bot bot("PLACE YOUR TOKEN HERE");
        bot.getEvents().onCommand("start", [&bot](TgBot::Message::Ptr message) {
            bot.getApi().sendMessage(message->chat->id, "Hi!");
        });
        bot.getEvents().onAnyMessage([&bot](TgBot::Message::Ptr message) {
            printf("User wrote %s\n", message->text.c_str());
            if (StringTools::startsWith(message->text, "/start")) {
                return;
            }
            bot.getApi().sendMessage(message->chat->id, "Your message is: " + message->text);
        });
        try {
            printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str());
            TgBot::TgLongPoll longPoll(bot);
            while (true) {
                printf("Long poll started\n");
                longPoll.start();
            }
        } catch (TgBot::TgException& e) {
            printf("error: %s\n", e.what());
        }
        return 0;
    }
  6. Build options for tgbot-cpp

    master

    When building the library with CMake, you can use the following flags to tune performance:

    • DTGBOT_DISABLE_NAGLES_ALGORITHM: Disables Nagle's algorithm.
    • DTGBOT_CHANGE_SOCKET_BUFFER_SIZE: Expands the socket buffer size.
    • DTGBOT_CHANGE_READ_BUFFER_SIZE: Expands the read buffer size.
    -DTGBOT_DISABLE_NAGLES_ALGORITHM
    -DTGBOT_CHANGE_SOCKET_BUFFER_SIZE
    -DTGBOT_CHANGE_READ_BUFFER_SIZE