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!
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:
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 (.*).
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 π