У Kwizbot є можливість писати свої власні action's на Javascript
Для цього використовується модуль Action Jail
<aside> 🧑💻 Якщо ви не розробник і не володієте JS, можливо, все, що описано далі, буде незрозуміло.
</aside>
Загальний вигляд модуля в інтерфейсі конструктора
Ви можете створювати нові action'и самостійно прямо з інтерфейсу.
/**
* Sample action jail action
* @method action_jail_sample
* @return {string}
*/
const action_jail_sample = async function () {
let result = 'custom_action_works';
//You should init state first
const state = this.getCurrentStateJSON();
//You can read from current state
const user_input = state.const['name']
//You can read action config
const req_params = state.node.json_obj;
//You can set to state
this.setCurrentStateConstant('custom_action_placeholder', 'I set from code');
this.setCurrentStateConstant('user_input', user_input);
//You can send action events just by returning text values
return result;
};
module.exports = action_jail_sample;
Найпростіший приклад - це action, який встановлює значення плейсхолдера і повертає різні події залежно від уже встановлених значень у state.
Наприклад просимо користувача ввести число. Якщо він ввів <10, то ми говоримо, що це "маленьке число", а якщо >10, то говоримо, що це велике число і просимо ввести маленьке число.
Код action'а
/**
* Sample action jail compare numbers action
* @method action_compare_numbers
* @return {string}
*/
const action_compare_numbers = async function () {
//You should init state first
const state = this.getCurrentStateJSON();
//You can read from current state from user input
const user_number = parseInt(state.const['number'])
if(user_number>10){
//Set placeholder {{compare_numbers_result}}
this.setCurrentStateConstant('compare_numbers_result', 'This is big number!');
//and return the event
return 'big_number'
}
this.setCurrentStateConstant('compare_numbers_result', 'This is small number!');
return 'small_number'
};
module.exports = action_compare_numbers;
Використання в сценарії