Api is not returning total no.of messages in conversation

Hello,
I am developing an app which requires to fetch all the conversation messages and create a incident in third party domain.I am trying to iterate over API and fetch conversation messages and filter attachments from those messages but as there is no count or total no.of messages provided i am unable to decide when to stop iteration.
I am using this API “https://{freshchat domain}/v2/conversations/{conversation_id}/messages?page=1&items_per_page=20”. Is there any other way to continue.

Thanks,
Tipuranjali.

1 Like

Hi @Tipuranjali,

You could check the length of the conversation array returned before sending another request.

For Example, if the returned array has length greater than 0 send another request. If it is empty (i.e, length is 0) return all the conversations.

Thanks,
Arshath

2 Likes

Hi Arshath,
Thanks a lot for your suggestion.
But for every new request i.e returned array is having the same array length and becoming a infinite loop.

FYI API from freshchat is not working properly.Only recent data is displaying whole conversation messages are not displaying.

Thanks,
Tipuranjali.

1 Like

Hi @Tipuranjali,

You need to recursively call the function with the increasing page number for each call.

Let me know if this helps.

Thanks,
Arshath

Hi Arshath,

I am increasing the page number and calling the function recursively but what ever the response i am getting in page1 , same response i am getting in page2 and page3 also.

FYI please check the code once.

$(document).ready(function () {
app.initialized().then(function (_client) {
    var client = _client;
    client.events.on('app.activated', function () {
        var page = 1;
        conversationMessages(client, page);
    });
});

function conversationMessages(client, page) {
    var headers = {
        Authorization: "Bearer <token>",
        "Content-Type": "application/json"
    };
    var options = {
        headers: headers,
    };
    var url = `https://{freshchatDomain}/v2/conversations/{conversation_id}/messages?page=${page}&items_per_page=50`;
    client.request.get(url, options).then(function (conv_data) {
        var responseData = JSON.parse(conv_data.response);
        if (responseData.messages.length > 0) {
            page++;
            conversationMessages(client, page);
        }
    }, function (error) {
        console.log(error);
    });
}

});

Thanks,
Tipuranjali.

1 Like

Hi @Tipuranjali,

Apologies for the inconvenience caused here.

It looks like the API is returning the original result instead of the empty array if the page no does not have any data. I will have this raised with our team, so that they can fix this.

In the mean time as a workaround I would recommend you use this NPM package here that you can consume which will generate the transcript with various options.

Do try out the above NPM package and let me know if this works.

5 Likes

Hi @Arjun_Paliath,

I tried the above NPM package u provided this fullfilled my reqirement(i.e I am able to fetch whole conversation messages using conversation id).

Thanks a lot for help.

Thanks,
Tipuranjali.

3 Likes