Send a Voice Message with Verification Code

var TeleSignSDK = require('telesignenterprisesdk');

console.log("## verify.voice ##");

const customerId = "FFFFFFFF-EEEE-DDDD-1234-AB1234567890";
const apiKey = "EXAMPLE----TE8sTgg45yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==";
const phoneNumber = "Your test phone number with no special characters or spaces goes here.";
const optionalParams = {verify_code: "32658"};

const client = new TeleSignSDK(customerId, apiKey);

// Callback handler for voice
function voiceCallback(error, responseBody) {
    if (error === null) {
        console.log(`Messaging response for messaging phone number: ${phoneNumber}` +
            ` => code: ${responseBody['status']['code']}` +
            `, description: ${responseBody['status']['description']}`);

        // Ask for user input
        prompt('Enter the verification code received:\n', function (input) {
            if (input === optionalParams['verify_code']) {
                console.log('Your code is correct.');
            } else {
                console.log('Your code is incorrect. input: ' + input + ", code: " + optionalParams['verify_code']);
            }
            process.exit();
        });

    } else {
        console.error("Unable to send voice call. " + error);
    }
}

// Method to handle user input
function prompt(question, callback) {
    const stdin = process.stdin,
        stdout = process.stdout;

    stdin.resume();
    stdout.write(question);

    stdin.once('data', function (data) {
        callback(data.toString().trim());
    });
}

// Send Voice request
client.verify.voice(voiceCallback, phoneNumber, optionalParams);

The code sample presented above shows you how to send a voice message containing a verification code using Voice Verify. This tutorial also provides a code sample for how to use a different language to send a voice message containing a verification code.

This tutorial contains the following sections:

Requirements

For this tutorial, the following is required:

  • customer ID - obtain from your account by logging in to teleportal.telesign.com (referred to as customer_id in this document)
  • API key - obtain from your account by logging in to teleportal.telesign.com (referred to as secret_key in this document)
  • SDK - Access to TeleSign’s GitHub repository for your selected language (you must request access from your Technical Account Manager):
  • Language Version
    • Node.js - 6+
    • Java - 7+
    • Python - 2.7+
    • Ruby - 2+
    • PHP - 5.6+
    • C# - 4.5+

Install the SDK

  1. Obtain your customer ID and API key.
  2. Log in to GitHub and navigate to the SDK in the language of your choice:
  3. Download or clone the repository. If you download it, extract the repository from the .ZIP file.
  4. To install the SDK for use, do the following:

Send a Voice Message with a Verification Code

This section explains how to send a voice message containing a verification code using Voice Verify. The sample code provided earlier is broken out into pieces and explained below.

  1. Begin by adding statements for including the appropriate part of the TeleSign SDK and any additional language specific functions you may need.
var TeleSignSDK = require('telesignenterprisesdk');
  1. Insert values for each of the items from customer_id/customerId through to phone_number/phoneNumber.
  • customer_id / customerId - Your TeleSign assigned customer ID, available in your account information in TelePortal.
  • api_key / apiKey - Your TeleSign assigned API key, available in your account information in TelePortal.
  • phone_number / phoneNumber - The phone number you want to use for the example. When you provide the number, it should be a string with no spaces or special characters. Include the complete number. For example, if you are doing a phone number for America, you would include the country code and the area code with the number. For example 16505551212.
console.log("## verify.voice ##");

const customerId = "FFFFFFFF-EEEE-DDDD-1234-AB1234567890";
const apiKey = "EXAMPLE----TE8sTgg45yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==";
const phoneNumber = "Your test phone number with no special characters or spaces goes here.";
  1. Generate a verification code.
const optionalParams = {verify_code: "32658"};
  1. Create a VerifyClient object and pass it your customer_id/customerId and api_key/apiKey.
const client = new TeleSignSDK(customerId, apiKey);
  1. Send a voice message containing the verification code you created. Store the results of your request in the response/telesignResponse variable. For node.js, use this step to create a callback handler for the voice message you will eventually send and a method called inside voiceCallback that will handle user input.
// Callback handler for voice
function voiceCallback(error, responseBody) {
    if (error === null) {
        console.log(`Messaging response for messaging phone number: ${phoneNumber}` +
            ` => code: ${responseBody['status']['code']}` +
            `, description: ${responseBody['status']['description']}`);
      
        // Ask for user input
        prompt('Enter the verification code received:\n', function (input) {
            if (input === optionalParams['verify_code']) {
                console.log('Your code is correct.');
            } else {
                console.log('Your code is incorrect. input: ' + input + ", code: " + optionalParams['verify_code']);
            }
            process.exit();
        });

    } else {
        console.error("Unable to send voice call. " + error);
    }
}

// Method to handle user input
function prompt(question, callback) {
    const stdin = process.stdin,
        stdout = process.stdout;

    stdin.resume();
    stdout.write(question);

    stdin.once('data', function (data) {
        callback(data.toString().trim());
    });
}
  1. Have the end user input the verification code they received, then check it to see if it is valid. Print the results of the check. For node.js, you send the voice message here, and the callback and user input handler methods you defined in the last step are passed in through the voiceCallback method.
// Send Voice request
client.verify.voice(voiceCallback, phoneNumber, optionalParams);
  1. 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. A sample is provided here if you want to cut n’ 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 section Supported Languages for Messages on the Codes, Languages, Time Zones page.

var TeleSignSDK = require('telesignenterprisesdk');

console.log("## verify.voice ##");

const customerId = "FFFFFFFF-EEEE-DDDD-1234-AB1234567890";
const apiKey = "EXAMPLE----TE8sTgg45yusumoN6BYsBVkh+yRJ5czgsnCehZaOYldPJdmFh6NeX8kunZ2zU1YWaUw/0wV6xfw==";
const phoneNumber = "Your test phone number with no special characters or spaces goes here.";
const optionalParams = {
    tts_message: "Votre code de vérification Widgets 'n' More est $$CODE$$.",
    language: "fr-FR"
};

const client = new TeleSignSDK(customerId, apiKey);

// Callback handler for voice
function voiceCallback(error, responseBody) {
    if (error === null) {
        console.log(`SMS response for phone number: ${phoneNumber}` +
            ` => code: ${responseBody['status']['code']}` +
            `, description: ${responseBody['status']['description']}`);
    } else {
        console.error("Unable to send voice. " + error);
    }
}

// Send voice request
client.verify.voice(voiceCallback, phoneNumber, optionalParams);

Code it Without the SDK

If you want to write all your own code, you will need to handle authentication yourself. You need the following documents: