Voice Verify API - Tutorial: Send a voice message with verification code with Telesign C# SDK
NOTE:
To add this product to your account, contact a Telesign expert. This product is available for full-service accounts only.
This tutorial teaches you how to use Telesign Voice Verify API with C# SDK, by walking you step-by-step through the creation of an integration where you send your own verification code to an end-user. Skip to the end of this page to see the full, completed sample code.
Before you begin
Make sure you have the following before you start:
- Authentication credentials: Your Customer ID and API Key. If you need help finding these items, go to the support article How do I find my Customer ID and API Key.
- Testing device: A mobile phone on which you can receive a call.
- dotnet CLI - This tool isn't required to use this SDK, but it is required for this tutorial.
NOTE:
This tutorial uses the following.
- MacOS
- .NET SDK v6.0.428
Please modify accordingly if your developer environment differs from these details.
Set up your project
-
Follow the Telesign Full-service C# SDK install instructions here on GitHub incorporating these details:
-
Use
voice_verify
as the project directory name. -
Use
SendVoice
as the name of the solution.
You should end up in the top-level directory ("SendVoice") for your solution in the Terminal.
-
Send a voice message with a verification code
- Open the file "Program.cs".
- Follow the steps below or copy the sample code into the file and update it accordingly.
- Add the imports below.
using System;
using System.Collections.Generic;
using Telesign;
- Add the following basic structure below the imports.
namespace SendVoice
{
class SendVoice
{
public static void Main(string[] args)
{
}
}
}
In the next step, you will begin adding statements to the Main
function in the PhoneId
class.
- Define variables in the
Main
function to store your authentication credentials. For testing purposes, you can just overwrite the default values below or use environment variables (see step 7).
string customerId = "FFFFFFFF-EEEE-DDDD-1234-AB1234567890";
string apiKey = "ABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==";
- Define a variable to hold the phone number you want to send a voice message to. For this tutorial, hardcode your testing device's phone number or pull it from an environment variable (see step 7).
string phoneNumber = "11234567890";
NOTE:
In your production integration, pull the phone number from your recipient database instead of hardcoding it.
- (Optional) Pull authentication credentials from environment variables. Although you might not choose to do so for this tutorial, in your production integration it's a good practice to reference your authentication credentials using environment variables.
if (System.Environment.GetEnvironmentVariable("CUSTOMER_ID") != null) {
customerId = System.Environment.GetEnvironmentVariable("CUSTOMER_ID");
}
if (System.Environment.GetEnvironmentVariable("API_KEY") != null) {
apiKey = System.Environment.GetEnvironmentVariable("API_KEY");
}
if (System.Environment.GetEnvironmentVariable("PHONE_NUMBER") != null) {
phoneNumber = System.Environment.GetEnvironmentVariable("PHONE_NUMBER");
}
- Generate a verification code.
string verifyCode = "12345";
Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("verify_code", verifyCode);
- Create a try-catch structure.
try
{
}
catch (Exception e)
{
}
- In the
try
block, create a VerifyClient object and pass it yourcustomerId
andapiKey
.
VerifyClient verifyClient = new VerifyClient(customerId, apiKey);
- Next in the the
try
block, send a voice message containing the verification code you created. Store the results of your request in thetelesignResponse
variable.
RestClient.TelesignResponse telesignResponse = verifyClient.Voice(phoneNumber, parameters);
- Next in the the
try
block, have the end user input the verification code they received.
Console.WriteLine("Please enter your verification code:");
string code = Console.ReadLine().Trim();
- Next in the the
try
block, check it to see if the verification code is valid and print the results of the check.
if (verifyCode == code)
{
Console.WriteLine("Your code is correct.");
}
else
{
Console.WriteLine("Your code is incorrect.");
}
- In the
catch
block, add code to print any error that might occur.
Console.WriteLine(e);
- After the try-catch structure, add code to allow you to exit the program.
Console.WriteLine("Press any key to quit.");
Console.ReadKey();
Test your integration
- Switch from your editor to the terminal and build the project:
dotnet build
- Run the project:
dotnet run
If all goes well, you should send a voice message containing a verification code to the phone number you selected. If you are testing with your phone, you should be able to enter the verification code in response to a prompt from your terminal. The sample will then check your verification code to see if it matches the verification code that was sent and report on the results.
Send a Voice Message with Verification Code in a Different Language
This section explains how to send a custom voice message in a different language using Voice Verify API. A sample is provided here if you want to cut and paste. Note that the only difference between this code sample and the previous code sample in this tutorial is that you use the tts_message
(text-to-speech) parameter to enter a custom message, and the language
parameter to specify the language. For a list of language codes, refer to - Voice Verify API - Supported languages for text-to-speech.
using System;
using System.Collections.Generic;
using Telesign;
namespace SendVoice
{
class SendVoice
{
static void Main(string[] args)
{
string customerId = "FFFFFFFF-EEEE-DDDD-1234-AB1234567890";
string apiKey = "EXAMPLEABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==";
string phoneNumber = "Your test phone number with no spaces or special characters goes here.";
if (System.Environment.GetEnvironmentVariable("CUSTOMER_ID") != null) {
customerId = System.Environment.GetEnvironmentVariable("CUSTOMER_ID");
}
if (System.Environment.GetEnvironmentVariable("API_KEY") != null) {
apiKey = System.Environment.GetEnvironmentVariable("API_KEY");
}
if (System.Environment.GetEnvironmentVariable("PHONE_NUMBER") != null) {
phoneNumber = System.Environment.GetEnvironmentVariable("PHONE_NUMBER");
}
string language = "fr-FR";
string ttsMessage = "Votre code de vérification Widgets 'n' More est $$CODE$$.";
Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("language", language);
parameters.Add("tts_message", ttsMessage);
try
{
VerifyClient verifyClient = new VerifyClient(customerId, apiKey);
RestClient.TelesignResponse telesignResponse = verifyClient.Voice(phoneNumber, parameters);
}
catch (Exception e)
{
Console.WriteLine(e);
}
Console.WriteLine("Press any key to quit.");
Console.ReadKey();
}
}
}
Sample code
using System;
using System.Collections.Generic;
using Telesign;
namespace SendVoice
{
class SendVoice
{
static void Main(string[] args)
{
string customerId = "FFFFFFFF-EEEE-DDDD-1234-AB1234567890";
string apiKey = "EXAMPLEABC12345yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==";
string phoneNumber = "Your test phone number with no special characters or spaces goes here.";
if (System.Environment.GetEnvironmentVariable("CUSTOMER_ID") != null) {
customerId = System.Environment.GetEnvironmentVariable("CUSTOMER_ID");
}
if (System.Environment.GetEnvironmentVariable("API_KEY") != null) {
apiKey = System.Environment.GetEnvironmentVariable("API_KEY");
}
if (System.Environment.GetEnvironmentVariable("PHONE_NUMBER") != null) {
phoneNumber = System.Environment.GetEnvironmentVariable("PHONE_NUMBER");
}
string verifyCode = "12345";
Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("verify_code", verifyCode);
try
{
VerifyClient verifyClient = new VerifyClient(customerId, apiKey);
RestClient.TelesignResponse telesignResponse = verifyClient.Voice(phoneNumber, parameters);
Console.WriteLine("Please enter your verification code:");
string code = Console.ReadLine().Trim();
if (verifyCode == code)
{
Console.WriteLine("Your code is correct.");
}
else
{
Console.WriteLine("Your code is incorrect.");
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
Console.WriteLine("Press any key to quit.");
Console.ReadKey();
}
}
}
Updated about 5 hours ago