Zuned Ahmed
Zuned Ahmed

Reputation: 1387

how to get aws sms delivery report programmatically?

I am using AWS SNS to send SMS to mobile.

I can see S3 Delivery Status upload on S3 Option is there. But for that, I have to run batch daily. The report will almost delayed.

The Second Option I can see on Cloudwatch.

Is it possible from cloudwatch we can publish SQS event with logs detail So that I can write SQS subscriber to fetch the message delivery status? or if any other option available which I have not explored so far please let me know.

I am using Java and aws-java-sdk to fetch detail.

Please Note I have gone through the document but unable to find something useful.

Any Suggestion is welcome.

Please I have gone through document but unable to find

Upvotes: 2

Views: 807

Answers (2)

Arun Raj
Arun Raj

Reputation: 263

To get SMS delivery status you have to do the following

  1. Enable 'Delivery status logging' to AWS Cloudwatch More Info
  2. set permission for the user (used in script) to access the AWS Cloudwatch log
  3. Read the log entry from cloudwatch with awssdk.

See the below PHP code which I am using in my project, you can refer this to a create java code

require 'inc/awsSdkForPhp/aws-autoloader.php';
$params = array(
    'credentials' => array(
        'key' => '<YOUR KEY>',
        'secret' => '<YOUR SECRET>',
    ),
    'region' => 'us-east-1', //  your aws from SNS region
    'version' => 'latest'
);
$cwClient = new \Aws\CloudWatchLogs\CloudWatchLogsClient($params);
$queryRes = $cwClient->startQuery([
  'endTime' => 1621231180,  // UNIX TIMESTAMP 
  'logGroupName' => 'sns/us-east-1/***/DirectPublishToPhoneNumber', // YOUR LOG GROUP NAME
  'queryString' => 'fields @timestamp, status, @message
  | filter notification.messageId="5a419afc-c4b3-55b3-85f9-c3e7676b2dd2"', // YOUR MESSAGE ID
  'startTime' => 1620954551 // START UNIX TIMESTAMP 
]);

$qryID = $queryRes->get('queryId');
sleep(3); // To wait the execution to be completed.
$resultObj =  $cwClient->getQueryResults(array(
  'queryId' => $qryID, // REQUIRED
));

//echo "<pre>";print_r($resultObj);echo "</pre>";
$result = $resultObj->get('results');

$jsnRs = json_decode($result[0][2]['value']); // TO get the delivery array 

echo "<br>status :".$jsnRs->status;
echo "<br>phone Carrier :".$jsnRs->delivery->phoneCarrier;
echo "<br>provider Response :".$jsnRs->delivery->providerResponse;
echo "<pre>";print_r($jsnRs);echo "</pre>";

I believe it will help someone

Upvotes: 2

Zuned Ahmed
Zuned Ahmed

Reputation: 1387

we can use lamda to readlogs from awscloudwatch by watch awscloudwatch events.

Upvotes: 0

Related Questions