import Tabs from @theme/Tabs; import TabItem from @theme/TabItem; Overview -- These code snippets show how you can use SignalWires 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. Full code example: List Messages with a Specific Error Code
python from datetime import datetime from signalwire.rest import Client as signalwire_client import pandas as pd client = signalwire_client(ProjectID, AuthToken, signalwire_space_url=YOURSPACE.signalwire.com) messages = client.messages.list(date_sent=datetime(2021, 11, 8), from_=+1xxxxxxxxxx, ) d = [] for record in messages: if record.error_code == 30022: d.append((record.from_, record.to, record.date_sent, record.sid, record.error_code)) 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) js const dfd = require(danfojs); const fs = require(fs); const { RestClient } = require(@signalwire/compatibility-api); // 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 = RestClient( project_id, access_token, { signalwireSpaceUrl: space_url }); 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 ]); } }); }) .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 YOUR-SPACE-URL.signalwire.com)); // 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, ]); // 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 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); ruby # load all gems from our gemfile require dotenv/load require signalwire/sdk require csv # set authentication variables and establish our SignalWire Client username = ENV[projectID] password = ENV[token] host = @client = Signalwire::REST::Client.new username, password, signalwire_space_url: host # request the message list from SignalWire messages = @client.messages.list( #specify to filter messages sent AFTER x date #date_sent_after:Date.new(2022,5,23) #specify to filter messages sent BEFORE x date #date_sent_before:Date.new(2022,5,23) #specify to filter messages sent BY a specific TFN in e.164 format #to:+12223334444 ) # our list for data messageList = [] # headers for our csv headers = [sid,to,from,date] # set error code (INT only). Options: https://developer.signalwire.com/compatibility-api/reference/error-codes errorCode = 30007 # for each message, if the error code matches the code we are searching for, push the message data to our messageList messages.each do|message| if message.error_code == errorCode messageList.push([message.sid,message.to,message.from,message.date_sent,]) end end # write messageList to our CSV with headers CSV.open(Msg_ECC_+errorCode.to_s+.csv,w,headers: headers, write_headers:true) do |csv| messageList.each do |message| csv << message end end 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](https://pandas.pydata.org/docs/getting_started/install.html). Read about datetime and how to install using pip [here](https://pypi.org/project/DateTime/). Read about the SignalWire Python SDK and how to install [here](/compatibility-api/sdks). ### 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. python 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! :::info Octal Literals in Some Python Versions For months Jan - September, A slight change was made because python version (3.9) does not support leading 0s 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. python 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](/rest/compatibility-api/overview/error-codes). python 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 youd like. To see all of the parameters returned in the JSON response, you can view our API documentation here: [List Messages API](/rest/compatibility-api/endpoints/list-messages) #### Create the DataFrame, print it, and export to CSV Next, we will create a dataframe using pandas with column name headers. Its 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 youd like. python 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): - [Danfo.js](https://www.npmjs.com/package/danfojs) - [fs](https://nodejs.org/api/fs.html) - there is no need to install this module, as it is part of the Node.js Core - [SignalWire Rest Client](https://www.npmjs.com/package/@signalwire/compatibility-api) ### 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 javascript const dfd = require(danfojs); const fs = require(fs); const { RestClient } = require(@signalwire/compatibility-api); #### 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. javascript // 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 = 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. javascript 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. javascript .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): - [SignalWire Rest Client](/compatibility-api/sdks) ### 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 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. php // 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. php // 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 well 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. php // 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); Ruby - ### What do I need to run this code? This snippet uses two gems [SignalWire](https://rubygems.org/gems/signalwire) and [Dotenv](https://github.com/bkeepers/dotenv). Additionally this snippet requires CSV which is part of the ruby standard gems You will need your SignalWire Project ID, API Token, and Space URL. ### How to Run Snippet? 1. Configure your SignalWire credentials in your .env file 2. (Optionally) configure date-constraints using the comments 3. Set the errorCode variable to the code you would like to search for 4. Run app.rb, the CSV will be saved in the root directory of the script ### Code Walkthrough #### Set-up First we need to load all of our gems and initialize our SignalWire client. ruby # load all gems from our gemfile require dotenv/load require signalwire/sdk require csv # set authentication variables and establish our SignalWire Client username = ENV[projectID] password = ENV[token] host = @client = Signalwire::REST::Client.new username, password, signalwire_space_url: host #### Retrieve message list Next we will use our Client to retrieve a list of all messages. Optionally, use the comments to filter data with specific parameters. ruby # request the message list from SignalWire messages = @client.messages.list( #specify to filter messages sent AFTER x date #date_sent_after:Date.new(2022,5,23) #specify to filter messages sent BEFORE x date #date_sent_before:Date.new(2022,5,23) #specify to filter messages sent BY a specific TFN in e.164 format #to:+12223334444 ) #### Filtering Data Then we will set up some variables, and begin to loop through the messages that our Client returned, adding relevant messages to our messageList. ruby # our list for data messageList = [] # headers for our csv headers = [sid,to,from,date] # set error code (INT only). Options: https://developer.signalwire.com/compatibility-api/rest/overview/error-codes errorCode = 30007 # for each message, if the error code matches the code we are searching for, push the message data to our messageList messages.each do|message| if message.error_code == errorCode messageList.push([message.sid,message.to,message.from,message.date_sent,]) end end #### Export to CSV Finally, we can export our messageList to a CSV file. By default the CSV file will include the error code in the file name, and be saved the the root directory you run the app.rb script from. ruby # write messageList to our CSV with headers CSV.open(Msg_ECC_+errorCode.to_s+.csv,w,headers: headers, write_headers:true) do |csv| messageList.each do |message| csv << message end end 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, [create a SignalWire account and Space](https://m.signalwire.com/signups/new?s=1). Please feel free to reach out to us on our [Community Slack](https://signalwire.community/) or create a Support ticket if you need guidance!