Kontent qismiga oʻtish

Munozara:Visual Studio Code

Sahifa kontenti boshqa tillarda dastaklanmaydi.
Vikipediya, erkin ensiklopediya


#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <curl/curl.h>

#include <json-c/json.h>

#include <unistd.h>  // sleep() funksiyasi uchun

#define BOT_TOKEN "YOUR_BOT_API_TOKEN"

#define BASE_URL "https://api.telegram.org/bot" BOT_TOKEN "/"

#define MAX_MESSAGE_LEN 1024

// Telegram API so'rovlarini yuborish va javobni olish uchun funksiya

size_t write_callback(void *ptr, size_t size, size_t nmemb, char *data) {

    // Har safar yangi javob kelganida, oldingi javobni bo'shatib, yangisini qo'shamiz

    strcat(data, ptr);

    return size * nmemb;

}

void send_message(char *chat_id, char *text) {

    CURL *curl;

    CURLcode res;

    char url[256];

    char data[MAX_MESSAGE_LEN] = "{\"chat_id\":\"%s\", \"text\":\"%s\"}";

    snprintf(url, sizeof(url), BASE_URL "sendMessage");

    snprintf(data, sizeof(data), data, chat_id, text);

    curl_global_init(CURL_GLOBAL_DEFAULT);

    curl = curl_easy_init();

    if(curl) {

        struct curl_slist *headers = NULL;

        headers = curl_slist_append(headers, "Content-Type: application/json");

        curl_easy_setopt(curl, CURLOPT_URL, url);

        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data);

        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);

        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);

        curl_easy_setopt(curl, CURLOPT_WRITEDATA, data);

        res = curl_easy_perform(curl);

        if(res != CURLE_OK)

            fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));

        curl_easy_cleanup(curl);

    }

    curl_global_cleanup();

}

// Foydalanuvchi tomonidan yuborilgan xabarni olish

void get_updates() {

    CURL *curl;

    CURLcode res;

    char url[256];

    char data[MAX_MESSAGE_LEN] = "";  // Xabarlarni saqlash uchun o'zgaruvchini bo'sh qilib boshlaymiz

    snprintf(url, sizeof(url), BASE_URL "getUpdates");

    curl_global_init(CURL_GLOBAL_DEFAULT);

    curl = curl_easy_init();

    if(curl) {

        curl_easy_setopt(curl, CURLOPT_URL, url);

        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);

        curl_easy_setopt(curl, CURLOPT_WRITEDATA, data);

        res = curl_easy_perform(curl);

        if(res != CURLE_OK)

            fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));

        // JSON javobni tahlil qilish

        printf("Response: %s\n", data);

        // JSON ma'lumotlarini tahlil qilish (oddiy misol)

        struct json_object *parsed_json;

        struct json_object *result;

        struct json_object *message;

        struct json_object *chat_id;

        struct json_object *text;

        parsed_json = json_tokener_parse(data);

        json_object_object_get_ex(parsed_json, "result", &result);

        // "result" massividagi har bir elementni tekshirish

        for (size_t i = 0; i < json_object_array_length(result); i++) {

            message = json_object_array_get_idx(result, i);

            json_object_object_get_ex(message, "chat", &chat_id);

            json_object_object_get_ex(message, "text", &text);

            printf("Message from chat_id %s: %s\n", json_object_get_string(chat_id), json_object_get_string(text));

            // O'yinlar uchun oddiy javob (misol)

            if (strstr(json_object_get_string(text), "hello") != NULL) {

                send_message(json_object_get_string(chat_id), "Hello! Welcome to the game!");

            }

        }

        json_object_put(parsed_json);

        curl_easy_cleanup(curl);

    }

    curl_global_cleanup();

}

int main() {

    printf("Bot ishga tushdi!\n");

    // Telegram botini boshlash

    while (1) {

        get_updates();  // Yangi xabarlarni olish

        sleep(1);       // Bir necha soniya kutish

    }

    return 0;

} 94.141.76.130 09:40, 2025-yil 20-yanvar (UTC)[javob berish]