Callbacks
Working with Callbacks
authologic-client library,
but also authologic-client-json and one of the JSON parsing modules, for example the
authologic-client-module-jackson.The Java SDK can also be used to parse the information sent to your callback URL. It is as simple as initializing one of the JSON parsing engines, deserializing the body of the request received in the callback and using the result data. For example this is how the code could look like:
JsonEngine jsonEngine = (new JacksonJsonEngineProvider()).createJsonEngine();
Callback callback = jsonEngine.deserialize(callbackBody, Callback.class);
if (callback.isConversationFinished()) {
Conversation conversation = callback.asConversationFinished().getConversation();
PersonName personName = conversation.getResult().getIdentity().getUser().getPerson().getName();
System.out.println("Person: " + personName.getFirstName() + ", " + personName.getLastName());
}
Let's discuss the above example.
Initializing JSON Parser
We start by initializing the JSON parser, so we can easily parse the response. We do it only once as the parser is thread-safe and can be reused later when needed:
JsonEngine jsonEngine = (new JacksonJsonEngineProvider()).createJsonEngine();
Deserializing the Results
We assume that we got the callback body and stored in the callbackBody variable. Once we have it
we can easily deserialize it to the CallbackResult class using the initialized JSON parser:
Callback callbackResult = jsonEngine.deserialize(ctx.body(), Callback.class);
The abstract Callback class provides two methods to identify the type of the result:
- the
isConversationFinishedreturningtrueif the result is of typeConversationrepresenting conversation - the
isUnknownCallbackreturningtrueif the result is of typeUnknownCallbackrepresenting unknown callback
In the example code we expect the FinishedConversationCallback, so we check for it:
if (callback.isConversationFinished()) {
After we verified that we got the class we were expecting we can retrieve the conversation from the deserialized object and print out the identified person first and second name:
Conversation conversation = callback.asConversationFinished().getConversation();
PersonName personName = conversation.getResult().getIdentity().getUser().getPerson().getName();
System.out.println("Person: " + personName.getFirstName() + ", " + personName.getLastName());
Full Result using Javelin
A full, working example using Javalin(opens in new tab), a simple web framework for Java and Kotlin looks as follows:
import com.authologic.client.api.*;
import com.authologic.client.json.JacksonJsonEngineProvider;
import io.javalin.Javalin;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
JacksonJsonEngine jsonEngine = (new JacksonJsonEngineProvider()).createJsonEngine();
Javalin app = Javalin.create()
.post("/", ctx -> {
Callback callback = jsonEngine.deserialize(ctx.body(), Callback.class);
if (callback.isConversationFinished()) {
Conversation conversation = callback.asConversationFinished().getConversation();
PersonName personName = conversation.getResult().getIdentity().getUser().getPerson().getName();
System.out.println("Person: " + personName.getFirstName() + ", " + personName.getLastName());
}
ctx.status(HttpStatus.OK);
})
.start(8080);
}
}