# Live Chat SDK Kotlin

### Configuration

{% hint style="success" %}
Using Java 11 or Android API 26 and above.
{% endhint %}

To use the DolphinLiveChat SDK library, please follow these steps :&#x20;

1. You need to import our library into yourr project&#x20;
   * You can assamble dolphin lib and get arr file. (In android studio: Gradle-> Expand dolphinlib-> build -> double click assemble)
   * After the build is complete, you will get the aar location&#x20;
   * Move the file to the “libs” folder of the client-app
2. You need to add some dependencies to your milk app .gradle file. Then, sync to compile 3Dolphins SDK. For example, include DolphinLiveChat into your project by placing it in the libs/ folder for example, as a Gradle compile dependency.

```kotlin
implementation(files("libs/dolphin-live-chat.aar"))
```

### Create Instance&#x20;

You must have the server url, client id, and client secret for the application before proceeding with authentication

{% hint style="info" %}
It can be obtained from the Channel Connector  menu of the  live Chat channel
{% endhint %}

This initialization only needs to be performed once during the application's lifecycle and can be applied at the initial startup. Here’s how to do it:

```
package com.imi.dolphin.livechat.DolphinLiveChatFactory;

class MainActivity : ComponentActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        EventBus.getDefault().register(this)


        val dolphinLiveChat : DolphinLiveChat = DolphinLiveChatFactory.create()
        val dolphinCore : DolphinCore = DolphinCore(
            clientId = <CLIENT_ID>,
            clientSecret = <CLIENT_SECRET>,
            channelUrl = <CHANNEL_URL>
        )

        dolphinLiveChat.init(dolphinCore = dolphinCore)
    }
}
```

### Connect to Server&#x20;

You need to initialize the Dolphin connector. Here's how to do it : &#x20;

```
 val dolphinProfile : DolphinProfile = DolphinProfile(
            username = <USERNAME>,
            email = <EMAIL>,
            phoneNumber = <PHONE>,
            uid = <UID>,
            customerId = null
        )

val dolphinLiveChatConnector : DolphinLiveChatConnector = dolphinLiveChat.getConnector(
            dolphinProfile = dolphinProfile
        )
```

Explanation Component:&#x20;

<table><thead><tr><th width="316">Nama</th><th>Deskripsi</th></tr></thead><tbody><tr><td>DolphinProfile</td><td>Represents the identify of the user initiating the chat</td></tr><tr><td>username</td><td>Represents the name of the user </td></tr><tr><td>email</td><td>Represents the user's email </td></tr><tr><td>phoneNumber</td><td>Represents the user's phone number</td></tr><tr><td>uid</td><td>Used to determine whether this user session will create a new ticket or not </td></tr></tbody></table>

If you have additional fields to be sent to the sever, you can include the in customVariables. Below is an example of customeVariableas (JsonArray)

```java
JSONArray customVariables = new JSONArray();
JSONObject field = new JSONObject();
field.put("name", "Doni");
customVariables.put(field);

dolphinProfile.customVariableJsonArray = customVariables
```

After successfully initializing the Dolphin connector, you can authenticate to the server using :

```
dolphinLiveChatConnector.connect(dolphinProfile)
```

### Event Handler&#x20;

The DolphiChat SDK uses EventBus to broadcast events throughout the application. You need register the object that will receive events from EventsBus. You can call it as follows : &#x20;

```
EventBus.getDefault().register(this);
```

> You can put this code in onResume(), onCreate() or onStart() method

Once you no longer need to capture the event, you should unregister the receiver by calling this method

```
EventBus.getDefault().unregister(this);
```

> You can put this code in onDestroy() method

### Receive Message

To receive message, do the following  :

```kotlin
@Subscribe
public void onReceiveMessage(ChatEvent chatEvent) {
    when (chatEvent.status) {
        ChatEvent.Status.INCOMING -> {
        // do something
        // chatEvent.chat get chat data
        // chatEvent.type get chat type
        }
        ChatEvent.Status.MESSAGE_READ -> {
        // do something
        // chatEvent.chat get chat data
        // chatEvent.type get chat type
        }

        ChatEvent.Status.MESSAGE_SENT -> {
        // do something
        // chatEvent.chat get chat data
        // chatEvent.type get chat type
        }

        ChatEvent.Status.MESSAGE_SEND_FAILED -> {
        // do something
        // chatEvent.status get chat status
        }

        ChatEvent.Status.AGENT_TYPING -> {
        // do something
        // chatEvent.chat get chat data
        // chatEvent.type get chat type
        }
        else -> return
    }
```

### Send Message&#x20;

```
DolphinConnect.getInstance().sendMessage("text message");
```

### Send Media Message

```
DolphinConnect.getInstance().sendFile(file, DolphinChat.Type.IMAGE);
```

{% hint style="info" %}
There are 4 media type, that is: IMAGE, DOCUMENT, AUDIO and VIDEO
{% endhint %}

### Binaries&#x20;

Required dependencies for the 3Dolphins application&#x20;

```
com.squareup.retrofit2:adapter-rxjava3:2.11.0
com.squareup.retrofit2:converter-gson:2.11.0
org.greenrobot:eventbus:3.3.1
com.fasterxml.jackson.module:jackson-module-kotlin:2.15.0
org.java-websocket:Java-WebSocket:1.6.0
com.squareup.okhttp3:logging-interceptor:4.12.0
com.squareup.okhttp3:okhttp:4.12.0
com.squareup.retrofit2:retrofit:2.11.0
io.reactivex.rxjava3:rxandroid:3.0.2
io.reactivex.rxjava3:rxjava:3.1.10
com.jakewharton.timber:timber:5.0.1
androidx.core:core-ktx:1.12.0
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.3dolphins.ai/integration/channel-connector/live-chat/live-chat-sdk-kotlin.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
