V
V
Vladimir2022-02-19 15:30:13
API
Vladimir, 2022-02-19 15:30:13

Sending file via telegram bot using libcurl with specific parameter?

I know that you can use a link with TOKEN , CHATID and TEXT parameters to send any message through the telegram bot.
Here is an example:

void Message(string message) {
    CURL* curl = curl_easy_init();
    
    string URL = "https://api.telegram.org/bot"+TOKEN+"/sendMessage?chat_id="+CHAT_ID+"&text="+message;

    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_easy_setopt(curl, CURLOPT_URL, URL.c_str());

    curl_easy_perform(curl);
    curl_easy_cleanup(curl);
};

But how to send a file? I know this can be done, but how exactly - no :( You need to use the
link https ://api.telegram.org/botTOKEN/sendDocument?cha ... t?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir, 2022-02-19
@knizymozf

I rummaged a little on the Internet and found the answer.

#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;
}

S
shurshur, 2022-02-19
@shurshur

If by link, then you need to transfer the link in the URL: ...&document=link
If the file is local, then transfer the file to multipart/form-data, something like this: https://curl.se/libcurl/c/ postit2.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question