# Live Chat SDK Kotlin

### Configuration

{% hint style="success" %}
Menggunakan  versi Java 11 atau Android API 26 keatas&#x20;
{% endhint %}

Untuk menggunakan DolphinLiveChat library SDK diharapkan untuk mengikuti langkah-langkah berikut ini:&#x20;

1. Anda perlu melakukan import library kami ke dalam project milik Anda
   * Anda dapat merakit dolphin lib dan mendapatkan file arr. (Di android studio: Gradle-> Expand dolphinlib-> build -> klik dua kali assemble)&#x20;
   * Setelah build selesai, Anda akan mendapatkan lokasi aar &#x20;
   * Pindahkan file arr ke folder “libs” dari client-app&#x20;

2. Anda perlu menambahkan beberapa dependensi di dalam aplikasi .gradle milk Anda. Kemudian, lakukan sinkronisasi untuk mengkompilasi 3Dolphins SDK. Contoh penerapan langkah ini adalah dengan memasukkan DolphinLiveChat ke dalam project Anda dengan meletakkannya di folder libs/ misalnya, sebagai dependensi kompilasi Gradle:

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

### Create Instance&#x20;

Anda memerlukan url server, client id, dan client secret untuk aplikasi sebelum melakukan autentikasi.

{% hint style="info" %}
Bisa didapatkan pada menu channel connector milik channel live chat&#x20;
{% endhint %}

Inisialisasi ini hanya perlu dilakukan satu kali dalam siklus hidup aplikasi dan dapat diterapkan pada permulaan awal. Berikut cara melakukannya:

```
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;

Anda harus melakukan inisialisasi Dolphin Connector. Berikut cara melakukannya:

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

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

Penjelasan Komponen:

<table><thead><tr><th width="316">Nama</th><th>Deskripsi</th></tr></thead><tbody><tr><td>DolphinProfile</td><td>Merupakan identitas dari user yang melakukan chat </td></tr><tr><td>username</td><td>Merupakan nama dari user tersebut </td></tr><tr><td>email</td><td>Merupakan email dari user tersebut </td></tr><tr><td>phoneNumber</td><td>Merupakan nomor teleponn dari user tersebut</td></tr><tr><td>uid</td><td>Berguna untuk menentukan sesi user ini akan membuat tiket baru atau tidak</td></tr></tbody></table>

Jika Anda memiliki field tambahan yang akan dikirim ke server, Anda dapat memasukkannya di customVariables. Berikut merupakan contoh customVariables (JsonArray)

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

dolphinProfile.customVariableJsonArray = customVariables
```

Setelah dolphin connector berhasil diinisiasi, Anda dapat melakukan autentikasi ke server dengan:

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

### Event Handler&#x20;

DolphinChat SDK menggunakan EventBus untuk menyiarkan peristiwa ke seluruh aplikasi. Yang perlu Anda lakukan adalah mendaftarkan objek yang akan menerima peristiwa dari EventBus. Anda dapat memanggilnya seperti ini:

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

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

Setelah Anda tidak perlu lagi menangkap event tersebut, Anda harus membatalkan pendaftaran penerima dengan memanggil metode ini:

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

> You can put this code in onDestroy() method

### Receive Message

Untuk dapat menerima pesan, lakukan ini:

```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;

Dependensi yang diperlukan untuk aplikasi 3Dolphins

```
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/5.3.0-id/integration/chanel-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.
