Crear un usuario

En este fragmento de código aprenderás a crear un Usuario.

Ejemplo

Asegúrate de que las siguientes variables estén configuradas con los valores que necesites, utilizando cualquier método que te resulte más cómodo:

ClaveDescripción
USER_NAME

The unique name of the User.

USER_DISPLAY_NAME

The display name of the User.

Requisitos previos

You will need to use an existing Application in order to be able to create a User. See the Create Conversation code snippet for information on how to create an Application.

Escriba el código

Añada lo siguiente a create-user.sh:

curl -X "POST" "https://api.nexmo.com/v1/users" \
     -H 'Authorization: Bearer '$JWT\
     -H 'Content-Type: application/json' \
     -d $'{
  "name": "'$USER_NAME'",
  "display_name": "'$USER_DISPLAY_NAME'"
}'

Ver fuente completa

Ejecute su código

Guarde este archivo en su máquina y ejecútelo:

bash create-user.sh

Requisitos previos

You will need to use an existing Application in order to be able to create a User. See the Create Conversation code snippet for information on how to create an Application.
npm install @vonage/server-sdk

Crea un archivo llamado create-user.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,
});

Ver fuente completa

Escriba el código

Añada lo siguiente a create-user.js:

vonage.users.createUser({
  'name': USER_NAME,
  'displayName': USER_DISPLAY_NAME,
})
  .then((user) => console.log(user))
  .catch((error) => console.error(error));

Ver fuente completa

Ejecute su código

Guarde este archivo en su máquina y ejecútelo:

node create-user.js

Requisitos previos

You will need to use an existing Application in order to be able to create a User. See the Create Conversation code snippet for information on how to create an Application.

Añada lo siguiente a build.gradle:

implementation 'com.vonage:server-sdk-kotlin:2.1.1'

Crea un archivo llamado CreateUser y añade el siguiente código al método main:

val client = Vonage {
    applicationId(VONAGE_APPLICATION_ID)
    privateKeyPath(VONAGE_PRIVATE_KEY_PATH)
}

Ver fuente completa

Escriba el código

Añada lo siguiente al método main del archivo CreateUser:

val user = client.users.create {
    name(USER_NAME)
    displayName(USER_DISPLAY_NAME)
    imageUrl(MESSAGES_IMAGE_URL)
    channels(
        Pstn(MESSAGES_TO_NUMBER),
        Sms(MESSAGES_TO_NUMBER),
        Viber(MESSAGES_TO_NUMBER),
        Whatsapp(MESSAGES_TO_NUMBER),
        Viber(MESSAGES_TO_NUMBER),
        Messenger(MESSENGER_RECIPIENT_ID),
        Vbc(VBC_EXTENSION),
        Sip(SIP_SECURE_URI, SIP_USERNAME, SIP_PASSWORD),
        Websocket(WEBSOCKET_URI)
    )
}

Ver fuente completa

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.kt.users por el paquete que contiene CreateUser:

gradle run -Pmain=com.vonage.quickstart.kt.users.CreateUser

Requisitos previos

You will need to use an existing Application in order to be able to create a User. See the Create Conversation code snippet for information on how to create an Application.

Añada lo siguiente a build.gradle:

implementation 'com.vonage:server-sdk:9.3.1'

Crea un archivo llamado CreateUser y añade el siguiente código al método main:

VonageClient client = VonageClient.builder()
        .apiKey(VONAGE_API_KEY)
        .apiSecret(VONAGE_API_SECRET)
        .build();

Ver fuente completa

Escriba el código

Añada lo siguiente al método main del archivo CreateUser:

User user = client.getUsersClient().createUser(
    User.builder()
        .name(USER_NAME)
        .displayName(USER_DISPLAY_NAME)
        .imageUrl("https://example.com/profile.jpg")
        .channels(
            new Pstn("448001234567"),
            new Sms("447700900000"),
            new Viber("447700900000"),
            new Whatsapp("447700900000"),
            new Viber("447700900000"),
            new Messenger("12345abcd"),
            new Vbc(123),
            new Sip("sip:4442138907@sip.example.com;transport=tls", "myUserName", "P@ssw0rd"),
            new Websocket("wss://example.com/socket")
        )
        .build()
);

Ver fuente completa

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.users por el paquete que contiene CreateUser:

gradle run -Pmain=com.vonage.quickstart.users.CreateUser

Requisitos previos

You will need to use an existing Application in order to be able to create a User. See the Create Conversation code snippet for information on how to create an Application.
Install-Package Vonage

Crea un archivo llamado CreateUser.cs y añade el siguiente código:

using System;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Vonage;
using Vonage.Request;
using Vonage.Users.CreateUser;

Ver fuente completa

Añada lo siguiente a CreateUser.cs:

var credentials = Credentials.FromAppIdAndPrivateKeyPath(VONAGE_APPLICATION_ID, VONAGE_PRIVATE_KEY_PATH);
var client = new VonageClient(credentials);

Ver fuente completa

Escriba el código

Añada lo siguiente a CreateUser.cs:

var response = await client.UsersClient.CreateUserAsync(CreateUserRequest.Build()
    .WithName(USER_NAME)
    .WithDisplayName(USER_DISPLAY_NAME)
    .Create());

Ver fuente completa

Requisitos previos

You will need to use an existing Application in order to be able to create a User. See the Create Conversation code snippet for information on how to create an Application.
pip install vonage python-dotenv

Escriba el código

Añada lo siguiente a create-user.py:

from vonage import Auth, Vonage
from vonage_users import Channels, PstnChannel, User

client = Vonage(
    Auth(
        application_id=VONAGE_APPLICATION_ID,
        private_key=VONAGE_PRIVATE_KEY,
    )
)

user_options = User(
    name=USER_NAME,
    display_name=USER_DISPLAY_NAME,
    channels=Channels(pstn=[PstnChannel(number=123456)]),
)
user = client.users.create_user(user_options)

print(user)

Ver fuente completa

Ejecute su código

Guarde este archivo en su máquina y ejecútelo:

python users/create-user.py

Pruébalo

Al ejecutar el código, crearás un nuevo usuario.