Dart Requires Chilkat v11.0.0+
Dart
Clickatell One API: Send an approved WhatsApp shipment template
See more Clickatell Examples
Send a business-initiated shipment update, including outside an open customer service window. First approve a template on the WhatsApp integration with two positional body placeholders for the order number and delivery date. Supply its exact name and approved language. The keys 1 and 2 refer to those placeholders in order.
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');
// Example approved wording: Order {{1}} is due for delivery on {{2}}.
final body = CkJsonObject();
body.updateString('messages[0].channel', 'whatsapp');
body.updateString('messages[0].to', 'PHONE_NUMBER');
body.updateString('messages[0].template.templateName', 'shipment_update');
body.updateString('messages[0].template.language', 'en_US');
body.updateString('messages[0].template.body.parameters.1', 'ORD-1042');
body.updateString('messages[0].template.body.parameters.2', 'March 19');
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')}');
}