Sample code for 30+ languages & platforms
Unicode C++ Requires Chilkat v11.0.0+

Clickatell One API: Upload and send a WhatsApp PDF

See more Clickatell Examples

Send a local invoice.pdf during the customer’s open WhatsApp service window. Upload the binary PDF for one recipient, then reference the returned fileId in a separate message to the same recipient. The upload and message submission have separate acceptance results.

Clickatell API reference

Chilkat Unicode C++ Downloads

Unicode C++
#include <CkHttpW.h>
#include <CkBinDataW.h>
#include <CkHttpResponseW.h>
#include <CkJsonObjectW.h>

void ChilkatSample(void)
    {
    //  Assumes Chilkat is unlocked. See Global Unlock Sample.
    CkHttpW http;
    http.put_RequireSslCertVerify(true);
    //  Load the key for this One API integration from your application configuration.
    http.SetRequestHeader(L"Authorization",L"ONE_API_KEY");
    http.SetRequestHeader(L"Accept",L"application/json");

    const wchar_t *recipient = L"PHONE_NUMBER";
    CkBinDataW pdf;
    bool loaded = pdf.LoadFile(L"invoice.pdf");
    if (loaded != true) {
        wprintf(L"Could not read invoice.pdf.\n");
        return;
    }

    //  Restrict this customer's invoice to that customer's number.
    http.SetUrlVar(L"recipient",recipient);
    CkHttpResponseW upload;
    bool uploaded = http.HttpBd(L"POST",L"https://platform.clickatell.com/v1/media?fileName=invoice.pdf&to={$recipient}",pdf,L"application/pdf",upload);
    if ((uploaded != true) || (upload.get_StatusCode() != 201)) {
        wprintf(L"PDF upload failed. HTTP status %d\n",upload.get_StatusCode());
        return;
    }

    CkJsonObjectW media;
    bool decoded = media.Load(upload.bodyStr());
    if ((decoded != true) || (media.BoolOf(L"accepted") != true)) {
        wprintf(L"Upload was not accepted: %s\n",media.stringOf(L"error.description"));
        return;
    }

    const wchar_t *fileId = media.stringOf(L"fileId");
    if ((media.JsonTypeOf(L"fileId") != 1) || (fileId == L"")) {
        wprintf(L"Upload returned no file ID.\n");
        return;
    }

    CkJsonObjectW body;
    body.UpdateString(L"messages[0].channel",L"whatsapp");
    body.UpdateString(L"messages[0].to",recipient);
    body.UpdateString(L"messages[0].media.fileId",fileId);
    body.UpdateString(L"messages[0].media.contentType",L"application/pdf");
    body.UpdateString(L"messages[0].media.fileName",L"invoice.pdf");

    CkHttpResponseW resp;
    bool sent = http.HttpJson(L"POST",L"https://platform.clickatell.com/v1/message",body,L"application/json",resp);
    if (resp.get_StatusCode() == 0) {
        wprintf(L"No HTTP response. Check the message report before resending.\n");
        return;
    }

    CkJsonObjectW reply;
    bool parsed = reply.Load(resp.bodyStr());
    if ((sent != true) || (resp.get_StatusCode() != 202)) {
        wprintf(L"Send returned HTTP %d\n",resp.get_StatusCode());
        if (reply.HasMember(L"error.description") == true) {
            wprintf(L"%s\n",reply.stringOf(L"error.description"));
        }

        return;
    }

    if (reply.HasMember(L"error.code") == true) {
        if (reply.IsNullOf(L"error.code") != true) {
            wprintf(L"Submission error %s  %s\n",reply.stringOf(L"error.code"),reply.stringOf(L"error.description"));
            return;
        }

    }

    //  HTTP acceptance and the recipient result are separate checks.
    if ((parsed != true) || (reply.JsonTypeOf(L"messages[0].accepted") != 5)) {
        wprintf(L"Missing recipient result. Check the message report.\n");
        return;
    }

    if (reply.BoolOf(L"messages[0].accepted") != true) {
        wprintf(L"Message rejected: %s  %s\n",reply.stringOf(L"messages[0].error.code"),reply.stringOf(L"messages[0].error.description"));
        return;
    }

    if (reply.JsonTypeOf(L"messages[0].apiMessageId") != 1) {
        wprintf(L"Accepted without a message ID. Check the message report.\n");
        return;
    }

    wprintf(L"Accepted. Message ID: %s\n",reply.stringOf(L"messages[0].apiMessageId"));
    }