Ask AI
English EN
Go to Watbot

Variables

Contact variables hold one person's data: city, the bouquet chosen, the order total. Global variables are shared by the whole bot: the shop address, an exchange rate, an order counter. Where they are shown and how to insert them into a message is described in Data you can insert.

What the functions write is saved once the code has finished without an error. Inside the code, reading already sees what was written.

getContactVariable

getContactVariable(name)

Returns the value of a contact variable.

Parameter Type What to pass
name String The variable name, as on the contact card. Spaces and non-Latin letters are allowed

Returns the value as a string, or undefined if the contact has no such variable. Wrap the value in Number() for calculations.

var count = Number(getContactVariable('roses_count') || 0);

if (count >= 11) {
    setContactVariable('discount', 10);
}

If the variable was written by this same code, the function returns the value in the form it was written: a number stays a number.

setContactVariable

setContactVariable(name, value)

Writes a contact variable. If the variable does not exist, it is created.

Parameter Type What to pass
name String The variable name
value String, number, true or false The value

Returns nothing.

An object, an array, null and undefined are not saved as they are — the variable gets an empty value. To save an object or an array, turn it into a string, and parse it back when reading:

setContactVariable('cart', JSON.stringify(['Roses', 'Tulips']));

var cart = JSON.parse(getContactVariable('cart') || '[]');

Booleans are better stored as strings, such as 'yes' and 'no': true and false come back as the strings '1' and '' the next time another block reads them.

deleteContactVariable

deleteContactVariable(name)

Deletes a contact variable.

Parameter Type What to pass
name String The variable name

Returns nothing. After deletion, getContactVariable with this name returns undefined.

deleteContactVariable('promo_code');

getGlobalVariable

getGlobalVariable(name)

Returns the value of a global variable of the bot.

Parameter Type What to pass
name String The global variable name

Returns the value as a string, or undefined if there is no such variable.

var freeDelivery = Number(getGlobalVariable('free_delivery_from') || 0);
var sum = Number(getContactVariable('order_total') || 0);

if (sum >= freeDelivery) {
    sendMessage('Delivery is free for you.');
}

setGlobalVariable

setGlobalVariable(name, value)

Writes a global variable of the bot. If the variable does not exist, it is created. The value is immediately visible to all of the bot's contacts.

Parameter Type What to pass
name String The global variable name
value Any The value. It is saved as a string: the number 5 becomes '5', and an object becomes '[object Object]', so turn objects into a string with JSON.stringify

Returns nothing.

var orders = Number(getGlobalVariable('orders_today') || 0);
setGlobalVariable('orders_today', orders + 1);

Warning

If code with such a write runs for several contacts at the same time, each one reads the same old value, and some of the increments are lost. Such a counter is not suitable for exact counting.

deleteGlobalVariable

deleteGlobalVariable(name)

Deletes a global variable of the bot.

Parameter Type What to pass
name String The global variable name

Returns nothing.

deleteGlobalVariable('deal_of_the_day');