# Callbacks

<note>

The purpose of the document is to provide information about the mechanism of informing about events in the system
in the context of the verification performed. They can also be used to receive other information, which you can learn
about [here](/docs/integration/callbacks).

</note>

### Working with Callbacks

<note>

Please remember that when working with callbacks you not only need the `authologic-client` library,
but also `authologic-client-json` and one of the JSON parsing modules, for example the
`authologic-client-module-jackson`.

</note>

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:

```java
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:

```java
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:

```java
Callback callbackResult = jsonEngine.deserialize(ctx.body(), Callback.class);
```

The abstract Callback class provides two methods to identify the type of the result:

- the `isConversationFinished` returning `true` if the result is of type `Conversation` representing conversation
- the `isUnknownCallback` returning `true` if the result is of type `UnknownCallback` representing unknown callback

In the example code we expect the `FinishedConversationCallback`, so we check for it:

```java
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:

```java
Conversation conversation = callback.asConversationFinished().getConversation();
PersonName personName = conversation.getResult().getIdentity().getUser().getPerson().getName();

System.out.println("Person: " + personName.getFirstName() + ", " + personName.getLastName());
```

<warning>

Please remember that your code that handles the callback should return code 200 in response to the request
coming from Authologic, otherwise the callback will be repeated.

</warning>

#### Full Result using Javelin

A full, working example using [Javalin](https://javalin.io/), a simple web framework for Java and Kotlin
looks as follows:

```java
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);
    }
}
```

<note>

Despite our sincere intentions, it is difficult to create perfect technical documentation.
If you have an idea on how to improve this documentation, or you have trouble understanding any section,
please email us at [tech-support@authologic.com](mailto:tech-support@authologic.com)

</note>
