Код: Выделить всё
sudo apt-get install libboost-all-dev libjson-c-dev
Код: Выделить всё
git clone https://github.com/libcpr/cpr.git
cd cpr
mkdir build
cd build
cmake ..
make
sudo make install
Код: Выделить всё
#include <cpr/cpr.h>
#include <iostream>
#include <string>
#include <json/json.h>
const std::string BOT_TOKEN = "YOUR_TELEGRAM_BOT_TOKEN"; // Замените на ваш токен
const std::string BASE_URL = "https://api.telegram.org/bot" + BOT_TOKEN;
void sendMessage(const std::string& chat_id, const std::string& text) {
std::string url = BASE_URL + "/sendMessage";
// Создаем JSON-объект
Json::Value jsonData;
jsonData["chat_id"] = chat_id;
jsonData["text"] = text;
// Отправляем POST-запрос
auto response = cpr::Post(cpr::Url{url},
cpr::Body{jsonData.toStyledString()},
cpr::Header{{"Content-Type", "application/json"}});
std::cout << "Response: " << response.text << std::endl; // Выводим ответ от Telegram
}
int main() {
std::string chat_id = "CHAT_ID"; // Замените на ID вашего чата
std::string text = "Hello, World!";
sendMessage(chat_id, text);
return 0;
}
Код: Выделить всё
g++ -std=c++11 -o telegram_bot telegram_bot.cpp -lcpr -lboost_system -lboost_thread -lpthread -ljson-c -I/usr/include/json
Код: Выделить всё
./telegram_bot
Не забудьте заменить YOUR_TELEGRAM_BOT_TOKEN и CHAT_ID на фактический токен вашего бота и ID вашего чата соответственно.