Seguimiento de los progresos de la OCN
En este fragmento de código se muestra cómo realizar un seguimiento de la distancia que recorre una OCNN hasta el llamante
utilizando la función notify acción
Ejemplo
Requisitos previos
npm install expressEscriba el código
Añada lo siguiente a 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}`);
});Ejecute su código
Guarde este archivo en su máquina y ejecútelo:
Requisitos previos
Añada lo siguiente a build.gradle:
implementation 'com.vonage:server-sdk-kotlin:2.1.1'
implementation 'io.ktor:ktor-server-netty'
implementation 'io.ktor:ktor-serialization-jackson'Escriba el código
Añada lo siguiente al método main del archivo TrackNccoProgress:
Ejecute su código
Podemos utilizar el plugin aplicación para Gradle para simplificar la ejecución de nuestra aplicación. Actualiza tu build.gradle con lo siguiente:
apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''Ejecute el siguiente comando gradle para ejecutar su aplicación, sustituyendo com.vonage.quickstart.kt.voice por el paquete que contiene TrackNccoProgress:
Requisitos previos
Añada lo siguiente a build.gradle:
implementation 'com.vonage:server-sdk:9.3.1'
implementation 'com.sparkjava:spark-core:2.9.4'Escriba el código
Añada lo siguiente al método main del archivo TrackNccoProgress:
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();
});Ejecute su código
Podemos utilizar el plugin aplicación para Gradle para simplificar la ejecución de nuestra aplicación. Actualiza tu build.gradle con lo siguiente:
apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''Ejecute el siguiente comando gradle para ejecutar su aplicación, sustituyendo com.vonage.quickstart.voice por el paquete que contiene TrackNccoProgress:
Requisitos previos
Install-Package VonageRequisitos previos
composer require slim/slim:^3.8 vonage/clientEjecute su código
Guarde este archivo en su máquina y ejecútelo:
Requisitos previos
pip install vonage python-dotenv fastapi[standard]Ejecute su código
Guarde este archivo en su máquina y ejecútelo:
Requisitos previos
gem install sinatra sinatra-contrib rack-contribEscriba el código
Añada lo siguiente a track-ncco-progress.rb:
# frozen_string_literal: true
require 'sinatra'
require 'sinatra/multi_route'
require 'rack/contrib'
use Rack::PostBodyContentTypeParser
before do
content_type :json
end
route :get, :post, '/webhooks/answer' do
[
{
'action' => 'talk',
'text' => 'Thanks for calling the notification line'
},
{
'action' => 'notify',
'payload' => {'foo' => 'bar'},
'eventUrl' => ["#{request.base_url}/webhooks/notification"]
},
{
'action' => 'talk',
'text' => 'You will never hear me as the notification URL will return an NCCO'
}
].to_json
end
route :get, :post, '/webhooks/notification' do
puts params
[
{
'action' => 'talk',
'text' => 'Your notification has been received, loud and clear'
}
].to_json
end
route :get, :post, '/webhooks/event' do
puts params
halt 204
end
set :port, 3000Ejecute su código
Guarde este archivo en su máquina y ejecútelo:
Pruébalo
Cuando llames a tu número de Vonage, escucharás un mensaje de texto a voz y recibirás una solicitud a tu URL de notificación