Retrieve messages exchanged within a conversation

This article shows you how to retrieve messages exchanged within a conversation. The aim is to be able to export these messages in Json format.

To do this, you'll need to use the GraphQL resource "conversation". You'll first need to authenticate yourself by retrieving a GraphQL token.

You must then retrieve the identifier of the conversation for which you wish to retrieve the messages. For example, you can retrieve all the identifiers of closed conversations over a given period via the "closedconversations" resource.

Once you've retrieved your GraphQL token and conversation identifier, here's an example of the code you'll need to implement to retrieve the messages exchanged during the conversation:

const APIToken = "xxxxxxxx";
const convID = "xxxxxxxx";


const options = {
  "method": "POST",
  "headers": {
    "Authorization": `Bearer ${APIToken}`,
    "Content-Type": "application/json"
  },
  "body": JSON.stringify({
    query: `query MyQuery($conversationId: UUID!) {
      conversation(id: $conversationId) {
        messages {
          edges {
            node {
              __typename
              ... on ParticipantConversationMessage {
                text
                createdAt
                author {
                  __typename
                }
              }
            }
          }
        }
      }
    }`,
    variables: {
    conversationId: convID
    }
  })
};
fetch("https://api.iadvize.com/graphql", options).then(async (response) => {
 
  let json = await response.json();
 
  let messages = json.data.conversation.messages.edges
    .filter(edge => edge.node.__typename === 'ParticipantConversationMessage')
    .map(edge => ({
    author: edge.node.author.__typename,
    text: edge.node.text,
    sentDate: edge.node.createdAt
  }));
 
  // Do what you want / need
  console.log(messages);

});

Last updated