back to tutorials

How to build conversations exporting script with ChatBotKit Node SDK

Here you will find a step-by-step tutorial on how to export conversations using the ChatBotKit Node SDK. This can be useful for analyzing user interactions and improving your chatbot's performance.

The tutorial includes instructions on setting up the project, creating a .env file, implementing the conversation export script, and running the script. If you're ready to get started, follow the steps below.

Step 1: Set up the project

  1. Create a new directory for your project and navigate to it using the command line.

  2. Initialize a new Node.js project by running the following command:

  3. Install the required dependencies by running the following command:

Step 2: Create a .env file

  1. Create a new file in the project directory and name it .env.

  2. Open the .env file and add the following line:

    Replace your_api_secret with the actual API secret provided by ChatBotKit.

Step 3: Implement the conversation export script

  1. Create a new JavaScript file in the project directory, e.g., export-conversations.js.

  2. Copy the provided code into the export-conversations.js file.

  3. Import the required modules at the top of the file:

  4. Load the environment variables from the .env file by adding the following line at the beginning of the script:

  5. Define an asynchronous function named main() that will handle the conversation export:

  6. Instantiate a new ConversationClient with the API secret obtained from the environment variables:

  7. Create an empty array to store the conversations:

  8. Use a for-await-of loop to iterate over the conversations retrieved from the ChatbotKit Conversation API using the list().stream() method:

  9. Inside the loop, create an empty object to store the conversation details and messages:

  10. Use another for-await-of loop to iterate over the messages of the current conversation using the message.list(conversationData.id).stream() method:

  11. Inside the inner loop, push each message's details to the conversation.messages array:

  12. After the inner loop ends, push the processed conversation object to the conversations array:

  13. Convert the conversations array to JSON format with proper indentation:

  14. Write the JSON content to a file named "conversations.json" using the fs.writeFile() method:

  15. Call the main() function to start the conversation export process:

Step 4: Run the script

  1. Open a command prompt and navigate to the project directory.

  2. Run the script using the following command:

  3. The script will start exporting conversations one by one, displaying the progress in the console.

  4. Once the script completes, you will find a file named "conversations.json" in the project directory containing the exported conversations with their respective messages.

That's it! You have successfully implemented a script to export conversations using the ChatBotKit Node SDK. Feel free to modify the code or integrate it into your own applications as needed. A full copy of the code can be found here.