Using the Bot Constructor, you can send a newsletter to Bitrix24 contacts and configure it so that messages are sent only to the necessary contacts - for example, clients, and company employees are excluded from it. To do this, you need to create and configure a funnel in Bitrix24, configure fields in the «Contact» card and assemble a script in the Bot Builder. ChatApp must be connected to Bitrix24 according to the instructions: https://chatapp.online/ru/help/attachment-settings/.
Setting up contact fields
Contact
- Open your contact card. If the contact already has a preset Contact Type field, skip to step 5.
- Click «Create Field».
- Select «List».
- Enter the name of the field — «Contact Type».
- In the list items, enter the types by which you want to separate the contacts. For example: «client», «partner», «personnel reserve».
- Click «Save».
Creating a script in the Bot Builder
1. Go to Bot Builder.
2. Remove the «Start Block».
3. Add an «Incoming Webhook» block. There is no need to fill in parameters or add fields - the block is needed only to run the script.
4. Copy the link from the «Incoming webhook» block and save it. It will need to be inserted into the «Outgoing webhook» robot in Bitrix24, see section «Setting up a funnel in Bitrix24», point 6.
5. Create three variables: i, b, c. All of them must be of type «Integer».
i - will be responsible for the serial number of the element in the list;
b - will store the number of processed contacts;
c - will contain information about the number of processed contacts within one request (50 contacts).
6. Create three «Change Variable» blocks in a row and connect them together.
- Set the variable b to 0;
- Set variable i to -1;
- Set variable c to 0.
7. Add the «API request» block.
8. In the «API request» block, select «Request type» - «GET» and paste the link from Bitrix24 into the «Request link» field. To get the link, go to Bitrix24 → click «Developers» → «Other» → «Incoming webhook» → copy «Webhook for calling rest api». Provide the necessary rights - click «Privilege Settings» → «+ select» → «CRM» → «Save». The request parameters must be specified in the link:
https://portal name/ bitrix24.ru/rest/1/raj4lkiag1pqnqqu/crm.contact.list?start={{b}}&select[]=NAME&select[]=PHONE&select[]=TYPE_ID |
- start={{b}} — the serial number of the contact from which the list should begin;
- select[]=NAME&select[]=PHONE&select[]=TYPE_ID — fields «Name» (NAME), «Phone» (PHONE), «Contact Type» (ID of the Contact Type field).
How to find out the field ID:
- In the Chrome browser, go to Bitrix24 and right-click on the required field;
- Click «View code»;
- name=ID of the required field. For example, «TYPE_ID».
9. In the «mapping» tab of the «API request» block, map the «$.total» result to the variable in the Constructor. Name the variable, for example «total». This is needed to find out how many elements are in the list.
10. Click «Save» in the «API request» block.
11. Add the «Programmer» block. It will allow you to process a list that will return an «API request».
12. Enter the code in the «Programmer» block:
let i = {{i}} +1 let b = {{b}} +1 let c = {{c}} +1 save({i, b, c}) |
Each cycle will add +1 to the previously created variables in order to know how many elements have already been processed and to be able to access new ones. «save({i, b, c})» – saves the result.
13. Create a «Change Variable» block.
14. Create a dialog variable «TYPE_ID» and in the «Change Variable» block, insert into it the value of the result of the «API request» block, which contains the contact type.
To do this, enter double curly braces «{{» and select the «data» result of the «API request» block. In the result after «data» add «.result[i].TYPE_ID». You will get a result like this: {{send_request|node-pW88Rp|data.result[i].TYPE_ID}}.
15. Now you need to set up a check on the «Contact Type» field to ensure that you only send the message to the right contacts. For example, so that contacts of the «Clients» and «Partners» types are included in the newsletter, but «Personnel Reserve» is not.
To do this, you need to find out the ID of the «Personnel Reserve» value of the «Contact Type» field:
- In the Chrome browser, right-click on the «Contact Type» field;
- Click «View code»;
- Look at the value in the value= field. Example value: «value=UC_TZW4SU» - this ID contains «personnel reserve».
16. Create a «Condition» block and set up a check that TYPE_ID contains «personnel reserve» - insert its ID into the field on the right.
17. If TYPE_ID = talent pool, then we skip this contact and repeat the step until the result of «Condition 1» is False. To do this, connect «Condition 1» back to the «Programmer» block.
18. To prevent the script from going into an endless loop, if there are more than 50 «personnel reserve» contacts in a row, add another «Condition» between the blocks:
If the variable c is less than or equal to 49, then the script moves on;
- If c=50, it's time to update the request and get the next page with new 50 contacts;
- To do this, return the result «=50» to the «Change Variable» block, where the value of i will change to -1.
19. After the second «Condition» block, place a «Pause» block so as not to reach the limit on executing blocks per second.
20. Add another «Pause» block between the first «Condition» block and the «Programmer» block.
The resulting script should look like this:
21. Add another «Condition» block: total<b or total≥b. This is necessary to check whether the check has reached the end of the mailing.
22. Add the «Sending message*» block if total≥b - the check has not reached the end of the mailing.
- Enter a message to send; In the «ChatID» field, enter the result from the «API request» block containing the ChatID. For example, with a phone number. To do this, enter double curly braces «{{» and select the «data» result of the «API request» block. In the result, after «data» add «.result[i].PHONE[0].VALUE».
- You will get a result like this: {{send_request|node-pW88Rp|data.result[i].PHONE[0].VALUE}}; Connect this block with the «Programmer» block to continue sending.
23. Add the block «Sending message*» if total<b - mailing is completed.
- Enter a message to send;
- In the «ChatID» field, enter the result from the «API request» block containing the ChatID. For example, with a phone number. To do this, enter double curly braces «{{» and select the «data» result of the «API request» block. In the result, after «data» add «.result[i].PHONE[0].VALUE». You will get a result like this: {{send_request|node-pW88Rp|data.result[i].PHONE[0].VALUE}};
- After this block, add a «Close Dialog» block and connect them so that the script completes its work.
Final scenario:
The script can be found at the link: https://constructorbot.chatapp.online/view/8307?share=8fd5124b57a3ac72e32e4e4356e622cf&lang=ru.
Setting up a funnel in Bitrix24
- In the Bitrix24 funnel, create the «Launch» stage.
- Go to «Automation rules».
- Add the «Outgoing Webhook» robot to the «Started» stage. To do this, click on «+» and select «Outgoing webhook».
- In the robot settings, you need to add a link that can be copied in the Bot Constructor from the «Incoming webhook» block. See the section «Creating a script in the Bot Constructor», paragraph 5.
- Go to «Robots».
- To start the mailing, move any deal to the «Launch» stage.