Magidoc

A Beginner's Guide to Exporting All Conversations

Welcome! In this tutorial, we'll guide you through the process of exporting all textual data from your conversations using a Ruby script. Don't worry if you're new to Ruby, this tutorial is beginner-friendly and can help you understand how the process works, which can be adapted to other programming languages as well.

We'll be using the query described in the List All Conversations guide.

Installing Ruby

#

Before we begin, make sure you have Ruby installed on your computer. If you don't have it already, you can follow these instructions:

Alternatively, if you don't want to install Ruby on your computer, you can use an online tool like Replit to write and run Ruby code in your browser.

Script

#

To get started, you'll need to obtain your Access Token and include it in the script. Without it, the script won't work. To learn how to find your Access Token, refer to our Quickstart guide. Once you have your Access Token, you're all set to start exporting your conversations! Good luck, and happy coding!

This Ruby script fetches and exports all conversations and their messages. Let's break it down into sections and explain what each section does:

1. Loading required libraries and setting up constants

    
  

This section loads the required libraries for handling HTTP requests, SSL, JSON, and URIs. It also sets up the ACCESS_TOKEN and the URL constants. You need to replace "YOUR ACCESS TOKEN GOES HERE" with your actual access token.

2. Defining GraphQL fragments and queries

    
  

This section defines the necessary GraphQL fragments and queries that the script will use to fetch the conversations and messages.

3. Function: fetch_messages_for_conversation(conversation)

    
  

This function fetches all the messages for a given conversation object. It uses a while loop to paginate through the messages, making API requests until there are no more messages to fetch. Then, it combines all fetched messages into the conversation object.

4. Function: fetch_conversations

    
  

This function fetches all the conversations and their associated messages. It uses a while loop to paginate through the conversations, making API requests until there are no more conversations to fetch. For each conversation, it calls the fetch_messages_for_conversation function to fetch all the messages if there are more than initially retrieved. Finally, it saves the conversations to a JSON file named conversations.json .

5. Function: make_request(query, variables = {})

    
  

This function is a helper function to make HTTP requests to the API with the specified GraphQL query and variables. It sets up the necessary headers, creates the request body, sends the request, and parses the response.

6. Calling the main function

    
  

This line calls the fetch_conversations function to start the process of fetching and exporting all conversations and their messages.

7. Summing up

In summary, this script fetches all conversations and their associated messages using GraphQL and the Rasayel API. It then combines the data and saves it to a JSON file named conversations.json . The script is organized into functions that handle fetching messages for a conversation, fetching all conversations, making API requests, and executing the main function to fetch and export the data.

The full script

#