Hooks using Java Reflection
Reflection allows dynamic inspection and manipulation of classes and objects on runtime.
JAWCE makes use of reflection for all hooks by default if your logic is written in Java
Learn more about Reflection Api
Each defined hook class should have a constructor that takes a HookArgs
object as a single parameter to their constructor.
Consider the example below
Template#
6005:
type: text
template: "com.example.ExampleRefHook:getOrderInfoTpl"
message: "Provide reference for order: {{ type }}, {{ currency }} {{ amount }}"
params:
order: "MyOrderNumber"
balance: 10
# .. other fields ..
Java Code#
@Sl4j
public class ExampleRefHook {
private final HookArgs args;
private final ISessionManagerImpl session;
public ExampleRefHook(HookArgs args) {
this.args = args;
this.session = (ISessionManagerImpl) args.getSession().session(args.getChannelUser().waId());
}
public HookArgs getOrderInfoTpl() {
log.info("Received hook args: {}", this.args);
// process logic based on passed template params
var amount = params.methodArgs().get("balance") > 10 ? 20 : 5;
// get order type previously saved in user session
var orderType = session.get("orderType", String.class);
args.setTemplateDynamicBody(
new TemplateDynamicBody(
null,
null,
Map.of(
"type", orderType,
"currency", "USD",
"amount", amount
)
));
return args;
}
}
The Java code and template above also demonstrates other advanced concepts you can use.
-
It takes the implemented
ISessionManager
service to get current user session object -
It makes use of
params
to handle logic based on the params given on that template