アプリケーションの作成
このコード・スニペットでは、Applicationsを作成する方法を説明します。
例
以下の置換可能な値が、便利な方法でサンプルコードに設定されていることを確認する必要があります:
| キー | 説明 |
|---|---|
VONAGE_API_KEY | Your Vonage API key (see it on your dashboard). |
VONAGE_API_SECRET | Your Vonage API secret (also available on your dashboard). |
Write the code
Add the following to 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": {Run your code
Save this file to your machine and run it:
Prerequisites
npm install @vonage/server-sdkCreate a file named create-application.js and add the following code:
const { Vonage } = require('@vonage/server-sdk');
const vonage = new Vonage({
apiKey: VONAGE_API_KEY,
apiSecret: VONAGE_API_SECRET,
});Write the code
Add the following to 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));Run your code
Save this file to your machine and run it:
Prerequisites
Add the following to build.gradle:
implementation 'com.vonage:server-sdk-kotlin:2.1.1'Create a class named CreateApplication and add the following code to the main method:
val client = Vonage {
apiKey(VONAGE_API_KEY)
apiSecret(VONAGE_API_SECRET)
}Write the code
Add the following to the main method of the CreateApplication class:
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()
}Run your code
We can use the アプリケーション plugin for Gradle to simplify the running of our application. Update your build.gradle with the following:
apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''Run the following gradle command to execute your application, replacing com.vonage.quickstart.kt.application with the package containing CreateApplication:
Prerequisites
Add the following to build.gradle:
implementation 'com.vonage:server-sdk:9.3.1'Create a class named CreateApplication and add the following code to the main method:
VonageClient client = VonageClient.builder()
.apiKey(VONAGE_API_KEY)
.apiSecret(VONAGE_API_SECRET)
.build();Write the code
Add the following to the main method of the CreateApplication class:
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());Run your code
We can use the アプリケーション plugin for Gradle to simplify the running of our application. Update your build.gradle with the following:
apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''Run the following gradle command to execute your application, replacing com.vonage.quickstart.application with the package containing CreateApplication:
Prerequisites
Install-Package VonageCreate a file named CreateApplication.cs and add the following code:
using Vonage;
using Vonage.Applications;
using Vonage.Applications.Capabilities;
using Vonage.Request;Add the following to CreateApplication.cs:
var credentials = Credentials.FromApiKeyAndSecret(VONAGE_API_KEY, VONAGE_API_SECRET);
var client = new VonageClient(credentials);Write the code
Add the following to 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);Prerequisites
composer require vonage/clientCreate a file named create-application.php and add the following code:
Run your code
Save this file to your machine and run it:
Prerequisites
pip install vonage python-dotenvWrite the code
Add the following to 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)Run your code
Save this file to your machine and run it:
Prerequisites
gem install vonageCreate a file named create-application.rb and add the following code:
client = Vonage::Client.new(
api_key: VONAGE_API_KEY,
api_secret: VONAGE_API_SECRET
)Write the code
Add the following to 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'
}
}
}
}
)
Run your code
Save this file to your machine and run it: