Ответы пользователя по тегу Telegram
  • Отправка файла через телеграм бота с помощью libcurl с определенным параметром?

    @knizymozf Автор вопроса
    Учусь программированию...
    Я немного порылся в интернете и нашел ответ.
    #define CURL_STATICLIB
    
    #include <stdio.h>
    #include <string>
    #include <curl.h>
    
    #ifdef _DEBUG
    #pragma comment (lib, "libcurl_a_debug.lib")
    #else
    #pragma comment (lib, "libcurl_a.lib")
    #endif
    
    #pragma comment (lib, "Normaliz.lib")
    #pragma comment (lib, "Ws2_32.lib")
    #pragma comment (lib, "Wldap32.lib")
    #pragma comment (lib, "Crypt32.lib")
    #pragma comment (lib, "advapi32.lib")
    
    int main(int argc, char* argv[])
    {
        CURL* curl;
        CURLcode res;
    
        curl_mime* form = NULL;
        curl_mimepart* field = NULL;
        struct curl_slist* headerlist = NULL;
        static const char buf[] = "Expect:";
    
        curl_global_init(CURL_GLOBAL_ALL);
    
        curl = curl_easy_init();
        if (curl) {
            /* Create the form */
            form = curl_mime_init(curl);
    
            /* Fill in the file upload field */
            field = curl_mime_addpart(form);
            curl_mime_name(field, "document");
            curl_mime_filedata(field, "FILE PATH"); // FILE - PATH
    
            /* initialize custom header list (stating that Expect: 100-continue is not
               wanted */
            headerlist = curl_slist_append(headerlist, buf);
            /* what URL that receives this POST */
            curl_easy_setopt(curl, CURLOPT_URL, "https://api.telegram.org/botTOKEN/sendDocument?chat_id=CHAT_ID");
            if ((argc == 2) && (!strcmp(argv[1], "noexpectheader")))
                /* only disable 100-continue header if explicitly requested */
                curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
            curl_easy_setopt(curl, CURLOPT_MIMEPOST, form);
    
            /* Perform the request, res will get the return code */
            res = curl_easy_perform(curl);
            /* Check for errors */
            if (res != CURLE_OK)
                fprintf(stderr, "curl_easy_perform() failed: %s\n",
                    curl_easy_strerror(res));
    
            /* always cleanup */
            curl_easy_cleanup(curl);
    
            /* then cleanup the form */
            curl_mime_free(form);
            /* free slist */
            curl_slist_free_all(headerlist);
        }
        return 0;
    }
    Ответ написан
    Комментировать