Track NCCO progress
In this code snippet you see how to track how far through an NCCO a caller gets
using the notify action
Example
Prerequisites
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'
implementation 'io.ktor:ktor-server-netty'
implementation 'io.ktor:ktor-serialization-jackson'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.voice with the package containing TrackNccoProgress:
Prerequisites
Add the following to build.gradle:
implementation 'com.vonage:server-sdk:9.3.1'
implementation 'com.sparkjava:spark-core:2.9.4'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.voice with the package containing TrackNccoProgress:
Prerequisites
Write the code
Add the following to TrackNccoController.cs:
[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());
}Prerequisites
Run your code
Save this file to your machine and run it:
Prerequisites
Write the code
Add the following to track-ncco.py:
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
)
]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:
Try it out
When you call your Vonage Number you will hear a text-to-speech message and receive a request to your notification URL