Skip to main content

ab_ecosystem_cpaas 1.0.0 copy "ab_ecosystem_cpaas: ^1.0.0" to clipboard
ab_ecosystem_cpaas: ^1.0.0 copied to clipboard

Dart and Flutter SDK for the AB Ecosystem CPaaS platform: WhatsApp Business API channels, CRM contacts, bulk campaigns, message templates and a realtime live chat inbox.

example/main.dart

// Walks the audience and channel resources end to end.
//
// Run with:
//   dart run example/main.dart
//
// It reads its configuration from the environment so no credential is ever
// committed:
//   CPAAS_GATEWAY_URL, CPAAS_CLIENT_ID, CPAAS_TOKEN, CPAAS_ORIGIN
import 'dart:io';

import 'package:ab_ecosystem_cpaas/ab_ecosystem_cpaas.dart';

Future<void> main() async {
  final crm = CrmClient(
    ClientOptions.token(
      Platform.environment['CPAAS_TOKEN'] ?? '',
      gatewayUrl: Platform.environment['CPAAS_GATEWAY_URL'] ?? '',
      clientId: Platform.environment['CPAAS_CLIENT_ID'] ?? '',
      // Required off the web: the gateway rejects a call without it, and only
      // a browser sets the header for you.
      origin: Platform.environment['CPAAS_ORIGIN'] ?? '',
    ),
  );

  try {
    // One page of contacts, newest first.
    final page = await crm.contacts.list(
      const ListContactsQuery(rows: 10, status: ContactStatus.live),
    );
    stdout.writeln('${page.pagination.total} contacts');
    for (final contact in page.data) {
      stdout.writeln('  ${contact.id}  ${contact.name.firstName ?? '-'}');
    }

    // The same list as a lazy stream, which pages for you.
    final recent =
        crm.contacts.iterate(const ListContactsQuery(rows: 100)).take(25);
    await for (final contact in recent) {
      stdout.writeln('  ${contact.whatsapp?.identifier ?? 'no whatsapp'}');
    }

    // Ad-hoc conditions: everyone in Pune added in the last 30 days.
    final filtered = await crm.contacts.list(
      ListContactsQuery(
        filter: const ContactFilter(
          conditions: <ContactFilterEntry>[
            ContactFilterEntry(
              fieldId: 'city',
              fieldType: ContactFilterFieldType.text,
              condition: ContactFilterCondition.equalTo,
              value: 'Pune',
            ),
            ContactFilterEntry(
              operator: ContactFilterOperator.and,
              fieldId: 'SYSTEM_CREATED_AT',
              fieldType: ContactFilterFieldType.date,
              condition: ContactFilterCondition.within,
              direction: ContactFilterDirection.last,
              timeUnit: ContactFilterTimeUnit.days,
              value: 30,
            ),
          ],
        ),
      ),
    );
    stdout.writeln('${filtered.pagination.total} matched the filter');

    // Create a contact, then unassign whoever it landed on.
    final created = await crm.contacts.create(
      const CreateContactInput(
        whatsapp: '+91 98765 43210',
        name: ContactName(firstName: 'Asha', lastName: 'Rao'),
      ),
    );
    await crm.contacts.update(
      created.id,
      const UpdateContactInput(agent: Patch<String>.clear()),
    );

    // Channels, and the templates on the first WhatsApp one.
    final channels = await crm.channels.list(
      const ListChannelsQuery(type: ChannelType.whatsapp, active: true),
    );

    for (final channel in channels.data) {
      stdout.writeln(
        '${channel.basicDetails.name}  '
        '${channel.wallet.isHidden ? 'wallet hidden' : channel.wallet.balance}',
      );

      final templates = await crm.templates.list(
        channel.id,
        const ListTemplatesQuery(status: TemplateStatus.approved),
      );
      stdout.writeln('  ${templates.pagination.total} approved templates');
    }
  } on CrmApiError catch (error) {
    // Branch on `code`, not on `message`, which is written for a person.
    stderr.writeln('${error.status} ${error.code}: ${error.message}');
    exitCode = 1;
  } on CrmConfigError catch (error) {
    stderr.writeln(error.message);
    exitCode = 2;
  } finally {
    crm.close();
  }
}
0
likes
160
points
420
downloads

Documentation

API reference

Publisher

verified publisherab-ecosystem.com

Weekly Downloads

Dart and Flutter SDK for the AB Ecosystem CPaaS platform: WhatsApp Business API channels, CRM contacts, bulk campaigns, message templates and a realtime live chat inbox.

Homepage

Topics

#whatsapp #messaging #crm #chat #sdk

License

MIT (license)

Dependencies

http, meta, socket_io_client

More

Packages that depend on ab_ecosystem_cpaas