Modersi / TelegramBotAPI

Telegram Bot API on C++ and Qt
MIT License
35 stars 21 forks source link
bot telegram telegram-api telegram-api-cxx telegram-bot telegram-bot-api telegram-bot-api-cxx telegram-bot-cxx telegram-cxx

Logo

QTelegramBotAPI

QTelegramBotAPI is a library for developing bots for the Telegram messenger.

Description

Bot in Telegram is something like a built-in program that users can interact with by buttons, commands, simple text messages, etc... You can create any bot you want! It can be a simple weather bot or a much more complicated Russian-Polish translator bot.

Getting started

This library was made following the official Telegram Bot API manual, so you can use it as documentation for all bot methods and all data structures that the library provides. Also, every header file contains a detailed self-description, so you don't always need to look in the official documentation.

You can find well-documented examples of the bot's abilities in the Examples directory. Further, we will talk about the most important classes in QTelegramBotAPI that are used to startup and use your bot.

Telegram::BotSettings

Telegram::BotSettings is a class that represents your bot settings and contains all required data to launch your bot. Used to construct Telegram::Bot, set up encryption, API key, host address, port, and more.

BotSettings can be constructed in 2 ways:

Telegram::Bot

Telegram::Bot is a class that represents your bot in Telegram. To create an instance of Telegram::Bot you have to pass Telegram::BotSettings object as an argument

auto bot_settings = Telegram::BotSettings::makeFromFile();
Telegram::Bot telegram_bot(bot_settings);

It contains all available API methods such as sendMessage(), sendPhoto(), etc..

Each method is asynchronous and uses std::future as a return value to provide asynchronous mechanic.

Methods with more than 4 optional arguments use designated initializers. So, for example, if you want to send a message with a reply keyboard you can write

telegram_bot.sendMessage({.chat_id = some_id, .text = some_text, .reply_markup = some_markup}); 

instead of

telegram_bot.sendMessage(some_id, some_text, std::nullopt, std::nullopt, std::nullopt, std::nullopt, std::nullopt, std::nullopt, some_markup); 


Also, it contains all Qt signals related to updates handling. So you can easily connect the needed signal to your function or slot. For example, there is Telegram::Bot::messageReceived(qint32 update_id, Message message) signal that is emitted every time when your bot receives a text message. You can find all available signals and their description in Bot.h file

QObject::connect(&telegram_bot, &Telegram::Bot::messageReceived, [&](qint32 update_id, Telegram::Message message) { 
  telegram_bot.sendMessage(message.chat->id, message.text.value_or("")); 
});

See IncomingMessagesHandling example for the details

Dependecies

QTelegramBotAPI is written using Qt 6.3.0 and OpenSSL 1.1.1j, so if you want to add the QTelegramBotAPI library to your project firstly you should link these libraries to your project.

find_package(Qt6 REQUIRED Core Network)             
target_link_libraries(${PROJECT_NAME} PRIVATE Qt6::Core PRIVATE Qt6::Network)

find_package(OpenSSL REQUIRED)          
target_link_libraries(${PROJECT_NAME} PRIVATE OpenSSL::SSL)

Linking to your project

You can easily link QTelegramBotAPI to your project with CMake by including header files located in TelegramBotAPI/include and linking with TelegramBotAPI.lib

target_include_directories(${PROJECT_NAME} PRIVATE install/path/TelegramBotAPI/include)
target_link_libraries(${PROJECT_NAME} PRIVATE install/path/TelegramBotAPI/TelegramBotAPI.lib)