Intro to SignalWire Statistics through Python API

Using Python tools to monitor SignalWire Messaging Activity

To quickly run this code, check out our recipe below and replace the variables with your own!

Step by step walkthroughs will be provided in further detail below.

Why Stats?

Have you ever wanted to gather data on the status of your messages but do not want to go through the hassle of checking each message individually? SignalWire hopes that all of our customers are able to utilize our products to revolutionize telecom and it all begins with successful delivery. If you share this sentiment with us then look no further as we dive into a few statistical methods to help further analyze the activity within your SignalWire Space.

Well, what are we going to do?

The power of API’s cannot be understated. In this example we are going to create an API request to gather status information on many messages all at once. From this pulled data, this code will also give an introduction to creating plots through matplotlib, creating data frames through pandas, and lastly how to format appropriate data to identify potential issues.

What you need to run the code

You must have the SignalWire Python SDK installed. You can install that here:

Python REST API will be used with the assistance of some packages: matplotlib.pyplot, pandas, and datetime. Resources on these will be mentioned below.

What variables change?

ProjectID - Your project ID is an alphanumeric string that tells the SignalWire SDK where to find your project. You can find this in an easily copyable format by going to your
SignalWire Portal and clicking the API tab on the lefthand side.

AuthToken - Your Auth Token is an alphanumeric string that helps to authenticate your HTTP requests to SignalWire. You can create this (if you haven’t already) or copy this in an easily copyable format by going to your SignalWire Portal and clicking the API tab. If you have not created an API token, press the blue new button. If you have, click show and copy the string.

SpaceURL - Your space URL is the domain of your space, i.e. example.signalwire.com. This can also be found in an easily copyable format within the API tab in your SignalWire space.

Change the date and from number when calling clients.messages.list() to specify the date range and from number that you need. If you are filtering by another parameter, you can add it within client.messages.list()

Numbers – Populate the ‘numbers’ array towards the end of the code, with the numbers that you are wanting to analyze. These must be formatted as ‘+1XXXYYYZZZZ’

The Code

from signalwire.rest import Client as signalwire_client
import matplotlib.pyplot as plt
import pandas as pd
from datetime import datetime


client = signalwire_client("AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE", "PTXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", signalwire_space_url = 'EXAMPLE.signalwire.com')

# List All Messages Sent on 5/14/2021
all_messages = client.messages.list(
                    date_sent=datetime(2021, 5, 14, 0, 0),
                                    )

# Create an array listing the status of each message listed
all=[]
for record in all_messages:
        all.append((record.status))

#Count how many of each Status is in 'all' array. Transfer Variables to Int
total_sent=int(all.count("sent"))
total_received=int(all.count("received"))
total_delivered=int(all.count("delivered"))
total_undelivered=int(all.count("undelivered"))

#Calculate Total Number Inbound and Outbound Messages
num_outbound_messages = total_sent + total_delivered + total_undelivered
num_inbound_messages = total_received

#Inbound vs Outbound Messages Pie Chart
inbound_outbound = [num_inbound_messages , num_outbound_messages]
my_colors = [ 'blue', 'grey']
vs_label = ['Inbound', 'Outbound']

plt.pie( inbound_outbound, colors= my_colors , autopct='%1.1f%%', labels= vs_label)
plt.title('Direction of All Messages')
plt.show()
plt.close()


## Status of Outbound Messages Pie Chart
outbound_pie_int = [total_sent,total_undelivered,total_delivered]
outbound_pie_label= ['Sent', 'Undelivered', 'Delivered']
my_colors2 = ['lightblue','Red','lightgreen']

plt.pie(outbound_pie_int, labels=outbound_pie_label,autopct='%1.1f%%', colors=my_colors2)
plt.title('Status of Outbound Messages')
plt.show()
plt.close()


## Print out Some Stats

# Previously, we looked at all numbers, now we are going to narrow in on a few numbers
d=[]
# List Numbers for Testing
numbers = ["+1XXXXXXXXXXX","+1XXXXXXXXXX","+1XXXXXXXXXX","+1XXXXXXXXXX"]

# Array that list's status of Messages from 'Numbers'
r=[]
# Array that list's Inbound Messages to 'Numbers'
sms_in=[]
#Array used for All Undelivered Messages in Space
un_m=[]

for x in numbers:
    # Listing all Messages from every number listed on the date 5/16/2021
    messages = client.messages.list(
                               date_sent=datetime(2021, 5, 14, 0, 0),
                               from_=x)
    # Listing all Messages to every number listed on the date 5/16/2021
    sms_inbound= client.messages.list(
                               date_sent=datetime(2021, 5, 14, 0, 0),
                               to=x)

# Append our Matrix with the Status of every outbound message
    for record in messages:
            r.append((record.status))
            the_status= record.status
            
            if the_status =='undelivered':
                un_m.append((record.to, record.status, record.sid))


    num_sent = int(r.count("sent"))
    num_delivered = int(r.count("delivered"))
    num_undelivered = int(r.count("undelivered"))

    for record in sms_inbound:
                sms_in.append((record.status))

                num_received= int(sms_in.count("received"))

    print("For the number:" + str(x))
    print("You Have " + str(num_sent) + " Sent Messages")
    print("You Have " + str(num_delivered) + " Delivered Messages")
    print("You Have " + str(num_undelivered) + " Undelivered Messages")
    print("You Also Have " + str(num_received) + " Inbound"+"\n")

    r=[]
    sms_in=[]

df=pd.DataFrame(un_m, columns=('Number', 'Status', 'SID'))
print(df)

Sample Outputs:

Comments

This code introduced to you three different techniques to view information given by SignalWire Compatibility APIs. We learned how to create a plot through myplotlib, how to create a data table through pandas, and lastly how to declare variables to further print out console friendly outputs. More resources on these python specific packages are listed below and I highly recommend looking at all of the neat customizations that are possible.


Did this page help you?