Write shared variables
- Functions and limitations →
- Create a list in shared variables →
- Use a list from a shared variable →
- Clear shared variables →
- How can different bot users send data to the same list →
- How to make a bulletin board in a bot →
Functions and limitations
The Write shared variables component allows all bot users to interact with a variable that was assigned by one of the users. All users can read or change the value of such variables.
The amount of shared variables in the bot should not exceed 3 megabytes.
Shared variables are always relevant at the time of the request. If the first user requests a shared variable when the second user changes its value, the first user will receive the old value, not the new one.
Working with shared and individual variables is similar. Both variables are combined, assembled into an object, and so on. Data types and List Control work the same way in Write shared variables and in the regular Assign a variable.
Create a list in shared variables
1. Add the Write shared variables component to the screen.
2. Come up with a variable Name.
3. Change Type to JSON Object.
4. Add a list to Value, for example:
[ "apples", "oranges", "bananas" ]
Use a list from a shared variable
1. Add the Read shared variables component. The component should be added and correctly filled in, otherwise the shared variable will not be read.
2. Change or leave unchanged Name of the variable where the result will be written.
3. In Names of shared variables to read, add the variable name from the Write shared variables component that you filled in before. In our case, it's fruits.
The list is output to the text in this way:
{{#each Us_list.fruits}} {{this}} {{/each}}
You can learn more about working with arrays and lists in this article →
Done. The assigned shared variables will be output for all users of the bot.
For the work of shared variables, the first screen must be run once for one user. After that, the second screen will work for all users without activating the first one.
If you complement the scenario, other users will be able to influence the variable value. For example, if one of the users passes the Assign a variable screen with the same name, but with a different value, then the value will be changed for everyone.
No matter where the variable is output or changed — next to the original assignment or somewhere separately in the bot - you always need to add the Read shared variables component before the output message. The variable will not be output without it.
The table with all common variables is located at the bottom of the Users section, in the Shared Variables section. Click Show shared variables.
Shared variables are displayed individually for each platform.
Re-upload shared variables button updates the list of shared variables.
Close shared variables button closes the table of common variables.
Shared variables are displayed individually for each platform.
You cannot delete Shared variables through the Users section. They are cleared only with a special component.
Clear shared variables
Shared variables are deleted with Clear shared variables component. This component works like Clearing variables for regular user variables.
If you need to delete shared variables from all users at once, you can send the Clear shared variables screen in a Broadcast message. If there is nothing else on this screen, then such a broadcast will be invisible to users.
Examples of using lists and arrays in shared variables
How can different bot users send data to the same list
You need to make a list of dishes that will respond to the needs of all guests of the event. To do this, each person should write to the bot what they want to see in the menu.
The name of the dish from the user will first get into the regular variables, then it will be reassigned to the shared ones. After that, a complete list of dishes from all participants will be output by shared variables.
1. Create a new screen and add the Write shared variables component to it.
2. Come up with a variable Name. In the example, the variable will be called party_food.
3. Change Type to Empty list.
This screen may not transfer anywhere. It needs to be run once in order for the variable, where the list will be kept, to be assigned to the bot. You can reproduce the screen separately from the main scenario with Template Event or by clicking on the link from the screen settings on the right panel of the bot editor.
If you run this screen again after receiving any results from users, the list will be reset and all results will disappear beyond recovery.
4. Create the second screen and add Message to it.
5. Add User Input and fill in Variable name where the entered data will come.
6. Add Forward and create a new screen where this Forward will transfer to.
7. Add the Write shared variables component to a new screen and click Add a variable.
8. Come up with any variable Name.
9. In the Type field, leave String.
10. In Value, output the variable from User Input. In our case, the user data were assigned on the last screen to the favorite_food variable, so we add {{favorite_food}} to Value.
For more information on how to output variables follow the link →
11. Open the Write shared variables component settings and check the box next to the Assemble into object item.
12. In Object name, enter the variable name of the empty list on the first screen. In our case, this is party_food.
13. In Lists Management, select Add to the end so that each new dish from each new user gets to the end of the list.
14. Add the Read Shared Variables component to the same screen.
15. Come up with The name of the variable, where to write the result.
16. In Names of shared variables to read, add the party_food variable from the first screen. This variable will have a list, which we will output to the text.
17. In order for the list of dishes to come to the chat, add Message with the following text:
We added your favorite dish to the general list. Now the party menu looks like this:
{{#{{#each Us_list 1.party_food}} {{foods}} {{/each}}
You can learn more about working with arrays and lists in this article →
Done. If you add different dishes from different accounts, they will fall into one list, which everyone who launches the bot will be able to see.
How to make a bulletin board in a bot
Let's create a scenario for the service where the customer and the performer will be able to find each other.
1. Create a new screen.
2. Add the Write shared variables component to this screen.
3. Come up with variable Name. In our example, the variable will be called specialists.
4. Change Data type to Empty list.
This screen may not transfer anywhere. It needs to be run once in order for the variable, where the specialists’ data will be kept, to be assigned to the bot. You can reproduce the screen separately from the main scenario with Template Event or by clicking on the link from the screen settings on the right panel of the bot editor.
If you run this screen again after receiving any results from users, the list will be empty and all messages will disappear beyond recovery.
With Message and User Input, create a screen with a questionnaire where specialists will enter their data. More information about how to make a questionnaire can be found in this article →
5. Add Forward at the end of the screen.
6. Create a new screen and set the transition to it from Forward.
7. Add the Write shared variables component to the screen.
8. Since two variables are added at once in this example, double-click the Add variable button.
9. Come up with variable Names. In the example, we use service and Us_contact. In the service variable, output the specialization to Value, in the Us_contact variable, output the contact to connect with a specialist.
These data are output from regular variables that the specialist previously sent via User Input. To output a variable to text, it must be wrapped in double curly brackets. In our case, Values will include {{service}} and {{Us_contact}}
You can learn more about how to output variables here →
10. Open the component settings and click the Assemble into an object check box.
11. Enter “specialists” or any other into Object Name.
12. Create a new screen and add the Read Shared Variables component to it.
13. Change or leave unchanged The name of the variable, where to write the result.
14. In Names of shared variables to read, write down the name of the list, in our example it is specialists.
15. Output all the data from the performer's questionnaire into the text:
List of performers:
{{#each shared.specialists}} Service: {{service}}, my phone number to contact {{Us_contact}} {{/each}}
You can learn more about working with arrays and lists in this article →
Done. After the performers assign data about their services and the method of communication to shared variables, customers will be able to read them in a chat with the bot. To do this, the customer should get to the last screen of this scenario, where shared variables are read and output.
You can use a screen with two buttons or set up two Template Events. One will lead to a screen with a questionnaire for performers, the other will lead to a screen for customers, where information from those who offer their services is output.
The scenario can be expanded by adding other data to the questionnaire and to the shared variables, such as price, terms of services, and so on.