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.
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.
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')
In this example, I have included how to filter by
DateSent and the
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", )
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
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')
Updated 17 days ago