Criar um aplicativo

Neste trecho de código, você verá como criar uma Application.

Exemplo

Você precisará garantir que os seguintes valores substituíveis sejam definidos no código de exemplo, utilizando qualquer método que lhe seja conveniente:

ChaveDescrição
VONAGE_API_KEY

Your Vonage API key (see it on your dashboard).

VONAGE_API_SECRET

Your Vonage API secret (also available on your dashboard).

Escreva o código

Adicione o seguinte ao arquivo ` create-application.sh`:

curl -X "POST" "https://api.nexmo.com/v2/applications" \
     -H 'Content-Type: application/json' \
     -u "$VONAGE_API_KEY:$VONAGE_API_SECRET" \
     -d $'{
  "name": "Code Snippets V2 Application",
  "capabilities": {
    "messages": {
      "webhooks": {
        "inbound_url": {
          "address": "https://example.com/webhooks/inbound",
          "http_method": "POST"
        },
        "status_url": {
          "address": "https://example.com/webhooks/status",
          "http_method": "POST"
        }
      }
    },
    "voice": {
      "webhooks": {
        "answer_url": {
          "address": "https://example.com/webhooks/answer",
          "http_method": "GET"
        },
        "fallback_answer_url": {
          "address": "https://fallback.example.com/webhooks/answer",
          "http_method": "GET"
        },
        "event_url": {
          "address": "https://example.com/webhooks/event",
          "http_method": "POST"
        }
      }
    },
    "rtc": {
      "webhooks": {
        "event_url": {
          "address": "https://example.com/webhooks/event",
          "http_method": "POST"
        }
      }
    },
    "vbc": {},
    "verify": {
      "webhooks": {

Ver código-fonte completo

Execute seu código

Salve este arquivo no seu computador e execute-o:

./create-application.sh

Pré-requisitos

npm install @vonage/server-sdk

Crie um arquivo chamado ` create-application.js ` e insira o seguinte código:

const { Vonage } = require('@vonage/server-sdk');

const vonage = new Vonage({
  apiKey: VONAGE_API_KEY,
  apiSecret: VONAGE_API_SECRET,
});

Ver código-fonte completo

Escreva o código

Adicione o seguinte ao arquivo ` create-application.js`:

vonage.applications.createApplication({
  name: APPLICATION_NAME,
  capabilities: {
    voice: {
      webhooks: {
        answer_url: {
          address: 'https://example.com/webhooks/answer',
          http_method: 'GET',
        },
        event_url: {
          address: 'https://example.com/webhooks/event',
          http_method: 'POST',
        },
      },
    },
    messages: {
      webhooks: {
        inbound_url: {
          address: 'https://example.com/webhooks/inbound',
          http_method: 'POST',
        },
        status_url: {
          address: 'https://example.com/webhooks/status',
          http_method: 'POST',
        },
      },
    },
    rtc: {
      webhooks: {
        event_url: {
          address: 'https://example.com/webhooks/rtcevent',
          http_method: 'POST',
        },
      },
    },
  },
})
  .then((app) => console.log(app))
  .catch((error) => console.error(error));

Ver código-fonte completo

Execute seu código

Salve este arquivo no seu computador e execute-o:

node create-application.js

Pré-requisitos

Adicione o seguinte ao arquivo ` build.gradle`:

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

Crie um arquivo chamado ` CreateApplication ` e adicione o código a seguir ao método ` main `:

val client = Vonage {
    apiKey(VONAGE_API_KEY)
    apiSecret(VONAGE_API_SECRET)
}

Ver código-fonte completo

Escreva o código

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

val application = client.application.create {
    name(APPLICATION_NAME)
    messages {
        inbound {
            address("https://example.com/webhooks/inbound")
            method(HttpMethod.POST)
        }
        status {
            address("https://example.com/webhooks/status")
            method(HttpMethod.POST)
        }
    }
    voice {
        answer {
            address("https://example.com/webhooks/answer")
            method(HttpMethod.GET)
        }
        fallbackAnswer {
            address("https://fallback.example.com/webhooks/answer")
            method(HttpMethod.GET)
        }
        event {
            address("https://example.com/webhooks/event")
            method(HttpMethod.POST)
        }
    }
    rtc {
        event {
            address("https://example.com/webhooks/event")
            method(HttpMethod.POST)
        }
    }
    verify {
        status {
            address("https://example.com/webhooks/verify")
            method(HttpMethod.POST)
        }
    }
    vbc()
}

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.application ` pelo pacote que contém ` CreateApplication`:

gradle run -Pmain=com.vonage.quickstart.kt.application.CreateApplication

Pré-requisitos

Adicione o seguinte ao arquivo ` build.gradle`:

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

Crie um arquivo chamado ` CreateApplication ` e adicione o código a seguir ao método ` main `:

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

Ver código-fonte completo

Escreva o código

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

Application application = client.getApplicationClient().createApplication(
    Application.builder()
        .name(APPLICATION_NAME)
        .addCapability(Messages.builder()
                .inbound(Webhook.builder()
                        .address("https://example.com/webhooks/inbound")
                        .method(HttpMethod.POST)
                        .build()
                )
                .status(Webhook.builder()
                        .address("https://example.com/webhooks/status")
                        .method(HttpMethod.POST)
                        .build()
                )
                .build()
        )
        .build()
);

System.out.println("Application Created:");
System.out.println(application.toJson());

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.application ` pelo pacote que contém ` CreateApplication`:

gradle run -Pmain=com.vonage.quickstart.application.CreateApplication

Pré-requisitos

Install-Package Vonage

Crie um arquivo chamado ` CreateApplication.cs ` e insira o seguinte código:

using Vonage;
using Vonage.Applications;
using Vonage.Applications.Capabilities;
using Vonage.Request;

Ver código-fonte completo

Adicione o seguinte ao arquivo ` CreateApplication.cs`:

var credentials = Credentials.FromApiKeyAndSecret(VONAGE_API_KEY, VONAGE_API_SECRET);
var client = new VonageClient(credentials);

Ver código-fonte completo

Escreva o código

Adicione o seguinte ao arquivo ` CreateApplication.cs`:

var request = new CreateApplicationRequest
{
    Name = "Code Snippets V2 Application",
    Capabilities = new ApplicationCapabilities
    {
        Messages = Vonage.Applications.Capabilities.Messages.Build()
            .WithInboundUrl("https://example.com/webhooks/inbound")
            .WithStatusUrl("https://example.com/webhooks/status"),
        Voice = Vonage.Applications.Capabilities.Voice.Build()
            .WithAnswerUrl("https://example.com/webhooks/answer", WebhookHttpMethod.Get)
            .WithEventUrl("https://example.com/webhooks/event", WebhookHttpMethod.Post)
            .WithFallbackAnswerUrl("https://fallback.example.com/webhooks/answer", WebhookHttpMethod.Get),
        Rtc = Rtc.Build().WithEventUrl("https://example.com/webhooks/event", WebhookHttpMethod.Post)
    }
};
var response = await client.ApplicationClient.CreateApplicationAsync(request);

Ver código-fonte completo

Pré-requisitos

composer require vonage/client

Crie um arquivo chamado ` create-application.php ` e insira o seguinte código:

$basic  = new \Vonage\Client\Credentials\Basic(VONAGE_API_KEY, VONAGE_API_SECRET);
$client = new \Vonage\Client(new \Vonage\Client\Credentials\Container($basic));

Ver código-fonte completo

Escreva o código

Adicione o seguinte ao arquivo ` create-application.php`:

try {
    $application = new Nexmo\Application\Application();
    $application->fromArray([
        'name' => 'Should Work2',
        'capabilities' => [
            'voice' => [
                'webhooks' => [
                    'answer_url' => [
                        'address' => "https://example.com/webhooks/answer",
                        'http_method' => "GET"
                    ],
                    'event_url' => [
                        'address' => "https://example.com/webhooks/event",
                        'http_method' => "POST"
                    ]
                ]
            ],
            'messages' => [
                'webhooks' => [
                    'inbound_url' => [
                        'address' => "https://example.com/webhooks/inbound",
                        'http_method' => "POST"
                    ],
                    'status_url' => [
                        'address' => "https://example.com/webhooks/status",
                        'http_method' => "POST"
                    ]
                ]
            ],
            'rtc' => [
                'webhooks' => [
                    'event_url' => [
                        'address' => "https://example.com/webhooks/rtcevent",
                        'http_method' => "POST"
                    ]
                ]
            ]
        ]
    ]);
    $application = $client->applications()->create($application);

    echo $application->getId() . PHP_EOL;
    echo $application->getName() . PHP_EOL;
} catch (\InvalidArgumentException $e) {
    echo $e->getMessage() . PHP_EOL;
}

Ver código-fonte completo

Execute seu código

Salve este arquivo no seu computador e execute-o:

php create-application.php

Pré-requisitos

pip install vonage python-dotenv

Escreva o código

Adicione o seguinte ao arquivo ` create-application.py`:

from vonage import Auth, Vonage
from vonage_application import (ApplicationConfig, ApplicationData,
                                ApplicationUrl, Capabilities, Messages,
                                MessagesWebhooks, Region, Verify,
                                VerifyWebhooks, Voice, VoiceUrl, VoiceWebhooks)

client = Vonage(Auth(api_key=VONAGE_API_KEY, api_secret=VONAGE_API_SECRET))

# Voice application options
voice = Voice(
    webhooks=VoiceWebhooks(
        answer_url=VoiceUrl(
            address='https://example.com/answer',
            http_method='POST',
            connect_timeout=500,
            socket_timeout=3000,
        ),
        fallback_answer_url=VoiceUrl(
            address='https://example.com/fallback',
            http_method='POST',
            connect_timeout=500,
            socket_timeout=3000,
        ),
        event_url=VoiceUrl(
            address='https://example.com/event',
            http_method='POST',
            connect_timeout=500,
            socket_timeout=3000,
        ),
    ),
    signed_callbacks=True,
    conversations_ttl=8000,
    leg_persistence_time=14,
    region=Region.NA_EAST,
)

# Messages application options
messages = Messages(
    version='v1',
    webhooks=MessagesWebhooks(
        inbound_url=ApplicationUrl(
            address='https://example.com/inbound', http_method='POST'
        ),
        status_url=ApplicationUrl(
            address='https://example.com/status', http_method='POST'
        ),
    ),
    authenticate_inbound_media=True,
)

# Verify application options
verify = Verify(
    webhooks=VerifyWebhooks(
        status_url=ApplicationUrl(address='https://example.com/status', http_method='GET')
    ),
)

# Set the application capabilities
capabilities = Capabilities(voice=voice, messages=messages, verify=verify)

# Set the application configuration that will be applied
params = ApplicationConfig(
    name='My Custom Application',
    capabilities=capabilities,
)

# Call the API
response: ApplicationData = client.application.create_application(params)

print(response)

Ver código-fonte completo

Execute seu código

Salve este arquivo no seu computador e execute-o:

python application/create-application.py

Pré-requisitos

gem install vonage

Crie um arquivo chamado ` create-application.rb ` e insira o seguinte código:

client = Vonage::Client.new(
  api_key: VONAGE_API_KEY,
  api_secret: VONAGE_API_SECRET
)

Ver código-fonte completo

Escreva o código

Adicione o seguinte ao arquivo ` create-application.rb`:

response = client.applications.create(
  name: 'Code Example App',
  capabilities: {
    'messages': {
      'webhooks': {
        'inbound_url': {
          'address': 'https://example.com/webhooks/inbound',
          'http_method': 'POST'
        },
        'status_url': {
          'address': 'https://example.com/webhooks/status',
          'http_method': 'POST'
        }
      }
    }
  }
)

Ver código-fonte completo

Execute seu código

Salve este arquivo no seu computador e execute-o:

ruby create-application.rb