C
C
WhatsApp - Send a Previously Uploaded Media Message
Demonstrates how to send a media message where the media content was previously uploaded.Chilkat C Downloads
#include <C_CkHttp.h>
#include <C_CkJsonObject.h>
#include <C_CkHttpResponse.h>
#include <C_CkStringBuilder.h>
void ChilkatSample(void)
{
BOOL success;
HCkHttp http;
HCkJsonObject json;
HCkHttpResponse resp;
HCkStringBuilder sbResponseBody;
HCkJsonObject jResp;
int respStatusCode;
const char *id;
int i;
int count_i;
success = FALSE;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http = CkHttp_Create();
// Implements the following CURL command:
// curl -X POST https://example.com/v1/messages \
// -H "Authorization: Bearer your-auth-token" \
// -d '
// {
// "recipient_type": "individual-or-group",
// "to": "whatsapp-id-or-group-id",
// "type": "image",
// "image": {
// "id": "your-media-id",
// "caption": "your-image-caption"
// }
// }'
// Use the following online tool to generate HTTP code from a CURL command
// Convert a cURL Command to HTTP Source Code
// Use this online tool to generate code from sample JSON:
// Generate Code to Create JSON
// The following JSON is sent in the request body.
// {
// "recipient_type": "individual-or-group",
// "to": "whatsapp-id-or-group-id",
// "type": "image",
// "image": {
// "id": "your-media-id",
// "caption": "your-image-caption"
// }
// }
json = CkJsonObject_Create();
CkJsonObject_UpdateString(json,"recipient_type","individual-or-group");
CkJsonObject_UpdateString(json,"to","whatsapp-id-or-group-id");
CkJsonObject_UpdateString(json,"type","image");
CkJsonObject_UpdateString(json,"image.id","your-media-id");
CkJsonObject_UpdateString(json,"image.caption","your-image-caption");
// Adds the "Authorization: Bearer your-auth-token" header.
CkHttp_putAuthToken(http,"your-auth-token");
resp = CkHttpResponse_Create();
success = CkHttp_HttpJson(http,"POST","https://example.com/v1/messages",json,"application/json",resp);
if (success == FALSE) {
printf("%s\n",CkHttp_lastErrorText(http));
CkHttp_Dispose(http);
CkJsonObject_Dispose(json);
CkHttpResponse_Dispose(resp);
return;
}
sbResponseBody = CkStringBuilder_Create();
CkHttpResponse_GetBodySb(resp,sbResponseBody);
jResp = CkJsonObject_Create();
CkJsonObject_LoadSb(jResp,sbResponseBody);
CkJsonObject_putEmitCompact(jResp,FALSE);
printf("Response Body:\n");
printf("%s\n",CkJsonObject_emit(jResp));
respStatusCode = CkHttpResponse_getStatusCode(resp);
printf("Response Status Code = %d\n",respStatusCode);
if (respStatusCode >= 400) {
printf("Response Header:\n");
printf("%s\n",CkHttpResponse_header(resp));
printf("Failed.\n");
CkHttp_Dispose(http);
CkJsonObject_Dispose(json);
CkHttpResponse_Dispose(resp);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(jResp);
return;
}
// Sample JSON response:
// (Sample code for parsing the JSON response is shown below)
// {
// "messages": [
// {
// "id": "gBEGkYiEB1VXAglK1ZEqA1YKPrU"
// }
// ]
// }
// Sample code for parsing the JSON response...
// Use the following online tool to generate parsing code from sample JSON:
// Generate Parsing Code from JSON
// Chilkat functions returning "const char *" return a pointer to temporary internal memory owned and managed by Chilkat.
// See this example explaining how this memory should be used: const char * functions.
i = 0;
count_i = CkJsonObject_SizeOfArray(jResp,"messages");
while (i < count_i) {
CkJsonObject_putI(jResp,i);
id = CkJsonObject_stringOf(jResp,"messages[i].id");
i = i + 1;
}
CkHttp_Dispose(http);
CkJsonObject_Dispose(json);
CkHttpResponse_Dispose(resp);
CkStringBuilder_Dispose(sbResponseBody);
CkJsonObject_Dispose(jResp);
}