Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

560 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tgbot-cpp

GitHub contributors

C++ library for Telegram bot API.

Documentation is located here.

State

  • Telegram Bot API 10.2.

Example

Simple echo bot which sends everything it receives:

#include <iostream>
#include <memory>

#include <tgbot/tgbot.h>

int main() {
    TgBot::Bot bot("PLACE YOUR TOKEN HERE");
    bot.getEvents().onCommand("start", [&bot](std::shared_ptr<TgBot::Message> message) {
        bot.getApi().sendMessage(message->chat->id, "Hi!");
    });
    bot.getEvents().onAnyMessage([&bot](std::shared_ptr<TgBot::Message> message) {
        const auto text = message->text.value_or("");
        std::cout << "User wrote " << text << std::endl;
        if (text.starts_with("/start")) {
            return;
        }
        bot.getApi().sendMessage(message->chat->id, "Your message is: " + text);
    });
    try {
        std::cout << "Bot username: " << bot.getApi().getMe()->username.value_or("") << std::endl;
        TgBot::TgLongPoll longPoll(bot);
        longPoll.startLoop();
    } catch (const TgBot::TgException& e) {
        std::cout << "error: " << e.what() << std::endl;
    }
    return 0;
}

Other examples are available in examples: webhook server, custom curl client, proxy usage, inline and reply keyboards, file upload/download, and command configuration.

Build and run an example against a locally installed copy of the library:

make install INSTALL_PREFIX=build/install
make example EXAMPLE=echobot INSTALL_PREFIX=build/install
TOKEN=... ./build/Release/examples/echobot/echobot

Alternatively, build and run the example in Docker:

TOKEN=... make docker-run-example EXAMPLE=echobot

Also you can build and run all examples at once using Docker Compose. For this copy env.example to env, fill in the values, then run:

make docker-compose-run-examples
make docker-compose-stop-examples

For your own bot image, use reo7sp/tgbot-cpp as the base image.

Usage in CMake

For an installed tgbot-cpp package, add it to your CMakeLists.txt:

find_package(TgBot CONFIG REQUIRED)
target_link_libraries(your_bot PRIVATE TgBot::TgBot)

See the complete echobot CMakeLists.txt.

The repository can also be added directly with add_subdirectory, for example when it is included as a Git submodule. See the submodule example.

Dependencies

Required to build the library:

  • C++20 compiler;
  • CMake 3.16 or newer;
  • Conan 2;
  • Make.

Additionally required for code generation:

  • Python;
  • Poetry.

Conan installs the library dependencies:

  • Boost;
  • nlohmann/json;
  • libcurl;
  • GoogleTest when tests are enabled.

These libraries do not need to be installed separately.

Installing dependencies

Linux

On Debian or Ubuntu, install the build tools and the official Conan 2 package:

sudo apt-get update
sudo apt-get install -y build-essential cmake curl git
CONAN_VERSION=2.31.2
curl -fLO "https://github.com/conan-io/conan/releases/download/${CONAN_VERSION}/conan-${CONAN_VERSION}-$(dpkg --print-architecture).deb"
sudo apt-get install -y "./conan-${CONAN_VERSION}-$(dpkg --print-architecture).deb"

For development, also install Python, Poetry and clang-format:

sudo apt-get install -y clang-format pipx python3
pipx install poetry

macOS

xcode-select --install
brew install cmake conan

For development:

brew install clang-format pipx python
pipx install poetry

Windows

Install Git, Make, CMake, Visual Studio 2022 with the Desktop development with C++ workload, and Conan 2 using its official Windows installer.

For development, also install Python, Poetry and clang-format.

Developing tgbot-cpp

Development checks are exposed through the Makefile:

make format
make lint
make test

Alternatively, via Docker:

make docker-test

Updating Telegram Bot API types

Help page.

Licence

The MIT License.

About

C++ library for Telegram bot API

Topics

Resources

Stars

1.2k stars

Watchers

37 watching

Forks

Releases

Packages

Used by

Contributors

Languages