Interaction-related filters
These hooks let you customize interaction definitions, sessions, and response messages.
Included hooks
- slc_filter_interaction_definition: Adjust the raw interaction definition data.
- slc_filter_interaction_session_load: Adjust interaction sessions after loading.
- slc_filter_interaction_session_save: Adjust interaction session data before saving.
- slc_filter_interaction_message: Adjust the response messages returned by the interaction handler.
- slc_filter_interaction_message_cancel_confirm: Adjust the cancel confirmation messages.
- slc_filter_interaction_message_cancel_request: Adjust the cancel request messages.
- slc_filter_interaction_normalize: Adjust normalized user input.
- slc_filter_interaction_validate: Adjust validation results.
slc_filter_interaction_definition
Use this filter to adjust the raw interaction definition data right after it is loaded. It runs inside InteractionDefinition::from_post() before the InteractionDefinition object is created.
Arguments
$data: (array) Interaction definition data. This usually includes keys such assteps,timeoutMinutes, andrunPolicy.$post_id: (int) The interaction post ID.$form_version: (int|string|null) The loaded form version.
Example
This example overrides timeout settings for a specific interaction version.
function my_filter_interaction_definition($data, $post_id, $form_version) {
if ((int) $form_version === 2) {
$data['timeoutMinutes'] = 30;
$data['timeoutRemind'] = 5;
}
return $data;
}
add_filter('slc_filter_interaction_definition', 'my_filter_interaction_definition', 10, 3);
slc_filter_interaction_session_load
Use this filter to adjust an interaction session after it has been loaded from the database. It is applied to the InteractionSession::from_db_row() return value.
Arguments
$session: (InteractionSession) The loaded session object.
Example
This example shows how you could add custom handling right after loading an active session.
function my_filter_interaction_session_load($session) {
if ($session->get_status() === 'active') {
// Add custom logic here if needed.
}
return $session;
}
add_filter('slc_filter_interaction_session_load', 'my_filter_interaction_session_load');
slc_filter_interaction_session_save
Use this filter to adjust the data that will be written to the database before an interaction session is saved. It runs inside SessionRepository::save() before the INSERT or UPDATE query.
Arguments
$data: (array) An associative array used for persistence. It includes keys such aschannel_prefix,line_user_id,status, andanswers.$session: (InteractionSession) The session object being saved.
Example
This example adds a custom marker to the stored answers before saving.
function my_filter_interaction_session_save($data, $session) {
if (isset($data['answers'])) {
$answers = json_decode($data['answers'], true);
if (is_array($answers)) {
$answers['source'] = 'interaction';
$data['answers'] = wp_json_encode($answers);
}
}
return $data;
}
add_filter('slc_filter_interaction_session_save', 'my_filter_interaction_session_save', 10, 2);
slc_filter_interaction_message
Use this filter to adjust the message array returned for each interaction step. It is applied at the end of both presentStep() and handle().
Arguments
$messages: (array) The array of messages generated by theMessageBuilder.$session: (InteractionSession) The current session.$event: (object) The LINE event object.
Example
This example appends a note message before sending.
function my_filter_interaction_message($messages, $session, $event) {
$messages[] = array(
'type' => 'text',
'text' => 'This message has been customized.',
);
return $messages;
}
add_filter('slc_filter_interaction_message', 'my_filter_interaction_message', 10, 3);
slc_filter_interaction_message_cancel_confirm
Use this filter to adjust the message shown when a cancel confirmation is needed. For example, you can customize the cancelConfirm step when a cancel word is detected.
Arguments
$messages: (array) The cancel confirmation message array.$session: (InteractionSession) The current session.$event: (object) The LINE event object.
Example
This example adds an extra line to the cancel confirmation message.
function my_filter_interaction_message_cancel_confirm($messages, $session, $event) {
$messages[] = array(
'type' => 'text',
'text' => 'Do you want to stop the current input?',
);
return $messages;
}
add_filter('slc_filter_interaction_message_cancel_confirm', 'my_filter_interaction_message_cancel_confirm', 10, 3);
slc_filter_interaction_message_cancel_request
Use this filter to adjust the message returned when cancellation is confirmed. It runs right before the session is deleted.
Arguments
$messages: (array) The cancel request message array.$session: (InteractionSession) The current session.$event: (object) The LINE event object.
Example
This example replaces the cancellation message.
function my_filter_interaction_message_cancel_request($messages, $session, $event) {
$messages = array(
array(
'type' => 'text',
'text' => 'Your interaction has been canceled.',
),
);
return $messages;
}
add_filter('slc_filter_interaction_message_cancel_request', 'my_filter_interaction_message_cancel_request', 10, 3);
slc_filter_interaction_normalize
Use this filter to adjust the normalized user input before validation runs.
Arguments
$normalized_input: (string) The normalized string returned byInputNormalizer::normalize().$step: (StepDefinition) The current step definition.$session: (InteractionSession) The current session.$event: (object) The LINE event object.
Example
This example removes full-width spaces after normalization.
function my_filter_interaction_normalize($normalized_input, $step, $session, $event) {
return trim(str_replace(' ', ' ', $normalized_input));
}
add_filter('slc_filter_interaction_normalize', 'my_filter_interaction_normalize', 10, 4);
slc_filter_interaction_validate
Use this filter to adjust the validation result for user input. You can customize the Validator::validate() return value before the interaction continues.
Arguments
$validation_result: (ValidationResult) The validation result object.$step: (StepDefinition) The current step definition.$session: (InteractionSession) The current session.$event: (object) The LINE event object.
Example
This example disables validation errors for a specific step.
function my_filter_interaction_validate($validation_result, $step, $session, $event) {
if ($step->get_id() === 'optional_step') {
return new \Shipweb\LineConnect\Interaction\ValidationResult(true, array());
}
return $validation_result;
}
add_filter('slc_filter_interaction_validate', 'my_filter_interaction_validate', 10, 4);