This article is part of the Twilio Conversations API series
In this article, you will learn how to get a Conversation and it’s associated Participants. Listing all Conversations is fundamental to building an app to select which Conversation to interact with. Once you have a list of Conversations, you can work on fetching the participants for each
You can list all Conversations in your Twilio account using the following code
const accountSid = "ACxxxxxxxxxxxxxxxxxx";
const authToken = "xxxxxxxxxxxxxxxxxx";
const client = require("twilio")(accountSid, authToken);
(async () => {
const conversations = await client.conversations.conversations.list();
console.log(ConversationsGrant);
})();
You should then get an array of JSON objects representing each Conversations
[
{
accountSid: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
chatServiceSid: 'ISxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
messagingServiceSid: 'MGxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
sid: 'CHxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
friendlyName: 'Test conversation with SMS participants only',
uniqueName: null,
attributes: '{}',
state: 'active',
dateCreated: 2021-05-30T01:43:06.000Z,
dateUpdated: 2021-05-30T01:43:06.000Z,
timers: {},
url: 'https://conversations.twilio.com/v1/Conversations/CHxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
links: {
participants: 'https://conversations.twilio.com/v1/Conversations/CHxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/Participants',
messages: 'https://conversations.twilio.com/v1/Conversations/CHxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/Messages',
webhooks: 'https://conversations.twilio.com/v1/Conversations/CHxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/Webhooks'
}
},
...
]
Let’s take this a step further and get the participants for each Conversation. The result will be a hash of conversations. They key of each will be the Conversation name and the value will be each participant’s phone number
(async () => {
const conversations = await client.conversations.conversations.list();
let conversationsToParticipants = {};
for (let i = 0; i < conversations.length; i++) {
const participants = await client.conversations
.conversations(conversations[i].sid)
.participants.list();
conversationsToParticipants[conversations[i].friendlyName] =
participants.map((participant) => participant.messagingBinding);
}
console.log(conversationsToParticipants);
})();
Here’s what the above code returns after running it.
{
"My first conversation": [
{ "projected_address": "+1XXXXXXXXXX", "type": "sms" },
{ "projected_address": "+1XXXXXXXXXX", "type": "sms" },
{ "projected_address": "+1XXXXXXXXXX", "type": "sms" },
{ "projected_address": "+1XXXXXXXXXX", "type": "sms" },
{
"proxy_address": "+1XXXXXXXXXX",
"type": "sms",
"address": "+1XXXXXXXXXX"
}
],
"Test conversation with SMS participants only": [
{ "projected_address": "+1XXXXXXXXXX", "type": "sms" },
{ "projected_address": "+1XXXXXXXXXX", "type": "sms" },
{ "type": "sms", "address": "+1XXXXXXXXXX" },
{ "type": "sms", "address": "+1XXXXXXXXXX" },
{ "projected_address": "+1XXXXXXXXXX", "type": "sms" }
],
"2 Way Masked SMS": [],
"My First Conversation": [null]
}
Interested in learning more about Twilio Conversations? Check out my upcoming eBook on Twilio Conversations and other articles.