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

Clickatell One API: Send personalized SMS reminders in a batch

See more Clickatell Examples

Send two appointments in one request with a different text and application ID for each. Use two distinct recipient numbers in international digits. For a US two-way integration, add its provisioned from number to each message.

Read each recipient result even when HTTP 207 reports a mixed outcome. Save the accepted provider ID against the appointment. A missing result calls for checking the message report before retrying. A client message ID helps correlate records and does not guarantee deduplication.

Clickatell API reference

Chilkat Dart Downloads

Dart
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 firstPhone = 'FIRST_PHONE_NUMBER';
  final secondPhone = 'SECOND_PHONE_NUMBER';
  final body = CkJsonObject();
  body.updateString('messages[0].channel', 'sms');
  body.updateString('messages[0].to', firstPhone);
  body.updateString('messages[0].content', 'Your appointment with Lee is at 10:00.');
  body.updateString('messages[0].clientMessageId', 'appt-1042');
  body.updateString('messages[1].channel', 'sms');
  body.updateString('messages[1].to', secondPhone);
  body.updateString('messages[1].content', 'Your appointment with Sam is at 14:30.');
  body.updateString('messages[1].clientMessageId', 'appt-1043');

  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();
  reply.load(resp.bodyStr);
  if ((!sent) || ((resp.statusCode != 202) && (resp.statusCode != 207))) {
    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;
    }
  }

  // Look up each submitted appointment in the reply using its unique recipient.
  // This works even if the result order changes or clientMessageId is absent.
  final results = CkJsonObject();
  var i = 0;
  while (i < 2) {
    body.i = i;
    final appointment = body.stringOf('messages[i].clientMessageId');
    final phone = body.stringOf('messages[i].to');
    final result = CkJsonObject();
    var found = true;
    try {
      reply.findRecord2('messages', 'to', phone, true, result);
    } on ChilkatException {
      found = false;
    }
    results.i = i;
    results.updateString('appointments[i].clientMessageId', appointment);
    if ((!found) || (result.jsonTypeOf('accepted') != 5)) {
      results.updateString('appointments[i].outcome', 'unknown');
      print('$appointment has no confirmed result.');
    } else {
      if (result.boolOf('accepted')) {
        results.updateString('appointments[i].outcome', 'accepted');
        if (result.jsonTypeOf('apiMessageId') == 1) {
          results.updateString('appointments[i].apiMessageId', result.stringOf('apiMessageId'));
        }

        print('$appointment accepted.');
      } else {
        results.updateString('appointments[i].outcome', 'rejected');
        results.updateString('appointments[i].reason', result.stringOf('error.description'));
        print('$appointment rejected: ${result.stringOf('error.description')}');
      }
    }

    i++;
  }

  // This demo writes a snapshot. In an application, update the two appointment records.
  try {
    results.writeFile('appointment-send-results.json');
  } on ChilkatException {
    print('Could not save the results. Reconcile the submitted messages before retrying.');
    return;
  }

  print('Saved appointment-send-results.json');
}