Dart Requires Chilkat v11.0.0+
Dart
List Twilio message history
See more Twilio Examples
List inbound and outbound messages, newest sent date first. Print the Message SID, direction, and status so you can distinguish incoming messages from outbound submissions and see their latest state.
Each response contains a messages array and a next_page_uri. Follow that URI until it is null. PageSize limits one response. Use credentials for the account whose history you want to read.
Chilkat Dart Downloads
import 'package:chilkat/chilkat.dart';
void main() {
var success = false;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
final http = CkHttp();
http.requireSslCertVerify = true;
http.basicAuth = true;
// Load the owning Account SID and an API key from application configuration.
final accountSid = 'YOUR_ACCOUNT_SID';
http.login = 'YOUR_API_KEY_SID';
http.password = 'YOUR_API_KEY_SECRET';
final url = CkStringBuilder();
url.append('https://api.twilio.com/2010-04-01/Accounts/');
url.append(accountSid);
url.append('/Messages.json');
url.append('?PageSize=50');
final resp = CkHttpResponse();
final reply = CkJsonObject();
var total = 0;
var more = true;
while (more) {
success = true;
try {
http.httpNoBody('GET', url.getAsString(), resp);
} on ChilkatException {
success = false;
}
if (resp.statusCode == 0) {
print(http.lastErrorText);
return;
}
var parsed = true;
try {
reply.load(resp.bodyStr);
} on ChilkatException {
parsed = false;
}
if ((!success) || (resp.statusCode != 200)) {
print('HTTP ${resp.statusCode}');
if ((parsed) && (reply.hasMember('code'))) {
print('Twilio error ${reply.intOf('code')}: ${reply.stringOf('message')}');
}
return;
}
if (!parsed) {
print('Expected a JSON response.');
return;
}
if (reply.jsonTypeOf('messages') != 4) {
print('Expected a messages array.');
return;
}
final count = reply.sizeOfArray('messages');
var i = 0;
while (i < count) {
reply.i = i;
print('${reply.stringOf('messages[i].sid')} ${reply.stringOf('messages[i].direction')} ${reply.stringOf('messages[i].status')}');
i++;
}
total += count;
// Twilio supplies a relative URI with the cursor and returns null on the last page.
if (reply.isNullOf('next_page_uri')) {
more = false;
} else {
if (reply.jsonTypeOf('next_page_uri') != 1) {
print('Expected a next-page URI or null.');
return;
}
url.setString('https://api.twilio.com');
url.append(reply.stringOf('next_page_uri'));
}
}
print('Messages found: $total');
}