📌 Listing Messages with a Specific Error Code to CSV

Overview

These code snippets show how you can use SignalWire's Compatibility API to filter the messages in your project by any given error code, and then export the message data into a CSV for your own record keeping or for passing to our Support team.

To quickly run this code, check out our recipe below and replace the variables with your own! Or, keep reading for an in-depth walkthrough.

Python

What do I need to run this code?

For the following code to work, you will need to have pandas, DateTime, and the SignalWire Python SDK installed.

Read about the different ways to install pandas here.

Read about datetime and how to install using pip here.

Read about the SignalWire Python SDK and how to install here.

How to Run Snippet?

If you copy the code and save it to a file named listMessagesWithSpecificError.py, for example, to run it you will need to run:

  • MacOS/Linux - python3 listMessagesWithSpecificError.py
  • Windows - py listMessagesWithSpecificError.py

Code Walkthrough

Load the necessary libraries and instantiate the SignalWire Client

We will start by importing the necessary resources. In this example, that is datetime, pandas, and the SignalWire Client. We also need to instantiate the SignalWire client using the project ID, auth token, and space URL.

from datetime import datetime
from signalwire.rest import Client as signalwire_client
import pandas as pd

client = signalwire_client("ProjectID", "AuthToken", signalwire_space_url = 'SpaceURL')

Get the list of messages

In this example, I have included how to filter by DateSent and the from number. DateSent is a DateTime object where the order for the arguments is Year, Month, Date, Hour, Minute, Seconds. You can leave hour, minute, and seconds at 0, unless you have a specific time of day you would like to filter by. Error code is not something you can filter by directly in the List Messages API - that filtering will come later in the code instead!

📘

Octal Literals in Some Python Versions

For months Jan - September, A slight change was made because python version (3.9) does not support leading 0's in datetime anymore. You must use the 0o prefix for octal literals now. That is reflected below in the code. If your version doesn’t include that limitation, you can switch it back to 01 or whatever month you need.

We will call the List Messages API filtering by date and from number to narrow down the results. If you send at a very high volume and do not try to filter down further by date or from number or some other factor, you may find that the code takes a very long time to compile.

messages = client.messages.list(date_sent=datetime(2021, 11, 8),
                                from_="+1xxxxxxxxxx",
                                )

Add messages with matching error code to an array

Next, we will create an empty array d to store all the filtered messages with a specific error code. We will loop through each individual message record that was returned and if it matches the specific error code we are looking for, we will append it to d. You can read more about the possible error codes to look for here.

d = []

for record in messages:
    if record.error_code == 30022:
        d.append((record.from_, record.to, record.date_sent, record.sid, record.error_code))

You can expand this to include as many or as few parameters as you'd like. To see all of the parameters returned in the JSON response, you can view our API documentation here: List Messages API

Create the DataFrame, print it, and export to CSV

Next, we will create a dataframe using pandas with column name headers. It's important to make sure that the order of the headers matches the order of the parameters you inserted into the array above. If you choose to add more or remove parameters, make sure to double-check that the order matches, or your data will be mismatched in the CSV.

Lastly, we will print the dataframe for debugging purposes and export it to csv. Using the parameter index=False turns off the indexing for each row. You can rename messages.csv to be as specific or general as you'd like.

df = pd.DataFrame(d, columns=('From', 'To', 'Date', 'MessageSID',  'Error Code'))

print('dataframe')
print('\n')
print(df)

df.to_csv('messages.csv', index=False, encoding='utf-8')

Node.js

What do I need to run this code?

We will need the following libraries (click their names to get instructions on how to install them):

How to Run Snippet?

If you save this code snippet in a file called listMessagesWithSpecificError.js, for example, you then need to run:
node listMessagesWithSpecificError.js.

Code Walkthrough

Load the necessary libraries

const dfd = require("danfojs");
const fs = require('fs');
const { RestClient } = require("@signalwire/node");

Instantiate the SignalWire Rest Client

In order for us to connect to SignalWire later on in the code using the Rest Client we first need to make sure we update space_url, project_id, and access_token.

// TODO: Update with your credentials
let space_url = "YOURSPACENAME.signalwire.com";
let project_id = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXX";
let access_token = "PTxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

const client = new RestClient(
    project_id, 
    access_token, 
    { signalwireSpaceUrl: space_url });

Get the list of messages

Here we get the list of messages sent after 2022/01/01, and if there is a match with the error code we select, we add that particular message information to the data array.

let data = [];

client.messages.list({
    dateSentAfter: new Date(Date.UTC(2022, 01, 01))
})
.then((messages) => {
    messages.forEach((message) => {
        if (message.errorCode == 11200) {
            data.push([
                message.from,
                message.to,
                message.dateSent,
                message.sid,
                message.errorCode
            ]);
        }
    });
})

Create the DataFrame, print it, and export to CSV

Here we create a DataFrame from the data array, set our desired table formatting configuration, and then print all of it to the terminal. Lastly, we export it to CSV for future reference or to simplify the process of gathering relevant informatio to our Support team.

.then(() => {
    let allData = new dfd.DataFrame(data, {
        columns: [
            "From",
            "To",
            "Date",
            "Message SID",
            "Error Code",
        ],
        config: {
            tableDisplayConfig: {
                columns: [
                    { width: 1 },
                    { width: 13 },
                    { width: 13 },
                    { width: 24 },
                    { width: 36 },
                    { width: 13 }
                ],
            }
        }
    });

    allData.print();
    fs.writeFileSync("Messages.csv", dfd.toCSV(allData));
});

PHP

What do I need to run this code?

We will need the following libraries (click their names to get instructions on how to install them):

How to Run Snippet?

If you save this code snippet in a file called listMessagesWithSpecificError.php, for example, you then need to run:
php listMessagesWithSpecificError.php.

Code Walkthrough

Load the necessary libraries

<?php
require __DIR__ .'/vendor/autoload.php';

use SignalWire\Rest\Client;

Instantiate the SignalWire Rest Client

In order for us to connect to SignalWire later on in the code using the Rest Client we first need to make sure we update space_url, project_id, and access_token.

// TODO: Update with your credentials
$client = new Client('YOUR-PROJECT-ID', 'PTYOUR-API-TOKEN', array("signalwireSpaceUrl" => "YOURSPACEURL.signalwire.com"));

Get the list of messages

Here we get the list of messages sent after 2022/01/01, but before 2022/04/29. If you only want to look for messages sent after a date, but not before a specific date you could comment out that line.

// Can filter message by whatever parameters you need. This example is any message sent after January 1, 2022.
$messages = $client->messages->read([
    "dateSentAfter" => "2022-01-01",
    "dateSentBefore" => "2022-04-29",
]);

Create the error array and the output CSV

We setup an array $header_fields to help organize our output CSV. We then open our output CSV Messages.csv and print the header row to it. Here we also create an array $errors and set our desired error codes to filter on.

// Write Headers for output CSV.
$header_fields = array('From', 'To', 'Date Sent', 'Message SID', 'Error Code');
echo '"'.implode('","', $header_fields).'"'."\n";
// Open File named Messages.csv
$fp = fopen('Messages.csv', 'w');
// Insert headers into output Messages.csv file.
fputcsv($fp, $header_fields);
// Define array of valid error codes you want to look for
$errors = array(
    '11200',
    '32000',
);

Write out the relevant messages to CSV

For this last step we'll set up an array $row of output data from the messages matching the specified fields from our earlier headers, and then also filter to exclude results that do not have the relevant error codes. We then print this data to our output CSV and then close that file.

// Write rows
foreach ($messages as $message) {
    $row = array(
            $message->from,
            $message->to,
            $message->dateSent->format('Y-m-d H:i:s'),
            $message->sid,
            $message->errorCode
        );
    // Exclude any records where there is no error code present.
    if (!in_array($row[4],$errors)) {
        continue;
    }
    // Insert rows into CSV
    fputcsv($fp, $row);
    echo '"'.implode('","', $row).'"'."\n";
}

// close file
fclose($fp);

Wrap up

In case something ever goes wrong with your messaging traffic, using this snippet you now know how to get relevant message information to investigate the problem or ask our Support team for help!

Sign Up Here

If you would like to test this example out, you can create a SignalWire account and space here.

Please feel free to reach out to us on our Community Slack or create a Support ticket if you need guidance!


Did this page help you?