BOTWIZARD Documentation

Applying custom blocks

Custom blocks can be used to add new functionality to the bot while also adding visual blocks in the script editor. If you are a developer, check out the article - methods for working with custom blocks.

Block example

Any conclusions, fields (checkmarks, text or script file upload), and 2 buttons (information and block settings on the external page) can be added to the block. You independently determine the scheme of fields and conclusions of the block when registering a new block. Once a block is saved, it is not possible to change the schema.

Block information and settings button

Available to use 2 buttons. Both buttons open a small iframe window right inside the script editor that will display your page (settings or info). An access_token is automatically sent to your page using the POST method, containing the token of the current user who is viewing your application.

Call handler

Set the block to the desired location in the script. When the subscriber sends a message to the chat and the block is launched, a request will be sent to your server. You can manage the state of the chat, determine the further course of the script, etc. For convenience, the API access key is immediately included in the request. You can specify from which user the request is made (for whom the token is issued) in the block itself (the user must be a member of the bot, it is mandatory). The issued token is active for a short period of time (up to 4 hours), and it is subject to a bot restriction (you can only perform actions with the bot on which the block is registered).

PHP handler example

<?php

$params = filter_input(INPUT_POST, 'payload');
$data = json_decode($params, true);
$lines = ['It Works!'];
$keys = ['customer', 'profile', 'message', 'formdata', 'extra'];

if (is_array($data)) {
    foreach ($keys as $key) {
        if (!empty($data[$key])) {
            array_push($lines,
                    str_repeat('=', 8),
                    strtoupper($key),
                    strval($data[$key]));
        }
    }
}

$res = [
    'new_fields' => [
        [
            'node_id' => -1,
            'key' => 'my_field',
            'datatype' => 'string',
            'field_name' => 'Myfield',
            'uservar' => 'my_field',
            'value' => 'test',
            'hash' => md5('test'),
        ],
    ],
    'data' => json_encode([
        'test' => 'Y',
        'amount' => rand(5, 100),
        'code' => 'xxx-xxx-xxx-xxx',
    ]),
    'replymsg' => json_encode([
        [
            'type' => 'text',
            '_text' => implode(PHP_EOL, $lines),
        ],
        [
            'type' => 'image',
            '_text' => 'Rose',
            '_url' => 'https://example.com/rose.jpeg',
        ],
    ]),
        // next_block_id = Node ID
        // next = out0, out1, out2, out3
];

header('content-type: application/json; charset=utf-8');
echo json_encode([
    'success' => true,
    'message' => '',
    'result' => json_encode($res),
]);

Data

The request includes all the required data. The call uses the same component as the [RPC] block (#3423487), so the parameters are similar. An exception is the "extra" parameter, which adds block information and an access token. An example is below.

CUSTOMER
EM: test
========
PROFILE
{"tel":"0000","login":"emu","nickname":"Emulator","avatar":null}
========
MESSAGE
{"type":"text","_text":"HELLO WORLD",
"_visible":"","quick_reply_buttons":"[]"}
========
FORMDATA
[{"node_id":1,"key":"welcome","datatype":"string","field_name":"",
"uservar":"welcome","value":"RESET"}]
========
EXTRA
{"block_token":"AAA","node_num":57,
"access_token":"{\"access_token\":\"BBB\",\"token_type\":\"bearer\",\"expires_in\":14400,\"refresh_token\":null}",
"node_fields":"{\"agree\":\"N\",\"caller_id\":\"90\"}",
"node_outs":"[]","settings":"[]"}