Cómo crear un script de exportación de conversaciones con 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
-
Create a new directory for your project and navigate to it using the command line.
-
Initialize a new Node.js project by running the following command:
npm init -y
-
Install the required dependencies by running the following command:
npm install dotenv @chatbotkit/sdk
Step 2: Create a .env file
-
Create a new file in the project directory and name it
.env
. -
Open the
.env
file and add the following line:CHATBOTKIT_API_SECRET=your_api_secret
Sustituir
your_api_secret
with the actual API secret provided by ChatBotKit.
Step 3: Implement the conversation export script
-
Create a new JavaScript file in the project directory, e.g.,
export-conversations.js
. -
Copy the provided code into the
export-conversations.js
file. -
Import the required modules at the top of the file:
import * as dotenv from 'dotenv' import fs from 'fs/promises' import { ConversationClient } from '@chatbotkit/sdk/conversation/index.js'
-
Load the environment variables from the
.env
file by adding the following line at the beginning of the script:dotenv.config()
-
Define an asynchronous function named
main()
that will handle the conversation export:async function main() { // Code for conversation export goes here }
-
Instantiate a new
ConversationClient
with the API secret obtained from the environment variables:const client = new ConversationClient({ secret: process.env.CHATBOTKIT_API_SECRET, })
-
Create an empty array to store the conversations:
const conversations = []
-
Use a
for-await-of
loop to iterate over the conversations retrieved from the ChatbotKit Conversation API using thelist().stream()
method:for await (const { data: conversationData } of client.list().stream()) { // Code to process each conversation goes here }
-
Inside the loop, create an empty object to store the conversation details and messages:
const conversation = { id: conversationData.id, createdAt: conversationData.createdAt, messages: [], }
-
Use another
for-await-of
loop to iterate over the messages of the current conversation using themessage.list(conversationData.id).stream()
method:for await (const { data: messageData } of client.message.list(conversationData.id).stream()) { // Code to process each message goes here }
-
Inside the inner loop, push each message's details to the
conversation.messages
array:conversation.messages.push({ id: messageData.id, type: messageData.type, text: messageData.text, createdAt: messageData.createdAt, })
-
After the inner loop ends, push the processed conversation object to the
conversations
array:conversations.push(conversation)
-
Convert the
conversations
array to JSON format with proper indentation:const jsonContent = JSON.stringify(conversations, null, 2)
-
Write the JSON content to a file named "conversations.json" using the
fs.writeFile()
method:await fs.writeFile('conversations.json', jsonContent)
-
Call the
main()
function to start the conversation export process:main()
Step 4: Run the script
-
Open a command prompt and navigate to the project directory.
-
Run the script using the following command:
node export-conversations.js
-
The script will start exporting conversations one by one, displaying the progress in the console.
-
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.