List Messages

👍

You're ready for this how-to guide if you've got the following:

A FreeClimb account
A registered application
A configured FreeClimb number
Your tools and language installed


Node.js

Using the FreeClimb SDK, you can make an asynchronous request for all messages associated with the account. Successful invocation returns the first page of messages and the object enables sequential iteration through the pages. Then, nextPageUri can retrieve the entire list of messages. Additional filtering criteria such as date and message are possible, but not shown in this example. The complete list of filtering criteria can be found within the List Messages API Reference.

Create your package.json file and save in the root directory of your project:

{
  "name": "node-list-messages-how-to-guide",
  "version": "1.0.0",
  "license": "MIT",
  "dependencies": {
    "@freeclimb/sdk": "^3.8.0",
    "dotenv": "^8.1.0"
  }
}

Install the packages:

yarn install

Get the list of messages:

require('dotenv').config()
const freeclimbSDK = require('@freeclimb/sdk')

const accountId = process.env.ACCOUNT_ID
const apiKey = process.env.API_KEY
const configuration = freeclimbSDK.createConfiguration({ accountId, apiKey })
const freeclimb = new freeclimbSDK.DefaultApi(configuration)

getMessages().then(messages => {
  console.log('got messages', messages)
}).catch(err => {
  console.log(err)
})

async function getMessages() {
  const messages = []

  let response = await freeclimb.listSmsMessages()
  messages.push(...response.messages)

  while (response.nextPageUri) {
    response = await freeclimb.getNextPage(response)
    messages.push(...response.messages)
  }
  return messages
}

Start the script:

node listMessages.js

Java

After creating the FreeClimbClient, you can access the MessagesRequester via the messages attribute to make a synchronous request for all messages associated with the account. Call the get function to return a MessageList, which contains the paginated list of messages and allows for sequential iteration. Additional filtering criteria, such as date and message are possible, but not shown in this example. The complete list of filtering criteria can be found within the List Messages API Reference.

Create your build.gradle file and save it to the root directory in your project:

/*
 * This file was generated by the Gradle 'init' task.
 *
 * This is a general purpose Gradle build.
 * Learn how to create Gradle builds at https://guides.gradle.org/creating-new-gradle-builds
 */

buildscript {
    repositories {
        mavenCentral()
        maven { url 'https://jitpack.io' }
    }
  
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'application'

mainClassName = 'Application'

repositories {
    mavenCentral()
    maven { url 'https://jitpack.io' }
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    testCompile "junit:junit"
    compile 'com.github.FreeClimbAPI:java-sdk:3.0.0'
}

sourceSets {
    main {
        java {
            srcDirs = ['src'] // changed line
        }
    }
}

Get the list of messages:

import com.vailsys.freeclimb.api.FreeClimbClient;
import com.vailsys.freeclimb.api.FreeClimbException;
import com.vailsys.freeclimb.api.message.MessageList;
import com.vailsys.freeclimb.api.message.Message;
import java.util.ArrayList;

public class Application {

  public static void main(String[] args) {
    FreeClimbClient client;
    MessageList messageList;
    String accountId = System.getenv("ACCOUNT_ID");
    String apiKey = System.getenv("API_KEY");
    try {
      client = new FreeClimbClient(accountId, apiKey); // Create FreeClimbClient object
      messageList = client.messages.get();

      // Don't bother trying to grab more pages if there is only one or zero
      // pages of results
      if (messageList.getTotalSize() > messageList.getPageSize()) {
        // Load in all the messages returned.
        while (messageList.getLocalSize() < messageList.getTotalSize()) {
          messageList.loadNextPage(); // Load in the next page of messages.
        }
      }

      ArrayList<Message> allMessages = messageList.export(); // Extract the array from the messageList

      for (Message m : allMessages) {
        // do something with each message
        System.out.print(m);
      }
    } catch (FreeClimbException e) {
      // Handle Errors
    }
  }
}

Build and run the program:

gradle run

C#

After creating the FreeClimbClient, you can access the MessagesRequester to make a synchronous request for all messages associated with the account. Call the get function to return a MessageList, which allows for sequential iteration through the list of messages. Additional filtering criteria, such as date and message are possible, but not shown in this example. The complete list of filtering criteria can be found within the List Messages API Reference.

Install the FreeClimb SDK:

dotnet add package freeclimb-cs-sdk --version 1.0.0.1

Get the list of messages:

using System;
using System.Collections.Generic;
using com.freeclimb.api;
using com.freeclimb.api.message;

namespace ListRecordings {

  class Program {
    public static string getFreeClimbAccountId () {
      return System.Environment.GetEnvironmentVariable("ACCOUNT_ID");
    }
    public static string getFreeClimbApiKey () {
      return System.Environment.GetEnvironmentVariable("API_KEY");
    }

        static void Main(string[] args)
        {
            // Create FreeClimbClient object
            FreeClimbClient client = new FreeClimbClient(getFreeClimbAccountId(), getFreeClimbApiKey());

            // Invoke get method to retrieve initial list of recording information
            MessageList messageList = client.getMessagesRequester.get();

            // Check if list is empty by checking total size of the list
            if (messageList.getTotalSize > 0)
            {
                // retrieve all recording for server
                while (messageList.getLocalSize < messageList.getTotalSize)
                {
                    messageList.loadNextPage();
                }

                // Convert current pages recording information to a linked list
                LinkedList<IFreeClimbCommon> commonList = messageList.export();

                // Loop through linked list to process recording information
                foreach (IFreeClimbCommon element in commonList)
                {
                    // Cast each element to the Recording element for processing
                    Message message = element as Message;

                    // Process recording element
                    Console.Write(message.getText);
                }
            }
        }
  }
}

Python

After creating the FreeClimb ApiClient object, you can use the list_sms_messages function in the DefaultApi class to make a list_sms_messages request and return the first page of messages. Then, the next_page_uri attribute can retrieve additional pages. In this example, these messages are stored in a file named message_results.txt. Additional filtering criteria, such as date and message are possible, but not shown in this example. The complete list of filtering criteria can be found within the List Messages API Reference.

Create your Requirements file and save in the root directory of your project:

requests >= 2.22.0
freeclimb >= 4.0.0

Install the packages:

$ pip install -r requirements.txt

Get the list of messages through the FreeClimb Python SDK:

import freeclimb
import os
import requests
import json
from freeclimb.api import default_api


configuration = freeclimb.Configuration(
    # Defining host is optional and default to https://www.freeclimb.com/apiserver
    host     = "https://www.freeclimb.com/apiserver",
    # Configure HTTP basic authorization: fc
    username = os.environ['ACCOUNT_ID'],
    password = os.environ['API_KEY']
)

with freeclimb.ApiClient(configuration) as api_client:
    # Create an instance of the API class
    api_instance = default_api.DefaultApi(api_client)
    # Create an instance of the API class

    first_message = api_instance.list_sms_messages()

    next_page_uri = first_message.next_page_uri

    all_messages = []
    all_messages.extend(map(lambda x: x.to_dict(), first_message.messages))
    while(next_page_uri != None):
        next_message = requests.get(url=configuration.host + next_page_uri,
                                    auth=(configuration.username, configuration.password))
        all_messages.extend(next_message.json().get('messages'))
        next_page_uri = next_message.json().get('next_page_uri')

    file = open("message_results.json", "w")
    file.write(json.dumps(all_messages))
    file.close()