-1

I am trying to get outbound sms logs against each individual phone number configured in twilio sub-account. I am using PHP programming language and looked into the api console: https://www.twilio.com/docs/api/rest/usage-records

But according to my exploration, usage-record api only returns total usage information against my twilio account. i.e. 500 outbound sms against all the three subscribed numbers in my account say N1, N2 and N3. What I want here is to retrieve the number of sms sent and received individually against each number N1, N2 and N3. Please help me in this regard.

Thanks

I have already did like this.

$sub_acc = $client->accounts->get($sub_account->sid);
$numbers = $sub_acc->incoming_phone_numbers;
$phone_sms_count = array();
foreach ($numbers as $number) {
    $sms_count = 0;
    foreach ($sub_acc->messages->getIterator(0, 50, array(
        "From" => $number->phone_number,
        "DateSent" => $last_six_months_date)) as $sms)
        {
            $sms_count++;
        }
    $phone_sms_count[$number->phone_number] = $sms_count;
}

But it takes too much time to calculate sms counts for each number. I have configured 5 numbers in single subaccount and I have 10 subaccounts this will break the limits. Is there any way to get sms counts for each phone number without iterating messages?

Smith
  • 11
  • 2

2 Answers2

1

I'm not sure why you received downvotes but to me the question is legit so I'll try to help.

You can use the Message API List Resource. Just filter the date range and from phone number. So to get records for last month (May 2016) just filter like:

// Set options
$from_number="XXXXXXXXXX"; 
$start_date="2016-05-01";
$end_date="2016-05-31";

// Build the option array
$get_iterator_options = array(
   'from' => $from_number,
   'DateSent' => $start_date,
   'DateSent' => $end_date
);

// Make the call with our options
foreach ($client->account->message->getIterator(
        0,
        50, 
        $get_iterator_options
    ) as $message
) {
    $msg_ids[] = $message->sid; // 
}

echo count($msg_ids);

Twilio provides a List Resource for Messages, Calls, Recordings, etc..

Keep in mind this is almost certainly slower than what you were trying to do originally especially if there are a lot of messages.

john
  • 1,330
  • 3
  • 20
  • 34
0

You can create a subaccount for each subscribed number (documentation). You can then retrieve total usage per subaccount.

Pieter
  • 1,764
  • 1
  • 12
  • 16