Conectar os participantes a uma conferência

Este trecho de código mostra como unir várias chamadas em uma conversa.

Várias chamadas recebidas podem ser agrupadas em uma única conversa (teleconferência ) ao conectá-las à mesma teleconferência com nome específico.

Os nomes das conferências são definidos no âmbito do aplicativo Vonage. Por exemplo, tanto o VonageApp1 quanto o VonageApp2 poderiam ter uma conferência chamada vonage-conference e não haveria nenhum problema.

Exemplo

Substitua as seguintes variáveis no código de exemplo:

ChaveDescrição
VOICE_CONFERENCE_NAME

The named identifier for your conference.

Pré-requisitos

npm install express body-parser

Escreva o código

Adicione o seguinte ao arquivo ` conference-call.js`:

const Express = require('express');
const bodyParser = require('body-parser');

const app = new Express();
app.use(bodyParser.json());

const onInboundCall = (_, response) => {
  const ncco = [
    {
      action: 'talk',
      text: 'Please wait while we connect you to the conference',
    },
    {
      action: 'conversation',
      name: VOICE_CONF_NAME,
    },
  ];

  response.json(ncco);
};

app.get('/webhooks/answer', onInboundCall);

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`);
});

Ver código-fonte completo

Execute seu código

Salve este arquivo no seu computador e execute-o:

node conference-call.js

Pré-requisitos

Adicione o seguinte ao arquivo ` build.gradle`:

implementation 'com.vonage:server-sdk-kotlin:2.1.1'
implementation 'io.ktor:ktor-server-netty'
implementation 'io.ktor:ktor-serialization-jackson'

Escreva o código

Adicione o seguinte ao método ` main ` do arquivo ` ConnectCallersToConference `:

embeddedServer(Netty, port = 8000) {
    routing {
        route("/webhooks/answer") {
            handle {
                call.response.header("Content-Type", "application/json")
                call.respond(
                    Ncco(
                        talkAction("Please wait while we connect you to the conference."),
                        conversationAction(VOICE_CONFERENCE_NAME)
                    ).toJson()
                )
            }
        }
    }
}.start(wait = true)

Ver código-fonte completo

Execute seu código

Podemos usar o plugin “ aplicativo ” para o Gradle a fim de simplificar a execução do nosso aplicativo. Atualize seu arquivo ` build.gradle ` com o seguinte:

apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''

Execute o seguinte comando ` gradle ` para rodar seu aplicativo, substituindo ` com.vonage.quickstart.kt.voice ` pelo pacote que contém ` ConnectCallersToConference`:

gradle run -Pmain=com.vonage.quickstart.kt.voice.ConnectCallersToConference

Pré-requisitos

Adicione o seguinte ao arquivo ` build.gradle`:

implementation 'com.vonage:server-sdk:9.3.1'
implementation 'com.sparkjava:spark-core:2.9.4'

Escreva o código

Adicione o seguinte ao método ` main ` do arquivo ` ConferenceCall `:

final String CONF_NAME = "my-conference";

/*
 * Route to answer incoming calls with an NCCO response.
 */
Route answerRoute = (req, res) -> {
    TalkAction intro = TalkAction.builder("Please wait while we connect you to the conference.").build();
    ConversationAction conversation = ConversationAction.builder(CONF_NAME).build();

    res.type("application/json");

    return new Ncco(intro, conversation).toJson();
};

Spark.port(3000);
Spark.get("/webhooks/answer", answerRoute);
Spark.post("/webhooks/answer", answerRoute);

Ver código-fonte completo

Execute seu código

Podemos usar o plugin “ aplicativo ” para o Gradle a fim de simplificar a execução do nosso aplicativo. Atualize seu arquivo ` build.gradle ` com o seguinte:

apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''

Execute o seguinte comando ` gradle ` para rodar seu aplicativo, substituindo ` com.vonage.quickstart.voice ` pelo pacote que contém ` ConferenceCall`:

gradle run -Pmain=com.vonage.quickstart.voice.ConferenceCall

Pré-requisitos

Install-Package Vonage

Escreva o código

Adicione o seguinte ao arquivo ` ConnectCallersToConferenceController.cs`:

[HttpGet("webhooks/answer")]
public string Answer()
{
    var VOICE_CONFERENCE_NAME = Environment.GetEnvironmentVariable("VOICE_CONFERENCE_NAME") ?? "VOICE_CONFERENCE_NAME";
    var talkAction = new TalkAction() { Text = "Please wait while we connect you to the conference" };
    var conversationAction = new ConversationAction() { Name = VOICE_CONFERENCE_NAME };
    var ncco = new Ncco(talkAction, conversationAction);
    return ncco.ToString();
}

Ver código-fonte completo

Pré-requisitos

composer require vonage/client slim/slim:^3.8

Escreva o código

Adicione o seguinte ao arquivo ` index.php`:

require 'vendor/autoload.php';

$app = new \Slim\App;

$app->get('/webhooks/answer', function (Request $request, Response $response) {
    $talk = new \Vonage\Voice\NCCO\Action\Talk('Hi, welcome to this Nexmo conference call');
    $convo = new \Vonage\Voice\NCCO\Action\Conversation('nexmo-conference-standard');

    $ncco = new \Vonage\Voice\NCCO\NCCO();
    $ncco->addAction($talk);
    $ncco->addAction($convo);

    return new JsonResponse($ncco->toArray());
});

$app->run();

Ver código-fonte completo

Execute seu código

Salve este arquivo no seu computador e execute-o:

php -S localhost:3000 -t .

Pré-requisitos

pip install vonage python-dotenv fastapi[standard]

Escreva o código

Adicione o seguinte ao arquivo ` connect-callers-to-a-conference.py`:

import os
from os.path import dirname, join

from dotenv import load_dotenv
from fastapi import FastAPI
from vonage_voice import Conversation, NccoAction, Talk

dotenv_path = join(dirname(__file__), '../.env')
load_dotenv(dotenv_path)

VOICE_CONFERENCE_NAME = os.environ.get("VOICE_CONFERENCE_NAME")

app = FastAPI()


@app.get('/webhooks/answer')
async def answer_call():
    ncco: list[NccoAction] = [
        Talk(text="Please wait while we connect you to the conference"),
        Conversation(name=VOICE_CONFERENCE_NAME),
    ]

    return [action.model_dump(by_alias=True, exclude_none=True) for action in ncco]

Ver código-fonte completo

Execute seu código

Salve este arquivo no seu computador e execute-o:

fastapi dev voice/connect-callers-to-a-conference.py

Pré-requisitos

gem install sinatra sinatra-contrib

Escreva o código

Adicione o seguinte ao arquivo ` join_a_conference_call.rb`:

require 'sinatra'
require 'sinatra/multi_route'
require 'json'

VOICE_CONFERENCE_NAME = ENV['VOICE_CONFERENCE_NAME']

before do
  content_type :json
end

route :get, :post, '/webhooks/answer' do
  [
    {
      action: 'talk',
      text: 'Please wait while we connect you to the conference'
    },
    {
      action: 'conversation',
      name: VOICE_CONFERENCE_NAME
    }
  ].to_json
end

set :port, 3000

Ver código-fonte completo

Execute seu código

Salve este arquivo no seu computador e execute-o:

ruby join_a_conference_call.rb

Experimente

Inicie seu servidor e faça várias chamadas recebidas para o número da Vonage atribuído a este aplicativo da Vonage. As chamadas recebidas serão conectadas à mesma conversa (conferência).

Leitura complementar

  • Teleconferência - Este guia explica os dois Concepts que a Vonage associa a uma chamada: “leg” e “conversação”.