SIGN UP

Key-Value Storage

Key-Value Storage

As you might know, there is an Applications concept in Voximplant. The Applications allow linking scenarios, phone numbers, users, rules, and call queues. To put it simply, an application is the entry point for any Voximplant-based solution, so it’s necessary to firstly create one and then continue with scenarios, rules, etc.


Previously, applications didn’t “remember” any of the actions or calculations made by scenarios, so developers were forced to save values to 3rd party services or their backend servers. If you ever had experience with local storage in web development, our new functionality would seem quite similar as it allows you to save key-value pairs that are unique for each application in your Voximplant account. This is called Key-Value Storage, which is being operated by a new VoxEngine module – ApplicationStorage. Follow the steps below to become acquainted with the principles of its usage.

WHAT YOU NEED

We suggest creating a simple scenario that “knows” how many times a user called a number. You will need:

  • Voximplant developer account. If you don’t have one, sign up here.
  • Voximplant application, JS scenario, rule, and user. Those will be created during this tutorial.
  • Web client to make a call, we’ll use our webphone at https://phone.voximplant.com/.

VOXIMPLANT APPLICATION SETTINGS

First, log in to your account here: https://manage.voximplant.com/auth. On the left menu, select Applications, click New application and create a storage application. Then go to your new application, switch to the Scenarios tab, and create a countingCalls scenario with the following code:

require(Modules.ApplicationStorage);

VoxEngine.addEventListener(AppEvents.CallAlerting, async (e) => {
let r = {value: -1};

    try {
        r = await ApplicationStorage.get('totalCalls');
        if (r === null) {
            r = await ApplicationStorage.put('totalCalls', 0);
        }
    } catch(e) {
        Logger.write('Failure while getting totalCalls value');
    }

    try {
        await ApplicationStorage.put('totalCalls', (r.value | 0) + 1);
    } catch(e) {
        Logger.write('Failure while updating totalCalls value');
    }
    
    e.call.answer();
    e.call.say(`Hello.  You call ${r.value} times`);

    e.call.addEventListener(CallEvents.PlaybackFinished, VoxEngine.terminate);

});

The first line mounts the ApplicationStorage module, the rest of the scenario logic is placed in the CallAlerting handler.

We declare a variable to make a comparison between a call counter and the initial value possible. Then we try to retrieve a totalCalls value from the application storage. If there is no value, we create such a variable in the storage:

try {
    r = await ApplicationStorage.get('totalCalls');
    if (r === null) {
        r = await ApplicationStorage.put('totalCalls', 0);
    }
} 

The next action to perform is to increment the value in the storage.

try {
        await ApplicationStorage.put('totalCalls', (r.value | 0) + 1);
    }
PAY ATTENTION
Each promise rejection should be handled as has been shown in the example above, otherwise, your scenario will fail during the execution. See the details here.
After working with the storage, the scenario answers a call with a synthesized message of how many times you called before. When the robotic speech is over, the scenario terminates a call session. That’s it.
 
When a scenario is ready, go to the switch to the Routing tab of your storage application and click New rule. Name it startCounting; use the created JS scenario in the rule and leave the default call pattern ( .* ).

The last action to perform here is to create a user for your application. Switch to the Users tab, click Create user, set username (e.g., user1) and password, then click Create. We’ll need this login-password pair to authenticate in the webphone.

HOW TO USE IT

Open https://phone.voximplant.com/ and log in, using your credentials. After successful login, enter an arbitrary string and click Call. If everything went properly, you’ll hear a synthesized greeting!

We wish you the best development experience with Voximplant. Keep an eye out – more great things to come!

 

 

Tags:voximplantstorageapplications
B6A24216-9891-45D1-9D1D-E7359CEB8282 Created with sketchtool.

Comments(0)

Add your comment

Please complete this field.

Recommended

Sign up for a free Voximplant developer account or talk to our experts
SIGN UP