List a User's Conversations
In this code snippet you learn how to get a list of Conversations a User is associated with.
Example
Ensure the following variables are set to your required values using any convenient method:
| Key | Description |
|---|---|
USER_ID | The unique ID of the User. |
Prerequisites
You will need to use an existing Application containing at least one Conversation and one User in order to see a list of a User's Conversations. See the Create Conversation code snippet for information on how to create an Application and a Conversation. See also the Create User code snippet on how to create a User.
Write the code
Add the following to list-user-conversations.sh:
curl "https://api.nexmo.com/v1/users/$CONV_USER_ID/conversations" \
-H 'Authorization: Bearer '$JWT\
-H 'Content-Type: application/json'Run your code
Save this file to your machine and run it:
Prerequisites
You will need to use an existing Application containing at least one Conversation and one User in order to see a list of a User's Conversations. See the Create Conversation code snippet for information on how to create an Application and a Conversation. See also the Create User code snippet on how to create a User.
npm install @vonage/server-sdkCreate a file named list-user-conversations.js and add the following code:
const { Vonage } = require('@vonage/server-sdk');
const vonage = new Vonage({
applicationId: VONAGE_APPLICATION_ID,
privateKey: VONAGE_PRIVATE_KEY,
});Write the code
Add the following to list-user-conversations.js:
const run = async () => {
try {
for await (const conversation of vonage.conversations.listAllUserConversation(USER_ID)) {
console.log(conversation);
}
} catch (error) {
console.error(error);
}
};
run();Run your code
Save this file to your machine and run it:
Prerequisites
You will need to use an existing Application containing at least one Conversation and one User in order to see a list of a User's Conversations. See the Create Conversation code snippet for information on how to create an Application and a Conversation. See also the Create User code snippet on how to create a User.
Add the following to build.gradle:
implementation 'com.vonage:server-sdk:9.3.1'Create a file named ListUserConversations and add the following code to the main method:
VonageClient client = VonageClient.builder()
.applicationId(VONAGE_APPLICATION_ID)
.privateKeyPath(VONAGE_PRIVATE_KEY_PATH)
.build();Write the code
Add the following to the main method of the ListUserConversations file:
var conversations = client.getConversationsClient().listUserConversations(USER_ID);
conversations.forEach(System.out::println);Run your code
We can use the application plugin for Gradle to simplify the running of our application. Update your build.gradle with the following:
apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''Run the following gradle command to execute your application, replacing com.vonage.quickstart.conversation with the package containing ListUserConversations:
Prerequisites
You will need to use an existing Application containing at least one Conversation and one User in order to see a list of a User's Conversations. See the Create Conversation code snippet for information on how to create an Application and a Conversation. See also the Create User code snippet on how to create a User.
Install-Package VonageCreate a file named GetUserConversations.cs and add the following code:
using System;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Vonage;
using Vonage.Conversations.GetUserConversations;
using Vonage.Request;Add the following to GetUserConversations.cs:
var credentials = Credentials.FromAppIdAndPrivateKeyPath(VONAGE_APPLICATION_ID, VONAGE_PRIVATE_KEY_PATH);
var client = new VonageClient(credentials);Write the code
Add the following to GetUserConversations.cs:
var response = await client.ConversationsClient.GetUserConversationsAsync(GetUserConversationsRequest.Build()
.WithUserId(USER_ID)
.Create());Prerequisites
You will need to use an existing Application containing at least one Conversation and one User in order to see a list of a User's Conversations. See the Create Conversation code snippet for information on how to create an Application and a Conversation. See also the Create User code snippet on how to create a User.
composer require vonage/clientCreate a file named list-user-conversations.php and add the following code:
require_once __DIR__ . '../../config.php';
require_once __DIR__ . '../../vendor/autoload.php';
$keypair = new \Vonage\Client\Credentials\Keypair(
file_get_contents(VONAGE_APPLICATION_PRIVATE_KEY_PATH),
VONAGE_APPLICATION_ID
);
$client = new \Vonage\Client($keypair);
$client->conversation()->listUserConversationsByUserId(USER_ID);Run your code
Save this file to your machine and run it:
Try it out
When you run the code you will get a list of Conversations associated with the specified User.
In this case the User is associated with a single Conversation:
[
{
"name": "mega_chat",
"timestamp": {
"created": "2018-10-18T13:28:14.760Z"
},
"image_url": "",
"display_name": "Mega Chat Room",
"state": "JOINED",
"member_id": "MEM-62667429-e0fa-4adb-87a9-b4768fd46fce",
"sequence_number": 8,
"href": "https://api.nexmo.com/v1/users/USR-ebf6ca49-4941-4762-b7a6-c30cb0c06179/conversations/CON-0b72410c-e090-45b2-86b7-65ff0c986c02",
"id": "CON-0b72410c-e090-45b2-86b7-65ff0c986c02"
}
]