这份文档还在翻译中,预期年底前完成。欢迎您提供宝贵的意见及建议。

Update Application

In this code snippet you will see how to update an Application.

Example

You will need to ensure that the following replaceable values are set in the example code using any convenient method:

Key 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).
VONAGE_APPLICATION_ID The Vonage Application ID.
NAME The new name for the Vonage application you want to update.

Write the code

Add the following to update-application.sh:

curl -X "PUT" "https://api.nexmo.com/v2/applications/$VONAGE_APPLICATION_ID" \
     -H 'Content-Type: application/json' \
     -u "$VONAGE_API_KEY:$VONAGE_API_SECRET" \
     -d $'{
  "name": "New App Name",
  "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": "POST"
        },
        "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": {}
  }
}'

View full source

Run your code

Save this file to your machine and run it:

./update-application.sh

Prerequisites

npm install @vonage/server-sdk

Create a file named update-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
}, {
    debug: true
});

View full source

Write the code

Add the following to update-application.js:

vonage.applications.update(VONAGE_APPLICATION_ID, {
    name: 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"
          }
        }
      }
    }
}, (error, result) => {
  if(error) {
    console.error(error);
  }
  else {
    console.log(result);
  }
});

View full source

Run your code

Save this file to your machine and run it:

node update-application.js

Prerequisites

Add the following to `build.gradle`:

compile 'com.vonage:client:6.2.0'

Create a class named UpdateApplication and add the following code to the main method:

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

View full source

Write the code

Add the following to the main method of the UpdateApplication class:

ApplicationClient applicationClient = client.getApplicationClient();
Application existingApplication = applicationClient.getApplication(VONAGE_APPLICATION_ID);

Capability messages = Messages.builder()
        .addWebhook(Webhook.Type.INBOUND,
                new Webhook("https://example.com/webhooks/inbound", HttpMethod.POST))
        .addWebhook(Webhook.Type.STATUS,
                new Webhook("https://example.com/webhooks/status", HttpMethod.POST))
        .build();

Capability voice = Voice.builder()
        .addWebhook(Webhook.Type.ANSWER,
                new Webhook("https://example.com/webhooks/answer", HttpMethod.POST))
        .addWebhook(Webhook.Type.EVENT,
                new Webhook("https://example.com/webhooks/event", HttpMethod.POST))
        .build();

Capability rtc = Rtc.builder()
        .addWebhook(Webhook.Type.EVENT,
                new Webhook("https://example.com/webhooks/event", HttpMethod.POST))
        .build();

Capability vbc = Vbc.builder().build();

Application application = applicationClient.updateApplication(
        Application.builder(existingApplication)
                .name(APPLICATION_NAME)
                .addCapability(messages)
                .addCapability(voice)
                .addCapability(rtc)
                .addCapability(vbc)
                .build()
);

System.out.println("Application Updated:");
System.out.println("Old: " + existingApplication.toJson());
System.out.println("New: " + application.toJson());

View full source

Run your code

We can use the application 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 UpdateApplication:

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

Prerequisites

Install-Package Vonage

Create a file named UpdateApplication.cs and add the following code:

using Vonage;
using Vonage.Request;
using Vonage.Applications;
using Vonage.Applications.Capabilities;
using Webhook = Vonage.Common.Webhook;

View full source

Add the following to UpdateApplication.cs:

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

            var messagesWebhooks = new Dictionary<Webhook.Type, Webhook>();
            messagesWebhooks.Add(
                Webhook.Type.inbound_url,
                new Webhook
                {
                    Address = "https://example.com/webhooks/inbound",
                    Method = "POST"
                });
            messagesWebhooks.Add(
                Webhook.Type.status_url,
                new Webhook
                {
                    Address = "https://example.com/webhooks/status",
                    Method = "POST"
                });
            var messagesCapability = new Messages(messagesWebhooks);
            var voiceWebhooks = new Dictionary<Webhook.Type, Webhook>();
            voiceWebhooks.Add(Webhook.Type.answer_url,
                new Webhook
                {
                    Address = "https://example.com/webhooks/answer",
                    Method = "GET"
                });
            voiceWebhooks.Add(Webhook.Type.event_url,
                new Webhook
                {
                    Address = "https://example.com/webhooks/events",
                    Method = "POST"
                });
            var voiceCapability = new Vonage.Applications.Capabilities.Voice(voiceWebhooks);
            var rtcWebhooks = new Dictionary<Webhook.Type, Webhook>();
            rtcWebhooks.Add(Webhook.Type.event_url,
                new Webhook
                {
                    Address = "https://example.com/webhooks/events",
                    Method = "POST"
                });
            var rtcCapability = new Rtc(rtcWebhooks);
            var vbcCapability = new Vbc();
            var request = new CreateApplicationRequest { 
                Name = NAME,
                Capabilities = new ApplicationCapabilities 
                { 
                    Messages = messagesCapability,
                    Rtc = rtcCapability, 
                    Voice = voiceCapability, 
                    Vbc = vbcCapability 
                } 
            };
            var response = client.ApplicationClient.UpdateApplication(VONAGE_APPLICATION_ID, request);
            Console.WriteLine(JsonConvert.SerializeObject(response));
        }
    }
}

View full source

Write the code

Add the following to UpdateApplication.cs:

var messagesWebhooks = new Dictionary<Webhook.Type, Webhook>();
messagesWebhooks.Add(
    Webhook.Type.inbound_url,
    new Webhook
    {
        Address = "https://example.com/webhooks/inbound",
        Method = "POST"
    });
messagesWebhooks.Add(
    Webhook.Type.status_url,
    new Webhook
    {
        Address = "https://example.com/webhooks/status",
        Method = "POST"
    });
var messagesCapability = new Messages(messagesWebhooks);
var voiceWebhooks = new Dictionary<Webhook.Type, Webhook>();
voiceWebhooks.Add(Webhook.Type.answer_url,
    new Webhook
    {
        Address = "https://example.com/webhooks/answer",
        Method = "GET"
    });
voiceWebhooks.Add(Webhook.Type.event_url,
    new Webhook
    {
        Address = "https://example.com/webhooks/events",
        Method = "POST"
    });
var voiceCapability = new Vonage.Applications.Capabilities.Voice(voiceWebhooks);
var rtcWebhooks = new Dictionary<Webhook.Type, Webhook>();
rtcWebhooks.Add(Webhook.Type.event_url,
    new Webhook
    {
        Address = "https://example.com/webhooks/events",
        Method = "POST"
    });
var rtcCapability = new Rtc(rtcWebhooks);
var vbcCapability = new Vbc();
var request = new CreateApplicationRequest { 
    Name = NAME,
    Capabilities = new ApplicationCapabilities 
    { 
        Messages = messagesCapability,
        Rtc = rtcCapability, 
        Voice = voiceCapability, 
        Vbc = vbcCapability 
    } 
};
var response = client.ApplicationClient.UpdateApplication(VONAGE_APPLICATION_ID, request);

View full source

Prerequisites

composer require vonage/client

Create a file named update-application.php and add the following code:

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

View full source

Write the code

Add the following to update-application.php:

try {
    $application = $client->applications()->get(VONAGE_APPLICATION_ID);
    $application->setName('New Name2');
    $application->getVoiceConfig()->setWebhook(
        Nexmo\Application\VoiceConfig::ANSWER,
        new Nexmo\Application\Webhook('http://test.domain/webhook/voice')
    );
    $application = $client->applications()->update($application);

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

View full source

Run your code

Save this file to your machine and run it:

php update-application.php

Prerequisites

pip install vonage

Create a file named update-application.py and add the following code:

client = vonage.Client(
    key=VONAGE_API_KEY,
    secret=VONAGE_API_SECRET
)

View full source

Write the code

Add the following to update-application.py:

response = client.application_v2.update_application(VONAGE_APPLICATION_ID, {
  "name": "Python Update 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"
        }
      }
    },
    "voice": {
      "webhooks": {
        "answer_url": {
          "address": "https://example.com/webhooks/answer",
          "http_method": "POST"
        },
        "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": {}
  }
})

View full source

Run your code

Save this file to your machine and run it:

python3 update-application.py

Prerequisites

gem install vonage

Create a file named update-application.rb and add the following code:

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

View full source

Write the code

Add the following to update-application.rb:

begin
  response = client.applications.update(
    VONAGE_APPLICATION_ID,
    id: VONAGE_APPLICATION_ID,
    name: 'Ruby Update 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'
          }
        }
      },
      'voice': {
        'webhooks': {
          'answer_url': {
            'address': 'https://example.com/webhooks/answer',
            'http_method': 'POST'
          },
          'event_url': {
            'address': 'https://example.com/webhooks/event',
            'http_method': 'POST'
          }
        }
      }
    }
  )
  puts 'Application Updated Successfully' if response.id
rescue StandardError => e
  puts e.message
end

View full source

Run your code

Save this file to your machine and run it:

ruby update-application.rb