Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

https://github.com/baderouaich/tgbotxx

Telegram Bot C++ Library
https://github.com/baderouaich/tgbotxx

bot botfather bots cplusplus cplusplus-20 cpp cpp-library cpp20 telegram telegram-api telegram-bot telegram-bot-api telegram-bot-app telegram-bot-framework telegram-bots telegram-userbot telegrambot telegrambotcpp tgbot tgbotxx

Last synced: about 2 months ago
JSON representation

Telegram Bot C++ Library

Lists

README

        

[![MIT License](https://img.shields.io/badge/license-MIT-yellow)](https://github.com/baderouaich/tgbotxx/blob/main/LICENSE)
[![Docs](https://codedocs.xyz/doxygen/doxygen.svg)](https://baderouaich.github.io/tgbotxx)
[![Language](https://img.shields.io/badge/C++-20-blue.svg?style=flat&logo=c%2B%2B)](https://img.shields.io/badge/C++-20-blue.svg?style=flat&logo=c%2B%2B)

# tgbotxx
Telegram Bot C++ Library

Compatible with Telegram [Bot API 6.9 (September 22, 2023)](https://core.telegram.org/bots/api-changelog)

### CI Status
| Operating system | Build status |
| ---------------- |---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Ubuntu (x64) | [![Ubuntu](https://img.shields.io/github/actions/workflow/status/baderouaich/tgbotxx/build-ubuntu.yml?branch=main)](https://github.com/baderouaich/tgbotxx/actions/workflows/build-ubuntu.yml) |
| Windows (x64) | [![Windows](https://img.shields.io/github/actions/workflow/status/baderouaich/tgbotxx/build-windows.yml?branch=main)](https://github.com/baderouaich/tgbotxx/actions/workflows/build-windows.yml) |
| macOS | [![macOS](https://img.shields.io/github/actions/workflow/status/baderouaich/tgbotxx/build-macos.yml?branch=main)](https://github.com/baderouaich/tgbotxx/actions/workflows/build-macos.yml) |

### Examples
see [examples](examples/) for more:

| Example | Description | Preview |
|:---------------------------------------------------------|:-------------------------------------------------------------------------------------------------------------------------------------------------------|:-------------------------------------------------------------------------------------------:|
| [WeatherBot](examples/WeatherBot) | This example shows how to program a Telegram Bot that displays the weather information of a city using the [weather api](https://www.weatherapi.com/). | preview |
| [EarthquakeBot](examples/EarthquakeBot) | This example shows how to program a Telegram Bot that will alert you if there is a recent earthquake somewhere in the world. | preview |
| [QrCodeBot](examples/QrCodeBot) | This example shows how to program a Telegram Bot that can generate QrCode images from text and extract text from QrCode Images. | preview |
| [UrlShortenerBot](examples/UrlShortenerBot) | This example shows how to program Telegram Bot for shortening URLs. | preview |
| [Inline Buttons](examples/Buttons/InlineKeyboardButton) | This example shows how to program a basic Telegram Bot that uses inline keyboard buttons to interact with users. | preview |
| [Keyboard Buttons](examples/Buttons/ReplyKeyboardMarkup) | This example shows how to program a basic Telegram Bot that uses keyboard buttons to interact with users. | preview |
| [PaidSubscriptionBot](examples/PaidSubscriptionBot) | This example shows how to program a basic Telegram Bot that offers it's services for a paid subscription. | preview |

### Usage
This library is using Inheritance-Based Extensibility technique providing a Bot class which you can inherit from
and optionally override callback events depending on your Bot needs.

This also allows you to instantiate multiple bots in the same program. Just make sure each Bot is running on a separate thread.

Usage example: Creating a new Bot called `MyBot` that overrides all callbacks:
```cpp
#include
#include
using namespace tgbotxx;

class MyBot : public Bot {
public:
MyBot() : Bot("BOT_TOKEN_FROM_BOT_FATHER") {}

private:
/// Called before Bot starts receiving updates (triggered by Bot::start())
/// Use this callback to initialize your code, set commands..
void onStart() override {
// Drop awaiting updates (when Bot is not running, updates will remain 24 hours
// in Telegram server before they get deleted or retrieved by BOT)
api()->deleteWebhook(true);

// Register bot commands ...
Ptr greet(new BotCommand());
greet->command = "greet";
greet->description = "This command will greet you";
Ptr stop(new BotCommand());
stop->command = "stop";
stop->description = "Stop the bot";
api()->setMyCommands({greet, stop}); // The above commands will be shown in the bot chat menu (bottom left)

std::cout << "Bot " << api()->getMe()->username << " Started\n";
}

/// Called when Bot is about to be stopped (triggered by Bot::stop())
void onStop() override {
/// Cleanup your code in this callback (close handles, backup data...)
std::cout << "Bot " << api()->getMe()->username << " Stopped\n";
}

/// Called when a new message is received of any kind - text, photo, sticker, etc.
void onAnyMessage(const Ptr& message) override {
api()->sendMessage(message->chat->id, "Hi " + message->from->firstName + "!, got your message!");
}

/// Called when a new command is received (messages with leading '/' char).
void onCommand(const Ptr& message) override {
if(message->text == "/stop") {
api()->sendMessage(message->chat->id, "Bot stopping...");
Bot::stop();
return;
}
}

/// Called when long polling fails
void onLongPollError(const std::string& reason) override {
std::cerr << "Long polling error: " << reason << std::endl;
}

// Other callbacks (optional overload)
/// Called when a non-command message is received of any kind - text, photo, sticker, etc.
void onNonCommandMessage(const Ptr &message) override {}
/// Called when an unknown command is received (messages with leading '/' char).
/// Known commands are set with Bot::setCommands()
void onUnknownCommand(const Ptr &message) override {}
/// Called when a new version of a message that is known to the bot and was edited
void onEditedMessage(const Ptr& editedMessage) override {}
/// Called when a new incoming inline query is received
void onInlineQuery(const Ptr& inlineQuery) override {}
/// Called when the result of an inline query that was chosen by a user and sent to their chat partner.
void onChosenInlineResult(const Ptr& chosenInlineResult) override {}
/// Called when a new incoming callback query is received
void onCallbackQuery(const Ptr& callbackQuery) override {}
/// Called when a new incoming shipping query is received.
void onShippingQuery(const Ptr& shippingQuery) override {}
/// Called when a new incoming pre-checkout query is received. Contains full information about checkout
void onPreCheckoutQuery(const Ptr& preCheckoutQuery) override {}
/// Called when a new poll state is received.
void onPoll(const Ptr& poll) override {}
/// Called when a user changed their answer in a non-anonymous poll.
void onPollAnswer(const Ptr& pollAnswer) override {}
/// Called when the bot's chat member status was updated in a chat.
void onMyChatMember(const Ptr& myChatMemberUpdated) override {}
/// Called when a chat member's status was updated in a chat.
void onChatMember(const Ptr& chatMemberUpdated) override {}
/// Called when a A request to join the chat has been sent.
void onChatJoinRequest(const Ptr& chatJoinRequest) override {}
};

int main() {
MyBot bot;
bot.start();
return 0;
}
```
### Usage (4 approaches)
#### 1. `FetchContent` *(recommended)*

Simply use CMake's `FetchContent` in your project's `CMakeLists.txt` as below:
```cmake
cmake_minimum_required(VERSION 3.10)
project(my_bot)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(FetchContent)
FetchContent_Declare(tgbotxx
GIT_REPOSITORY "https://github.com/baderouaich/tgbotxx"
GIT_TAG main
)
FetchContent_MakeAvailable(tgbotxx)

add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} PUBLIC tgbotxx)
```

#### 2. `PkgConfig`: clone and install the library locally, then use PkgConfig:

example

```shell
git clone https://github.com/baderouaich/tgbotxx
cd tgbotxx
cmake .. -DCMAKE_BUILD_TYPE=Release
sudo make install
# On Windows run `make install` as administrator
```

```cmake
cmake_minimum_required(VERSION 3.10)
project(my_bot)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(PkgConfig REQUIRED)
pkg_check_modules(tgbotxx REQUIRED tgbotxx)

if(NOT tgbotxx_FOUND)
message(FATAL_ERROR "Did you install tgbotxx locally?")
endif()

add_executable(${PROJECT_NAME} main.cpp)
target_link_directories(${PROJECT_NAME} PUBLIC ${tgbotxx_LIBRARY_DIRS})
target_include_directories(${PROJECT_NAME} PUBLIC ${tgbotxx_INCLUDE_DIRS})
target_compile_options(${PROJECT_NAME} PUBLIC ${tgbotxx_CFLAGS_OTHER})
target_link_libraries(${PROJECT_NAME} PUBLIC ${tgbotxx_LIBRARIES})
```

#### 3. `find_package`: clone and install the library locally, then use find_package(tgbotxx REQUIRED):

example

```cmake
cmake_minimum_required(VERSION 3.10)
project(my_bot)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(tgbotxx REQUIRED)

if(NOT tgbotxx_FOUND)
message(FATAL_ERROR "Did you install tgbotxx locally?")
endif()

add_executable(${PROJECT_NAME} main.cpp)
target_link_directories(${PROJECT_NAME} PUBLIC ${tgbotxx_LIBRARY_DIRS})
target_include_directories(${PROJECT_NAME} PUBLIC ${tgbotxx_INCLUDE_DIRS})
target_compile_options(${PROJECT_NAME} PUBLIC ${tgbotxx_CFLAGS_OTHER})
target_link_libraries(${PROJECT_NAME} PUBLIC ${tgbotxx_LIBRARIES})
```

#### 4. `Submodule`: Use tgbotxx as a project submodule (without installation)

example

You can also use this library as a submodule in your bot project without the need of installing it in your system.
Use git clone or git submodule add the library:

```shell
git submodule add https://github.com/baderouaich/tgbotxx ./lib/tgbotxx
```
or
```shell
git clone https://github.com/baderouaich/tgbotxx ./lib/tgbotxx
```

Then add `add_subdirectory(lib/tgbotxx)` in your `CMakeLists.txt`.
```cmake
cmake_minimum_required(VERSION 3.10)
project(my_bot)

add_subdirectory(lib/tgbotxx) # <-- clone tgbotxx in your project's lib/ directory

add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} PUBLIC tgbotxx) # <-- link with tgbotxx
```

### Ref:
[Telegram Api Documentation](https://core.telegram.org/bots/api)

[Telegram Api Schema](https://core.telegram.org/schema) [Json Schema](https://core.telegram.org/schema/json)