More Hook
A lot more on hooks
In this section, let's quickly go through more hooks
- params
- router
- checkpoint
Checkpoint
Save a stage as a last access point: checkpoint.
One use case is to take your users back to their latest previous menu instead of taking them back to the very start menu.
Params
Ever wanted to pass custom static data to your hooks? Use params (like functions parameters / method arguments)
Params can be applied to any type of message
Let's take a look at an example
100:
type: button
on-receive: "com.example.jawce.service.AccountHook:processAccountRequest"
message:
body: "Confirm account type closure?"
buttons:
- Savings Account
- Loan Account
params:
account-type: Individual
currency: ZAR
routes:
"re:.*": "LOAN_PROCESSOR_STAGE"
In your business logic hook, you can get the passed params as below
class AccountHook {
private final HookArgs args;
public AccountHook(HookArgs args) {
this.args = args;
}
public Object processAccountRequest() {
log.info("Process account args: {}", args);
// access params
var params = args.getMethodArgs();
if(params.get("currency").equals("ZAR")) {
// TODO: handle logic
}
return args;
}
}
Router
The only job for this hook is to provide dynamic routing.
Wondering how to dynamically redirect user to a certain stage? Use the router to act as a route decider switch.
100:
type: button
on-receive: "example.hooks.service.account_route_switch"
message:
body: "Confirm account type closure?"
buttons:
- Savings Account
- Loan Account
params:
account-type: Individual
currency: ZAR
routes:
"re:.*": "LOAN_PROCESSOR_STAGE"
In your business logic hook, you can do your decision making and redirect user like below
@hook
def account_route_switch(arg: HookArg) -> HookArg:
# if params currency is ZAR, then
# dynamically redirect user template stage
# for collecting zar account details
if args.params.get('currency') == 'ZAR':
next_route = {EngineConstants.DYNAMIC_ROUTE_KEY: "REQUEST_ZAR_ACC_DETAILS_STAGE"}
arg.additional_data = next_route
return arg