Dart Requires Chilkat v11.0.0+
Dart
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.
Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
// Assumes Chilkat is unlocked. See Global Unlock Sample.
final http = CkHttp();
http.requireSslCertVerify = true;
// Load the key for this One API integration from your application configuration.
http.setRequestHeader('Authorization', 'ONE_API_KEY');
http.setRequestHeader('Accept', 'application/json');
final recipient = 'PHONE_NUMBER';
final pdf = CkBinData();
try {
pdf.loadFile('invoice.pdf');
} on ChilkatException {
print('Could not read invoice.pdf.');
return;
}
// Restrict this customer's invoice to that customer's number.
http.setUrlVar('recipient', recipient);
final upload = CkHttpResponse();
var uploaded = true;
try {
http.httpBd('POST', 'https://platform.clickatell.com/v1/media?fileName=invoice.pdf&to={\$recipient}', pdf, 'application/pdf', upload);
} on ChilkatException {
uploaded = false;
}
if ((!uploaded) || (upload.statusCode != 201)) {
print('PDF upload failed. HTTP status ${upload.statusCode}');
return;
}
final media = CkJsonObject();
var decoded = true;
try {
media.load(upload.bodyStr);
} on ChilkatException {
decoded = false;
}
if ((!decoded) || (!media.boolOf('accepted'))) {
print('Upload was not accepted: ${media.stringOf('error.description')}');
return;
}
final fileId = media.stringOf('fileId');
if ((media.jsonTypeOf('fileId') != 1) || (fileId == '')) {
print('Upload returned no file ID.');
return;
}
final body = CkJsonObject();
body.updateString('messages[0].channel', 'whatsapp');
body.updateString('messages[0].to', recipient);
body.updateString('messages[0].media.fileId', fileId);
body.updateString('messages[0].media.contentType', 'application/pdf');
body.updateString('messages[0].media.fileName', 'invoice.pdf');
final resp = CkHttpResponse();
var sent = true;
try {
http.httpJson('POST', 'https://platform.clickatell.com/v1/message', body, 'application/json', resp);
} on ChilkatException {
sent = false;
}
if (resp.statusCode == 0) {
print('No HTTP response. Check the message report before resending.');
return;
}
final reply = CkJsonObject();
var parsed = true;
try {
reply.load(resp.bodyStr);
} on ChilkatException {
parsed = false;
}
if ((!sent) || (resp.statusCode != 202)) {
print('Send returned HTTP ${resp.statusCode}');
if (reply.hasMember('error.description')) {
print(reply.stringOf('error.description'));
}
return;
}
if (reply.hasMember('error.code')) {
if (!reply.isNullOf('error.code')) {
print('Submission error ${reply.stringOf('error.code')} ${reply.stringOf('error.description')}');
return;
}
}
// HTTP acceptance and the recipient result are separate checks.
if ((!parsed) || (reply.jsonTypeOf('messages[0].accepted') != 5)) {
print('Missing recipient result. Check the message report.');
return;
}
if (!reply.boolOf('messages[0].accepted')) {
print('Message rejected: ${reply.stringOf('messages[0].error.code')} ${reply.stringOf('messages[0].error.description')}');
return;
}
if (reply.jsonTypeOf('messages[0].apiMessageId') != 1) {
print('Accepted without a message ID. Check the message report.');
return;
}
print('Accepted. Message ID: ${reply.stringOf('messages[0].apiMessageId')}');
}