Créer un utilisateur
Dans cet extrait de code, vous apprendrez à créer un utilisateur.
Exemple
Veillez à ce que les variables suivantes soient réglées sur les valeurs souhaitées en utilisant toute méthode appropriée :
| Clé | Description |
|---|---|
USER_NAME | The unique name of the User. |
USER_DISPLAY_NAME | The display name of the User. |
Conditions préalables
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.
Rédiger le code
Ajouter ce qui suit à 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'"
}'Exécutez votre code
Enregistrez ce fichier sur votre machine et exécutez-le :
Conditions préalables
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-sdkCréez un fichier nommé create-user.js et ajoutez le code suivant :
const { Vonage } = require('@vonage/server-sdk');
const vonage = new Vonage({
applicationId: VONAGE_APPLICATION_ID,
privateKey: VONAGE_PRIVATE_KEY,
});Rédiger le code
Ajouter ce qui suit à create-user.js:
vonage.users.createUser({
'name': USER_NAME,
'displayName': USER_DISPLAY_NAME,
})
.then((user) => console.log(user))
.catch((error) => console.error(error));Exécutez votre code
Enregistrez ce fichier sur votre machine et exécutez-le :
Conditions préalables
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.
Ajouter ce qui suit à build.gradle:
implementation 'com.vonage:server-sdk-kotlin:2.1.1'Créez un fichier nommé CreateUser et ajoutez le code suivant à la méthode main:
val client = Vonage {
applicationId(VONAGE_APPLICATION_ID)
privateKeyPath(VONAGE_PRIVATE_KEY_PATH)
}Rédiger le code
Ajouter ce qui suit à la méthode main du fichier 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)
)
}Exécutez votre code
Nous pouvons utiliser le plugin Applications pour Gradle afin de simplifier l'exécution de notre application. Mettez à jour votre build.gradle avec ce qui suit :
apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''Exécutez la commande gradle suivante pour exécuter votre application, en remplaçant com.vonage.quickstart.kt.users par le paquet contenant CreateUser:
Conditions préalables
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.
Ajouter ce qui suit à build.gradle:
implementation 'com.vonage:server-sdk:9.3.1'Créez un fichier nommé CreateUser et ajoutez le code suivant à la méthode main:
VonageClient client = VonageClient.builder()
.apiKey(VONAGE_API_KEY)
.apiSecret(VONAGE_API_SECRET)
.build();Rédiger le code
Ajouter ce qui suit à la méthode main du fichier 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()
);Exécutez votre code
Nous pouvons utiliser le plugin Applications pour Gradle afin de simplifier l'exécution de notre application. Mettez à jour votre build.gradle avec ce qui suit :
apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''Exécutez la commande gradle suivante pour exécuter votre application, en remplaçant com.vonage.quickstart.users par le paquet contenant CreateUser:
Conditions préalables
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 VonageCréez un fichier nommé CreateUser.cs et ajoutez le code suivant :
using System;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Vonage;
using Vonage.Request;
using Vonage.Users.CreateUser;Ajouter ce qui suit à CreateUser.cs:
var credentials = Credentials.FromAppIdAndPrivateKeyPath(VONAGE_APPLICATION_ID, VONAGE_PRIVATE_KEY_PATH);
var client = new VonageClient(credentials);Rédiger le code
Ajouter ce qui suit à CreateUser.cs:
var response = await client.UsersClient.CreateUserAsync(CreateUserRequest.Build()
.WithName(USER_NAME)
.WithDisplayName(USER_DISPLAY_NAME)
.Create());Conditions préalables
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-dotenvRédiger le code
Ajouter ce qui suit à 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)Exécutez votre code
Enregistrez ce fichier sur votre machine et exécutez-le :
Essayez-le
Lorsque vous exécutez le code, vous créez un nouvel utilisateur.