Listar las conversaciones de un usuario
En este fragmento de código aprenderás a obtener una lista de Conversaciones a las que está asociado un Usuario.
Ejemplo
Asegúrese de que las siguientes variables se ajustan a los valores requeridos utilizando cualquier método conveniente:
| Clave | Descripción |
|---|---|
USER_ID | The unique ID of the User. |
Requisitos previos
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.
Escriba el código
Añada lo siguiente a list-user-conversations.sh:
curl "https://api.nexmo.com/v1/users/$CONV_USER_ID/conversations" \
-H 'Authorization: Bearer '$JWT\
-H 'Content-Type: application/json'Ejecute su código
Guarde este archivo en su máquina y ejecútelo:
Requisitos previos
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-sdkCrea un archivo llamado list-user-conversations.js y añade el siguiente código:
const { Vonage } = require('@vonage/server-sdk');
const vonage = new Vonage({
applicationId: VONAGE_APPLICATION_ID,
privateKey: VONAGE_PRIVATE_KEY,
});Escriba el código
Añada lo siguiente a 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();Ejecute su código
Guarde este archivo en su máquina y ejecútelo:
Requisitos previos
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.
Añada lo siguiente a build.gradle:
implementation 'com.vonage:server-sdk:9.3.1'Crea un archivo llamado ListUserConversations y añade el siguiente código al método main:
VonageClient client = VonageClient.builder()
.applicationId(VONAGE_APPLICATION_ID)
.privateKeyPath(VONAGE_PRIVATE_KEY_PATH)
.build();Escriba el código
Añada lo siguiente al método main del archivo ListUserConversations:
var conversations = client.getConversationsClient().listUserConversations(USER_ID);
conversations.forEach(System.out::println);Ejecute su código
Podemos utilizar el plugin aplicación para Gradle para simplificar la ejecución de nuestra aplicación. Actualiza tu build.gradle con lo siguiente:
apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''Ejecute el siguiente comando gradle para ejecutar su aplicación, sustituyendo com.vonage.quickstart.conversation por el paquete que contiene ListUserConversations:
Requisitos previos
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 VonageCrea un archivo llamado GetUserConversations.cs y añade el siguiente código:
using System;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Vonage;
using Vonage.Conversations.GetUserConversations;
using Vonage.Request;Añada lo siguiente a GetUserConversations.cs:
var credentials = Credentials.FromAppIdAndPrivateKeyPath(VONAGE_APPLICATION_ID, VONAGE_PRIVATE_KEY_PATH);
var client = new VonageClient(credentials);Escriba el código
Añada lo siguiente a GetUserConversations.cs:
var response = await client.ConversationsClient.GetUserConversationsAsync(GetUserConversationsRequest.Build()
.WithUserId(USER_ID)
.Create());Requisitos previos
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/clientCrea un archivo llamado list-user-conversations.php y añade el siguiente código:
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);Ejecute su código
Guarde este archivo en su máquina y ejecútelo:
Pruébalo
Al ejecutar el código obtendrá una lista de Conversaciones asociadas al Usuario especificado.
En este caso el Usuario está asociado a una única Conversación:
[
{
"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"
}
]