Premier appel sortant

serverCall() lance un appel sortant en envoyant le contexte de l'appel à l'API Vonage. L'API Vonage déclenche alors votre webhook de réponse en lui transmettant ce contexte sous la forme custom_data, et votre webhook renvoie un NCCO qui détermine l'acheminement.

Flux sortant

  1. Appels de l'application cliente serverCall(context).
  2. L'API Vonage déclenche votre webhook de réponse en lui transmettant le contexte suivant : custom_data.
  3. Votre webhook renvoie « NCCO ».
  4. L'API Vonage relie le contenu multimédia au destinataire de l'appel.

Extrait du client

const context = {
  to: 'sip:alice@example.com',
  from: 'agent-123'
};

try {
  const callId = await client.serverCall(context);
  console.log(`Outbound call started, callId: ${callId}`);
} catch (error) {
  console.error('Failed to start outbound call', error);
}
 val context = mapOf(
     "to" to "sip:alice@example.com",
     "from" to "agent-123"
 )

 client.serverCall(context) { error, callId ->
     if (error != null) {
         println("Failed to start outbound call: $error")
         return@serverCall
     }

     println("Outbound call started, callId: $callId")
 }
 HashMap<String, String> context = new HashMap<>();
 context.put("to", "sip:alice@example.com");
 context.put("from", "agent-123");

 client.serverCall(context, (error, callId) -> {
     if (error != null) {
         System.out.println("Failed to start outbound call: " + error);
         return null;
     }

     System.out.println("Outbound call started, callId: " + callId);
     return null;
 });
 let context = [
     "to": "sip:alice@example.com",
     "from": "agent-123"
 ]

 client.serverCall(context) { error, callId in
     guard error == nil, let callId = callId else {
         print("Failed to start outbound call: \(error?.localizedDescription ?? "unknown")")
         return
     }

     print("Outbound call started, callId: \(callId)")
 }
 NSDictionary *context = @{
     @"to": @"sip:alice@example.com",
     @"from": @"agent-123"
 };

 [client serverCall:context callback:^(NSError * _Nullable error, VGCallId _Nullable callId) {
     if (error != nil || callId == nil) {
         NSLog(@"Failed to start outbound call: %@", error);
         return;
     }

     NSLog(@"Outbound call started, callId: %@", callId);
 }];

Webhook de réponse du serveur

L'exemple ci-dessous illustre le format requis pour la réponse du webhook. Votre implémentation concrète variera en fonction du langage et du framework utilisés pour votre backend.

Votre webhook de réponse doit être accessible au public et renvoyer un NCCO valide.

Utilisation "type": "app" dans le point de terminaison « connect » pour acheminer l'appel vers un Client SDK en fonction du nom d'utilisateur. Utilisez "type": "phone" pour acheminer l'appel vers un numéro du réseau public commuté (PSTN) à la place. Consultez la Types de points d'extrémité NCCO Connect pour toutes les options.

const customData =
  (req.body.custom_data as { to?: string } | undefined) ?? {};
const destination = customData.to ?? 'callee-username';

const answerNcco = [
  {
    action: 'connect',
    endpoint: [
      {
        type: 'app',
        user: destination
      }
    ]
  }
];

res.json(answerNcco);

Référence NCCO : ici

Traitement des erreurs

serverCall peut échouer s'il n'y a pas de session active, si l'établissement de la communication expire ou si l'API Vonage rencontre un problème avec votre webhook. Traitez toujours l'erreur :

try {
  const callId = await client.serverCall({ context });
  console.log("Call started:", callId);
} catch (error) {
  console.error("Call failed:", error.type, error.message);
}
client.serverCall(context) { error, callId ->
    if (error != null) {
        println("Call failed: ${error.message}")
        // Common causes:
        // - No active session (call createSession first)
        // - Network timeout
        // - Answer webhook returned invalid NCCO
        return@serverCall
    }
    println("Call started: $callId")
}
client.serverCall(context) { error, callId in
    if let error = error {
        print("Call failed: \(error.localizedDescription)")
        return
    }
    print("Call started: \(callId ?? "")")
}

Pour obtenir la liste complète des types d'erreurs possibles, consultez Erreurs.