Skip to content

Royna2544/tgbot-cpp

 
 

Repository files navigation

tgbot-cpp

GitHub contributors

C++ library for Telegram bot API.

Documentation is located here.

State

This library implements all types and methods of Telegram Bot API 10.1. The type parsers are generated from the machine-readable spec (pinned in scripts/api_spec.ref) and a CI parity gate (scripts/gen_parsers.py --check) keeps them in sync.

Sample

Simple echo bot which sends everything it receives:

#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;
}

All other samples are located here.

Dependencies

Dependencies:

  • CMake
  • OpenSSL
  • ZLib
  • nlohmann/json

The HTTP backend (cpp-httplib) is vendored in third_party/, so it does not need to be installed separately. Boost is only required to build the unit tests.

Library installation on Linux

You can install dependencies on Debian-based distibutives with these commands:

sudo apt install g++ make binutils cmake libssl-dev zlib1g-dev nlohmann-json3-dev

Optionally, install the dependencies for testing and documenting

sudo apt install libboost-test-dev doxygen

You can compile and install the library with these commands:

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

Alternatively, you can use Docker to build and run your bot. Set the base image of your's Dockerfile to reo7sp/tgbot-cpp.

Library installation on MacOS

You can install dependencies with these commands:

brew install gcc cmake openssl zlib nlohmann-json

You can compile and install the library like Linux instructions.

Library installation on Windows

Download vcpkg and tgbot-cpp

Taken from Vcpkg - Quick Start: Windows.

Prerequisites:

First, download and bootstrap vcpkg itself; it can be installed anywhere, but generally we recommend using vcpkg as a submodule for CMake projects, and installing it globally for Visual Studio projects. We recommend somewhere like C:\src\vcpkg or C:\dev\vcpkg, since otherwise you may run into path issues for some port build systems.

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

In order to use vcpkg with Visual Studio, run the following command (may require administrator elevation):

> .\vcpkg\vcpkg integrate install

To install the libraries for Windows x64, run:

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

To install for Windows x86, run:

> .\vcpkg\vcpkg install tgbot-cpp

The library will now be installed and Visual Studio should be able to find the vcpkg installation.

Setup project with CMakeLists

Use the example CMakeLists.txt with changes:

  1. Remove /usr/local/include
  2. Change /usr/local/lib/libTgBot.a to C:/src/vcpkg/installed/x64-windows/lib/TgBot.lib or something simmilar according to your own installation path.

Bot compilation

With CMake

Example CMakeLists.txt

Also, you can treat this repository as a submodule of your project, for example, see echobot-submodule.

Logging

The library emits diagnostics (network retries, rate limiting, webhook handler errors, request traces) through a small TgBot::Logger interface. By default a DefaultLogger prints warnings and errors to stderr.

To route logs into your own backend, implement TgBot::Logger and register it with TgBot::setLogger. The library does not depend on any logging framework, so you are free to bridge it to whatever you use. For example, with spdlog:

#include <tgbot/tgbot.h>
#include <spdlog/spdlog.h>

class SpdlogSink : public TgBot::Logger {
public:
    void log(TgBot::LogLevel level, const std::string& message) override {
        switch (level) {
            case TgBot::LogLevel::Trace:   spdlog::trace(message); break;
            case TgBot::LogLevel::Debug:   spdlog::debug(message); break;
            case TgBot::LogLevel::Info:    spdlog::info(message); break;
            case TgBot::LogLevel::Warning: spdlog::warn(message); break;
            case TgBot::LogLevel::Error:   spdlog::error(message); break;
            case TgBot::LogLevel::Off:     break;
        }
    }
};

// before creating the Bot:
TgBot::setLogger(std::make_shared<SpdlogSink>());

To change the verbosity of the built-in logger, register a DefaultLogger with a different minimum level, e.g. TgBot::setLogger(std::make_shared<TgBot::DefaultLogger>(TgBot::LogLevel::Trace)).

Licence

The MIT License.

About

C++ library for Telegram bot API

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • C++ 95.2%
  • Python 3.1%
  • C 1.3%
  • Other 0.4%