Swift

Recevoir un appel

En haut de la page ViewController sous la classe client ajoutez une propriété de type "string" qui contient une référence à tout appel en cours.

class ViewController: UIViewController {
    
    let connectionStatusLabel = UILabel()
    let client = VGVoiceClient()
    var callID: String?
    ...
}

Lorsque l'application reçoit un appel, vous devez lui donner la possibilité d'accepter ou de rejeter l'appel. Pour ce faire, ajoutez l'élément displayIncomingCallAlert à la fonction ViewController classe.

class ViewController: UIViewController {
    ...
    func displayIncomingCallAlert(callID: String, caller: String) {
        let alert = UIAlertController(title: "Incoming call from", message: caller, preferredStyle: .alert)
        
        alert.addAction(UIAlertAction(title: "Answer", style: .default, handler: { _ in
            self.client.answer(callID) { error in
                if error == nil {
                    self.callID = callID
                }
            }
        }))
        
        alert.addAction(UIAlertAction(title: "Reject", style: .destructive, handler: { _ in
            self.client.reject(callID) { error in
                if let error {
                    self.connectionStatusLabel.text = error.localizedDescription
                }
            }
        }))
        
        self.present(alert, animated: true, completion: nil)
    }
}

Les displayIncomingCallAlert prend comme paramètres l'identifiant de l'appel et l'appelant. Notez que dans la fonction UIAlertAction pour avoir répondu à l'invitation. Si l'invitation est acceptée, vous attribuerez à la personne qui a répondu à l'invitation la mention callID au bien de l'année précédente

Pour utiliser displayIncomingCallAlert vous devez utiliser le VGVoiceClientDelegate qui possède une fonction qui sera appelée lorsque le client recevra un message VGVoiceInvite.

extension ViewController: VGVoiceClientDelegate {
  ...

  func voiceClient(_ client: VGVoiceClient, didReceiveInviteForCall callId: String, from caller: String, withChannelType type: VGVoiceChannelType) {
      DispatchQueue.main.async { [weak self] in
          self?.displayIncomingCallAlert(callID: callId, caller: caller)
      }
  }
  
  func voiceClient(_ client: VGVoiceClient, didReceiveInviteCancelForCall callId: String, with reason: VGVoiceInviteCancelReason) {
      DispatchQueue.main.async { [weak self] in
          self?.dismiss(animated: true)
      }
  }
  
  func voiceClient(_ client: VGVoiceClient, didReceiveHangupForCall callId: String, withQuality callQuality: VGRTCQuality, reason: VGHangupReason) {
      DispatchQueue.main.async { [weak self] in
          self?.callID = nil
          self?.connectionStatusLabel.text = "Call Ended"
      }
  }
}

Vous pouvez également mettre en œuvre la fonction didReceiveHangupForCall qui est appelée si l'appel est raccroché.

NOTE : Assurez-vous également que le serveur webhook que vous avez construit dans les étapes précédentes est toujours en cours d'exécution.

Presse Cmd + R pour construire et exécuter à nouveau, lorsque vous appelez le numéro lié à votre application de tout à l'heure, une alerte vous est présentée. Vous pouvez décrocher et l'appel sera connecté !

Incoming call alert

Crochets Web

Pendant que vous procédez à l'appel, veuillez basculer vers le terminal et remarquer l'icône de l'appel. /voice/answer appelé pour récupérer le NCCO :

NCCO request:
  - caller: 441234567890

En outre, au fur et à mesure que l'appel passe par différentes étapes, /voice/event est envoyé des événements :

EVENT:
{
  headers: {},
  from: '441234567890',
  to: '442038297050',
  uuid: '0779a56d002f1c7f47f82ef5fe84ab79',
  conversation_uuid: 'CON-8f5a100c-fbce-4218-8d4b-16341335bcd6',
  status: 'ringing',
  direction: 'inbound',
  timestamp: '2021-03-29T21:20:05.582Z'
}
---
EVENT:
{
  headers: {},
  from: '441234567890',
  to: '442038297050',
  uuid: '0779a56d002f1c7f47f82ef5fe84ab79',
  conversation_uuid: 'CON-8f5a100c-fbce-4218-8d4b-16341335bcd6',
  status: 'started',
  direction: 'inbound',
  timestamp: '2021-03-29T21:20:05.582Z'
}
---
EVENT:
{
  start_time: null,
  headers: {},
  rate: null,
  from: '441234567890',
  to: '442038297050',
  uuid: '0779a56d002f1c7f47f82ef5fe84ab79',
  conversation_uuid: 'CON-8f5a100c-fbce-4218-8d4b-16341335bcd6',
  status: 'answered',
  direction: 'inbound',
  network: null,
  timestamp: '2021-03-29T21:20:06.182Z'
}
---
EVENT:
{
  from: '441234567890',
  to: 'Alice',
  uuid: '944bf4bf-8dc7-4e23-86b2-2f4234777416',
  conversation_uuid: 'CON-8f5a100c-fbce-4218-8d4b-16341335bcd6',
  status: 'started',
  direction: 'outbound',
  timestamp: '2021-03-29T21:20:13.025Z'
}
---
EVENT:
{
  start_time: null,
  headers: {},
  rate: null,
  from: '441234567890',
  to: 'Alice',
  uuid: '944bf4bf-8dc7-4e23-86b2-2f4234777416',
  conversation_uuid: 'CON-8f5a100c-fbce-4218-8d4b-16341335bcd6',
  status: 'answered',
  direction: 'outbound',
  network: null,
  timestamp: '2021-03-29T21:20:13.025Z'
}
---
EVENT:
{
  headers: {},
  end_time: '2021-03-29T21:20:16.000Z',
  uuid: '944bf4bf-8dc7-4e23-86b2-2f4234777416',
  network: null,
  duration: '5',
  start_time: '2021-03-29T21:20:11.000Z',
  rate: '0.00',
  price: '0',
  from: '441234567890',
  to: 'Alice',
  conversation_uuid: 'CON-8f5a100c-fbce-4218-8d4b-16341335bcd6',
  status: 'completed',
  direction: 'outbound',
  timestamp: '2021-03-29T21:20:17.574Z'
}
---
EVENT:
{
  headers: {},
  end_time: '2021-03-29T21:20:18.000Z',
  uuid: '0779a56d002f1c7f47f82ef5fe84ab79',
  network: 'GB-FIXED',
  duration: '12',
  start_time: '2021-03-29T21:20:06.000Z',
  rate: '0.00720000',
  price: '0.00144000',
  from: ' 441234567890',
  to: '442038297050',
  conversation_uuid: 'CON-8f5a100c-fbce-4218-8d4b-16341335bcd6',
  status: 'completed',
  direction: 'inbound',
  timestamp: '2021-03-29T21:20:17.514Z'
}
---