Fortschritte des NCCO verfolgen
In diesem Codeschnipsel sehen Sie, wie Sie verfolgen können, wie weit ein Aufrufer durch ein NCCO kommt
unter Verwendung der notify Aktion
Beispiel
Voraussetzungen
npm install expressSchreiben Sie den Code
Fügen Sie Folgendes zu track-ncco-progress.js hinzu:
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}`);
});Führen Sie Ihren Code aus
Speichern Sie diese Datei auf Ihrem Rechner und führen Sie sie aus:
Voraussetzungen
Fügen Sie Folgendes zu build.gradle hinzu:
implementation 'com.vonage:server-sdk-kotlin:2.1.1'
implementation 'io.ktor:ktor-server-netty'
implementation 'io.ktor:ktor-serialization-jackson'Schreiben Sie den Code
Fügen Sie der Methode main in der Datei TrackNccoProgress Folgendes hinzu:
embeddedServer(Netty, port = 8000) {
routing {
get("/webhooks/answer") {
call.response.header("Content-Type", "application/json")
call.respond(
Ncco(
talkAction("Thanks for calling the notification line."),
notifyAction(
call.request.path().replace("answer", "notification"),
mapOf("foo" to "bar")
),
talkAction("You will never hear me as the notification URL will return an NCCO")
).toJson()
)
}
post("/webhooks/notification") {
val event = EventWebhook.fromJson(call.receive())
call.response.header("Content-Type", "application/json")
call.respond(
Ncco(
talkAction("Your notification has been received, loud and clear."),
).toJson()
)
}
}
}.start(wait = true)Führen Sie Ihren Code aus
Wir können das Applikation Plugin für Gradle verwenden, um die Ausführung unserer Anwendung zu vereinfachen. Aktualisieren Sie Ihre build.gradle mit dem Folgenden:
apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''Führen Sie den folgenden Befehl gradle aus, um Ihre Anwendung auszuführen, und ersetzen Sie dabei com.vonage.quickstart.kt.voice durch das Paket, das TrackNccoProgress enthält:
Voraussetzungen
Fügen Sie Folgendes zu build.gradle hinzu:
implementation 'com.vonage:server-sdk:9.3.1'
implementation 'com.sparkjava:spark-core:2.9.4'Schreiben Sie den Code
Fügen Sie der Methode main in der Datei TrackNccoProgress Folgendes hinzu:
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();
});Führen Sie Ihren Code aus
Wir können das Applikation Plugin für Gradle verwenden, um die Ausführung unserer Anwendung zu vereinfachen. Aktualisieren Sie Ihre build.gradle mit dem Folgenden:
apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''Führen Sie den folgenden Befehl gradle aus, um Ihre Anwendung auszuführen, und ersetzen Sie dabei com.vonage.quickstart.voice durch das Paket, das TrackNccoProgress enthält:
Voraussetzungen
Install-Package VonageSchreiben Sie den Code
Fügen Sie Folgendes zu TrackNccoController.cs hinzu:
[HttpGet("[controller]/webhooks/answer")]
public IActionResult Answer()
{
var host = Request.Host.ToString();
//Uncomment the next line if using ngrok with --host-header option
//host = Request.Headers["X-Original-Host"];
var eventUrl = $"{Request.Scheme}://{host}/webhooks/notification";
var talkAction = new TalkAction() { Text = "Thanks for calling the notification line" };
var notifyAction = new NotifyAction()
{
EventUrl = new[] { eventUrl },
Payload = new FooBar() { Foo = "bar" }
};
var talkAction2 = new TalkAction() { Text = "You will never hear me as the notification URL will return an NCCO" };
var ncco = new Ncco(talkAction, notifyAction, talkAction2);
return Ok(ncco.ToString());
}
[HttpPost("webhooks/notification")]
public async Task<IActionResult> Notify()
{
var notification = await WebhookParser.ParseWebhookAsync<Notification<FooBar>>(Request.Body, Request.ContentType);
Console.WriteLine($"Notification received payload's foo = {notification.Payload.Foo}");
var talkAction = new TalkAction() { Text = "Your notification has been received, loud and clear" };
var ncco = new Ncco(talkAction);
return Ok(ncco.ToString());
}Voraussetzungen
composer require slim/slim:^3.8 vonage/clientFühren Sie Ihren Code aus
Speichern Sie diese Datei auf Ihrem Rechner und führen Sie sie aus:
Voraussetzungen
pip install vonage python-dotenv fastapi[standard]Schreiben Sie den Code
Fügen Sie Folgendes zu track-ncco.py hinzu:
from fastapi import FastAPI, Request
from vonage_voice import NccoAction, Notify, Talk
app = FastAPI()
@app.get('/webhooks/answer')
async def inbound_call(request: Request):
ncco: list[NccoAction] = [
Talk(text=f'Thanks for calling the notification line.'),
Notify(
payload={"foo": "bar"},
eventUrl=[str(request.base_url) + 'webhooks/notification'],
),
Talk(text=f'You will never hear me as the notification URL will return an NCCO.'),
]
return [action.model_dump(by_alias=True, exclude_none=True) for action in ncco]
@app.post('/webhooks/notification')
async def on_notification():
return [
Talk(text=f'Your notification has been received, loud and clear').model_dump(
by_alias=True, exclude_none=True
)
]Führen Sie Ihren Code aus
Speichern Sie diese Datei auf Ihrem Rechner und führen Sie sie aus:
Voraussetzungen
gem install sinatra sinatra-contrib rack-contribSchreiben Sie den Code
Fügen Sie Folgendes zu track-ncco-progress.rb hinzu:
Führen Sie Ihren Code aus
Speichern Sie diese Datei auf Ihrem Rechner und führen Sie sie aus:
Probieren Sie es aus
Wenn Sie Ihre Vonage Numbers anrufen, hören Sie eine Text-to-Speech-Nachricht und erhalten eine Anfrage an Ihre Benachrichtigungs-URL