How to retrieve all messages from a conversation twilio

Viewed 98

I am trying to retrieve a list of messages in a specific conversation using Twilio's api. I followed this api which allows me to retrieve messages matching filtered criteria. Below is my code:

const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = require("twilio")(accountSid, authToken);

(async () => {
    try {
        const messages = await client.messages.list({
            from: fromNumber,
            to: toNumber,
            limit: 20,
        });

        console.log(messages);
    } catch (error) {
        console.log(error);
    }
})();

This is great, but it only lists the messages from fromNumber. I want to see messages from toNumber as well.

I believe this is the api that I should use:

const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = require('twilio')(accountSid, authToken);

client.conversations.conversations('CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
      .messages
      .list({limit: 20})
      .then(messages => messages.forEach(m => console.log(m.sid)));

But I'm not sure how to obtain CHXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.

I realize I can use my code above and flip toNumber and fromNumber then stitch the messages together, but I'd like to get the entire conversation if possible.

0 Answers
Related