From 54da7e13b97cb27380f459d0801f3b1be51c5970 Mon Sep 17 00:00:00 2001
From: "Aleksanov, Yuriy" <aleksanovyd@loodsen.ru>
Date: Thu, 6 Mar 2025 05:03:47 +0300
Subject: [PATCH] msg1

---
 my_msg.cpp | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)

diff --git a/my_msg.cpp b/my_msg.cpp
index e69de29..41bd390 100644
--- a/my_msg.cpp
+++ b/my_msg.cpp
@@ -0,0 +1,63 @@
+#include <iostream>
+#include <string>
+#include <curl/curl.h>
+#include <cctype>
+
+using namespace std;
+
+string telegram_id;
+string bot_token;
+
+int my_msg(const string& msg) {
+    cout << msg << endl;
+
+    if (bot_token.empty() || telegram_id.empty()) {
+        return 0;
+    }
+
+    CURL* curl = curl_easy_init();
+    if (!curl) return 6;
+
+    string escaped_msg = escape_json(msg);
+    
+    string chat_id_field;
+    if (is_numeric(telegram_id)) {
+        chat_id_field = "\"chat_id\": " + telegram_id;
+    } else {
+        chat_id_field = "\"chat_id\": \"" + telegram_id + "\"";
+    }
+    
+    string json_data = "{" + chat_id_field + ", \"text\": \"" + escaped_msg + "\"}";
+
+    struct curl_slist* headers = nullptr;
+    headers = curl_slist_append(headers, "Content-Type: application/json");
+
+    curl_easy_setopt(curl, CURLOPT_URL, ("https://api.telegram.org/bot" + bot_token + "/sendMessage").c_str());
+    curl_easy_setopt(curl, CURLOPT_POST, 1L);
+    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, json_data.c_str());
+    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
+    curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl/7.68.0");
+    
+    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, [](void*, size_t size, size_t nmemb, void*) -> size_t {
+        return size * nmemb;
+    });
+
+    CURLcode res = curl_easy_perform(curl);
+    long http_code = 0;
+    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
+
+    curl_slist_free_all(headers);
+    curl_easy_cleanup(curl);
+
+    if (res != CURLE_OK) return 5;
+    
+    switch (http_code) {
+        case 200: return 0;
+        case 401: return 1;
+        case 400: return 2;
+        case 404: return 3;
+        default:  
+            if (http_code >= 500) return 4;
+            return 4; 
+    }
+}
\ No newline at end of file