How to Augment Voice Calls with Twilio Intelligence in C#
Time to read:
How to Augment Voice Calls with Twilio Intelligence in C#
You know how to build a voice AI agent from scratch, one that supports speech recognition, text-to-speech, turn detection, and real-time audio streaming — all at low latency. But, what if you could also analyze customer conversations in real time and collect useful data insights for future calls?
With Twilio Conversation Intelligence and Conversation Orchestrator, backed by Conversation Memory, you can!
Specifically, in this tutorial you're going to learn how to use these three technologies together to retrieve a short summary of each customer call, and an analysis of the caller's sentiment. What's more, you'll also see whether your agent followed the guidelines you set for it. All of this information will then be persisted to a SQLite database, so that you can make use of it later.
Architecture
As this tutorial adds three new technologies to the previous application, here's a quick overview of how the new functionality works.
You will add a new route which receives a POST (webhook) request from Twilio after customer calls end. The request body will be a JSON string that contains, among other things, a short summary of the call, an assessment of the caller's sentiment, and how the agent adhered to a series of criteria. That information will be extracted from the request and then persisted to the application's SQLite database.
You're not going to do more with the received information. But, there are links at the end of the tutorial showing how you could continue building on the changes made in this tutorial, should you want to.
Prerequisites
To follow along with the tutorial, you will need the following:
- A free Twilio account — Sign up for an account here
- A voice-capable Twilio phone number
- The Owl Air phone agent from the previous tutorial, or any Conversation Relay-based agent built with .NET
- .NET 9 or newer
- An OpenAI API key
- The SQLite Command Line Shell, or your preferred database admin tool (which has SQLite support)
- Git
- ngrok or a similar tool, to expose your local server to Twilio
Build the app
Step 1: Set up Conversation Orchestrator and Conversation Memory
Before you can set up Conversation Intelligence, which does most of the work, you need to create a Memory Store and Conversation Configuration.
To do that, sign in to the Twilio Console, and go to Products & Services > Conversation Orchestrator > Conversation configurations. There, click Create a Conversation configuration. On the Name Configuration step, enter a name and description, then click Next.
On the Messaging and chat traffic step, click Next. On the Voice traffic step, scroll down and enable the Set up automatic capture checkbox. From the Voice phone numbers list, select your Twilio phone number, then click Next.
Now, on the Configure lifecycle step click Next. After that, on the Enable Conversation Memory step, create a memory store, by clicking Create new memory store, entering a name In the Memory store name field, and clicking Save.
From the Memory store list, select the memory store you just created, leave Turn on observations and summaries enabled, and click Next.
Finally, On the Summary step, review your settings and click Create Conversation configuration. Copy the conversation configuration ID for use later.
Step 2: Set up Conversation Intelligence
Before you can complete this step, you need to make the application publicly accessible on the internet as you'll need the ngrok URL later in this section. Run the command, below, to create a connection to the app on port 5000.
Now it is time to configure your Conversation Intelligence.
To do that, go to Products & Services > Conversation Intelligence > Intelligence configurations. There, click Create Intelligence configuration. Add a name, description, and, in the Attach Conversation configurations section, select the name of the Conversation configuration that you created in the previous step, and click Submit.
With that done, in Intelligence configurations, click Create rule next to the Intelligence configuration which you just created. Then, in the Add language operators section, enable Sentiment, Summary, and Script-Adherence, and click Next.
Now, in the Script-Adherence section, at the bottom of the Set Parameters step, add the following text into the script field and click Next.
Now, on the Trigger and action step, choose At conversation end in the Trigger section. Then, in the Action section, paste your ngrok URL plus "/intelligence-results" in the Webhook action field. Click Next.
In the Add context step, scroll down to the Conversation Memory section and enable Enable Conversation Memory for this rule and click Next. In the Summary step, click Create rule.
Step 3: Update the existing project structure
Add some new directories to your project by running the following command:
This data/database directory will store the application's SQLite database and a SQL file defining the database's schema. The other folders, which may or may not already exist in your base project, will help structure the .NET application.
Step 4: Set up the application's database
Create a new file named dump.sql in the data/database directory and paste the following SQL into that file.
The instructions:
- Enable SQLite's WAL (Write-Ahead Logging) mode (which, among other benefits, significantly improves performance)
- Enable foreign key support
- Define three tables:
- intelligence_results: stores the core information about the conversation
- operators: stores the information extracted by Conversation Intelligence, such as the call summary and intent
- operator_script_adherence_categories: stores the script adherence information, linking it to the relevant record in
operators
It's not the most sophisticated schema, but it can store the information in a maintainable way.
Now, use SQLite's Command-Line Shell (or your preferred database management tool) to provision the database with the following command.
Step 5: Create the Conversation Intelligence route
Add the required packages
The application needs a few NuGet packages to run: Dapper, a lightweight object mapper that simplifies working with the SQLite database, and Microsoft.Data.Sqlite, the official SQLite driver for .NET. You will also need to be sure you are running the latest version of the Twilio package. Install these dependencies by running the following commands from the project root:
Create the operator result records
Now, create the small immutable data types that will hold the pieces of information extracted from the webhook. Each of the three operator types — Summary, Sentiment, and ScriptAdherence — implements a common ITypeOperator interface, so that the database repository can treat them uniformly. ScriptAdherence additionally holds a list of Category records, one per script-adherence category that Conversation Intelligence evaluates. In a final build these would all be different files for extensibility. But for the purpose of a tutorial, because these are small, related, immutable data types, you can keep all five in a single file. In the Models folder of your existing project, create a file named OperatorResults.cs, and paste the code below into it.
Create the database service
It's time to create the database service that the webhook controller will use to persist the retrieved webhook data into the application's SQLite database.
In the Services folder, create a file named DatabaseService.cs, and paste the code below into the file.
This code opens a connection, starts a transaction, and inserts into intelligence_results. For each operator, the switch expression maps the runtime type ( Summary, Sentiment, or ScriptAdherence) to the short string stored in the database ("summary", "sentiment", or "script-adherence"). Dapper's ExecuteScalar<long> runs the insert and returns the value of last_insert_rowid(), which is SQLite's built-in function for getting the ID of the most recently inserted row.
Note that this sample project is building off of the demo project OwlAir. If you're working from a different Conversation Relay sample, update the namespace accordingly in your own code.
Create the webhook controller
Now, create the controller that will handle incoming Conversation Intelligence webhooks. In the Controllers folder, create a file named IntelligenceResultsController.cs, and paste the code below into the file.
The Post method is the heart of the class. It's called when Twilio sends a POST request to /intelligence-results at the end of a call. It:
1. Reads the raw JSON request body into a JsonElement, so we can walk the tree by hand without having to define a matching DTO for every nested shape Twilio might send.
2. Extracts conversationId and iterates over operatorResults, calling ImportOperator for each one.
3. ImportOperator looks at each operator's displayName and constructs the appropriate typed record: a Summary, Sentiment, or ScriptAdherence. Unknown display names return null and are skipped.
4. Once operators are collected, it fetches the conversation from the Conversations v2 API to get the start (CreatedAt) and end (UpdatedAt) timestamps — the webhook payload doesn't include these.
5. Finally, it passes everything to DatabaseService.RecordCall for persistence.
Register services and initialize database
Open Program.cs and replace its contents with:
Note that this code builds off of the existing OwlAir demo, so your namespace and introductory prompt may vary if you are using another starting demo project. You may also be using appsettings.json instead of a .env file so check your personal configuration.
The addition to your code helps connect with the database and write to your conversation intelligence:
1. DatabaseService is registered with the DI container. Any controller that declares a DatabaseService constructor parameter will be handed the same singleton instance.
2. TwilioClient.Init is called once at startup, with the account SID and auth token from configuration.
3. The SQL schema runs on startup. This creates the database.sqlite3 file and its tables the first time the app runs, so you don't need to invoke the SQLite command-line shell manually.
Add this block to ConversationRelayDemo.csproj so the schema file gets copied to the build output directory:
Step 6: Start the application
With the code now complete, start the application by running the command below in your terminal, after replacing the four environment variable placeholders with their respective values.
Set your Twilio credentials and OpenAI API key as environment variables. In your .env file, it will look like this, with the placeholders replaced with correct values.
The app will start on http://localhost:5000 by default. Expose it with ngrok by typing:
Add the resulting ngrok URI to your DOMAIN variable above.
Now, start (or restart) your server, by typing:
You will see output similar to the example below, after the application starts.
To make sure your call connects, wire your Twilio call as you may have done previously, by adding the ngrok URL, followed by /twiml to your Twilio dashboard as the A Call Comes In webhook connection. Look for it in the dashboard under Numbers and Senders > Overview > Configuration Details > Voice.
Place a call to your Twilio number. After the call ends and Twilio Conversation Intelligence has finished processing, you should see a new row in intelligence_results and its associated rows in operators and operator_script_adherence_categories.
Test that the app works as expected
With the application running, call your Twilio phone number. You should hear Hoot's greeting within a second or two of the call connecting:
> "Thanks for calling Owl Air! I'm Hoot. I can help with flight status, baggage policy, loyalty points, or booking changes. Which of those can I help you with?"
Then, like when you tested the first version of the app, try a few test questions to verify the full flow is working:
- "What's the baggage policy?": Hoot should describe carry-on and checked bag rules in natural spoken language.
- "How do loyalty points work?": Hoot should explain the earn and redemption rates.
- "Can I change my flight?": Hoot should give the change fee policy, with amounts spelled out in words.
After the call finishes, using your database tool of choice, have a look at the records in the application's database. There, you should see a summary of the conversation, along with the related operator information. You can also look on your Twilio dashboard and see records under your Intelligence Configuration.
Conclusion
You've now learned how to use Conversation Intelligence, Conversation Orchestrator, and Conversation Memory, as well as the Conversations API (V2), to retrieve a short summary of each call and an analysis of the caller's sentiment, and persist the information to a SQLite database, so that you can make use of it later. What's more, you also know whether your agent followed the guidelines you set for it.
But don't stop there! Now that the application can store conversation information, why not add a route for viewing a summary of all stored conversations, and one for viewing individual conversation details?
Then, I strongly encourage you to learn more about Conversation Intelligence, Conversation Orchestrator, and Conversation Memory, as well as the Conversations API (V2).
Amanda Lange is a .NET Engineer of Technical Content. She is here to teach how to create great things using C# and .NET programming. She can be reached at amlange [ at] twilio.com.
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.