Key-value storage, or how our applications have become more convenient

Key-value storage, or how our applications have become more convenient

Anyone who develops on Voximplant is aware of the concept of "applications" that connect cloud scripts, phone numbers, users, rules, and call queues to each other. Simply put, applications are the cornerstone of development on our platform, the entry point to any Voximplant-based solution, as it all starts with the creation of an application.

Previously, applications "remembered" neither the actions that scripts performed nor the results of calculations, so developers were forced to store values ​​in third-party services or on their backend. If you have ever worked with local storage in a browser, then our new functionality is very similar to this, because. Allows apps to remember key-value pairs that are unique to each app in your account. Storage operation became possible thanks to the new module ApplicationStorage - under the cut you will find a short guide on how to use it, welcome!

You will need

  • Voximplant account. If you don't have it, then registration lives here;
  • the Voximplant application, as well as a script, a rule, and a single user. We will create all this in this tutorial;
  • web client to make a call - use our webphone phone.voximplant.com.

Voximplant Settings

First login to your account: manage.voximplant.com/auth. In the menu on the left, click "Applications", then "New Application" and create an application named storage. Go to the new application, switch to the Scripts tab to create a countingCalls script 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(`ΠŸΡ€ΠΈΠ²Π΅Ρ‚ΡΡ‚Π²ΡƒΡŽ.  ΠšΠΎΠ»ΠΈΡ‡Π΅ΡΡ‚Π²ΠΎ ΠΏΡ€ΠΎΡˆΠ»Ρ‹Ρ… Π·Π²ΠΎΠ½ΠΊΠΎΠ²: ${r.value}. `, Language.RU_RUSSIAN_MALE);

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

});

The first line connects the ApplicationStorage module, the rest of the logic is placed in the event handler call alert.

First, we declare a variable so that we can compare the initial value with the call count. We then try to get the value of the totalCalls key from the store. If there is no such key yet, then we create it:

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

Next, you need to increase the value of the key in the storage:

try {
        await ApplicationStorage.put('totalCalls', (r.value | 0) + 1);
    }

PLEASE NOTE

For each promise, you must explicitly specify failover, as shown in the listing above - otherwise the script will be stopped and you will see an error in the logs. Details here.

After working with the repository, the script answers the incoming call using voice synthesis and tells you how many times you have called before. After this message, the script terminates the session.

Once you've saved the script, go to the Routing tab of your application and click on New Rule. Name it startCounting, specify the countingCalls script, and leave the default mask (.*).

Key-value storage, or how our applications have become more convenient
The last one is to create a user. To do this, go to "Users", click "Create User", enter a name (for example, user1) and password, then click "Create". We will need this login-password pair for authentication in the webphone.

Checking

Open the webphone from the link phone.voximplant.com and log in using the account name, app name, and username/password pair from the app. After a successful login, enter any character set in the input field and click Call. If everything was done correctly, then you will hear a synthesized greeting!

We wish you a great development at Voximplant and stay tuned - we will have many more πŸ˜‰

Source: habr.com

Add a comment