Install tgbot-cpp on MacOS
masterInstall dependencies using Homebrew:
brew install gcc cmake boost openssl zlib curlAfter installing dependencies, follow the Linux compilation instructions (clone, cmake, make, make install) to build the library.
repository·master·Indexed 22 days ago
https://github.com/reo7sp/tgbot-cppA 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.
Install dependencies using Homebrew:
brew install gcc cmake boost openssl zlib curlAfter installing dependencies, follow the Linux compilation instructions (clone, cmake, make, make install) to build the library.
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-devOptional dependencies for testing and documentation:
sudo apt install libboost-test-dev doxygenClone the repository and build using CMake:
git clone https://github.com/reo7sp/tgbot-cpp
cd tgbot-cpp
cmake .
make -j4
sudo make installYou can use the pre-built Docker image reo7sp/tgbot-cpp as your base image in your Dockerfile.
You can use the provided samples/echobot/CMakeLists.txt as a template. If you are on Windows using vcpkg, remember to:
/usr/local/include./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.
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 -lpthreadEnsure 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.batIntegrate vcpkg with Visual Studio (requires admin):
> .\vcpkg\vcpkg integrate installFor x64 Windows:
> .\vcpkg\vcpkg install tgbot-cpp:x64-windowsFor x86 Windows:
> .\vcpkg\vcpkg install tgbot-cppYou 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;
}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