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 a starting date and multiple
from numbers. 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.
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.
Next, we need to create two arrays. One will contain all of the
from numbers that we want to filter by and the other will be an empty array used for storing message records in greater detail.
from_numbers = ["+1xxxxxxxxxx", "+1xxxxxxxxxx", "+1xxxxxxxxxx", "+1xxxxxxxxxx"] d = 
Now we will call the List Messages API on each of the
from numbers by looping through the array. After the API has stored all the matching records, we will loop through them one by one to gather additional message details about each record and store it in the empty array
d that we created above.
for number in from_numbers: messages = client.messages.list(from_=number, date_sent_after=(datetime(2021, 10, 0o1))) for record in messages: d.append((record.from_, record.to, record.date_sent, record.status, record.sid, record.body, record.error_message))
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', 'Status', 'MessageSID', 'Message Body', 'Error Message')) print('dataframe') print('\n') print(df) df.to_csv('messages.csv', index=False, encoding='utf-8')
Updated 17 days ago