NCCOの進捗状況
このコード・スニペットでは、呼び出し元がNCCOのどこまで到達したかを追跡する方法を示している。
を使って notify アクション
例
Prerequisites
Write the code
Add the following to track-ncco-progress.js:
const Express = require('express');
const app = new Express();
const onInboundCall = (request, response) => {
const ncco = [
{
'action': 'talk',
'text': 'Thanks for calling the notification line',
},
{
'action': 'notify',
'payload': {
'foo': 'bar',
},
'eventUrl': [`${request.protocol}://${request.get('host')}/webhooks/notification`],
},
{
'action': 'talk',
'text': 'You will never hear me as the notification URL will return an NCCO ',
},
];
response.json(ncco);
};
const onNotification = (_, response) => {
const ncco = [
{
'action': 'talk',
'text': 'Your notification has been received, loud and clear',
},
];
response.json(ncco);
};
app
.get('/webhooks/answer', onInboundCall)
.post('/webhooks/notification', onNotification);
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});Run your code
Save this file to your machine and run it:
Prerequisites
Add the following to build.gradle:
Run your code
We can use the アプリケーション plugin for Gradle to simplify the running of our application. Update your build.gradle with the following:
Run the following gradle command to execute your application, replacing com.vonage.quickstart.kt.voice with the package containing TrackNccoProgress:
Prerequisites
Add the following to build.gradle:
Write the code
Add the following to the main method of the TrackNccoProgress class:
port(3000);
/*
* Answer Route
*/
get("/webhooks/answer", (req, res) -> {
String notifyUrl = String.format("%s://%s/webhooks/notification", req.scheme(), req.host());
TalkAction intro = TalkAction.builder("Thanks for calling the notification line.")
.build();
Map<String, String> payload = new HashMap<>();
payload.put("foo", "bar");
NotifyAction notify = NotifyAction.builder()
.payload(payload)
.eventUrl(notifyUrl)
.build();
TalkAction unheard = TalkAction.builder("You will never hear me as the notification URL will return an NCCO")
.build();
res.type("application/json");
return new Ncco(intro, notify, unheard).toJson();
});
/*
* Notification Route
*/
post("/webhooks/notification", (req, res) -> {
res.type("application/json");
return new Ncco(
TalkAction.builder("Your notification has been received, loud and clear.")
.build()
).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:
Run the following gradle command to execute your application, replacing com.vonage.quickstart.voice with the package containing TrackNccoProgress:
Prerequisites
Prerequisites
Write the code
Add the following to index.php:
require 'vendor/autoload.php';
$app = new \Slim\App();
$app->get('/webhooks/answer', function (Request $request, Response $response) {
//Get our public URL for this route
$uri = $request->getUri();
$url = $uri->getScheme() . '://'.$uri->getHost() . ($uri->getPort() ? ':'.$uri->getPort() : '') . '/webhooks/notification';
$notify = new \Vonage\Voice\NCCO\Action\Notify(
['foo' => 'bar'],
new \Vonage\Voice\Webhook($url, 'GET')
);
$ncco = new \Vonage\Voice\NCCO\NCCO();
$ncco
->addAction(
new \Vonage\Voice\NCCO\Action\Talk('Thanks for calling the notification line')
)
->addAction($notify)
->addAction(
new \Vonage\Voice\NCCO\Action\Talk('You will never hear me as the notification URL will return an NCCO')
)
;
return new JsonResponse($ncco);
});
$app->map(['GET', 'POST'], '/webhooks/notification', function (Request $request, Response $response) {
/** @var \Vonage\Voice\Webhook\Event */
$event = \Vonage\Voice\Webhook\Factory::createFromRequest($request);
error_log(print_r($event, true));
$ncco = new \Vonage\Voice\NCCO\NCCO();
$ncco->addAction(
new \Vonage\Voice\NCCO\Action\Talk('Your notification has been received, loud and clear')
);
return new JsonResponse($ncco);
});
$app->map(['GET', 'POST'], '/webhooks/event', function (Request $request, Response $response) {
/** @var \Vonage\Voice\Webhook\Event */
$event = \Vonage\Voice\Webhook\Factory::createFromRequest($request);
error_log(print_r($event, true));
return $response->withStatus(204);
});
$app->run();Run your code
Save this file to your machine and run it:
Prerequisites
Run your code
Save this file to your machine and run it:
Prerequisites
Run your code
Save this file to your machine and run it:
試してみる
Vonage番号に電話をかけると、音声合成メッセージが流れます。 通知URLへのリクエスト