Créer une application

Dans cet extrait de code, vous verrez comment créer une Applications.

Exemple

Vous devrez vous assurer que les valeurs remplaçables suivantes sont définies dans le code de l'exemple à l'aide d'une méthode appropriée :

CléDescription
VONAGE_API_KEY

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

VONAGE_API_SECRET

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

Rédiger le code

Ajouter ce qui suit à 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": {

Voir la source complète

Exécutez votre code

Enregistrez ce fichier sur votre machine et exécutez-le :

./create-application.sh

Conditions préalables

npm install @vonage/server-sdk

Créez un fichier nommé create-application.js et ajoutez le code suivant :

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

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

Voir la source complète

Rédiger le code

Ajouter ce qui suit à 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));

Voir la source complète

Exécutez votre code

Enregistrez ce fichier sur votre machine et exécutez-le :

node create-application.js

Conditions préalables

Ajouter ce qui suit à build.gradle:

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

Créez un fichier nommé CreateApplication et ajoutez le code suivant à la méthode main:

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

Voir la source complète

Rédiger le code

Ajouter ce qui suit à la méthode main du fichier 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()
}

Voir la source complète

Exécutez votre code

Nous pouvons utiliser le plugin application 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') : ''

Lancez la commande suivante gradle pour exécuter votre application, en remplaçant com.vonage.quickstart.kt.application par le paquet contenant CreateApplication:

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

Conditions préalables

Ajouter ce qui suit à build.gradle:

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

Créez un fichier nommé CreateApplication et ajoutez le code suivant à la méthode main:

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

Voir la source complète

Rédiger le code

Ajouter ce qui suit à la méthode main du fichier 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());

Voir la source complète

Exécutez votre code

Nous pouvons utiliser le plugin application 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') : ''

Lancez la commande suivante gradle pour exécuter votre application, en remplaçant com.vonage.quickstart.application par le paquet contenant CreateApplication:

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

Conditions préalables

Install-Package Vonage

Créez un fichier nommé CreateApplication.cs et ajoutez le code suivant :

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

Voir la source complète

Ajouter ce qui suit à CreateApplication.cs:

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

Voir la source complète

Rédiger le code

Ajouter ce qui suit à 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);

Voir la source complète

Conditions préalables

composer require vonage/client

Créez un fichier nommé create-application.php et ajoutez le code suivant :

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

Voir la source complète

Rédiger le code

Ajouter ce qui suit à 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;
}

Voir la source complète

Exécutez votre code

Enregistrez ce fichier sur votre machine et exécutez-le :

php create-application.php

Conditions préalables

pip install vonage python-dotenv

Rédiger le code

Ajouter ce qui suit à 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)

Voir la source complète

Exécutez votre code

Enregistrez ce fichier sur votre machine et exécutez-le :

python application/create-application.py

Conditions préalables

gem install vonage

Créez un fichier nommé create-application.rb et ajoutez le code suivant :

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

Voir la source complète

Rédiger le code

Ajouter ce qui suit à 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'
        }
      }
    }
  }
)

Voir la source complète

Exécutez votre code

Enregistrez ce fichier sur votre machine et exécutez-le :

ruby create-application.rb