Live Chat SDK Kotlin

Configuration

Using Java 11 or Android API 26 and above.

To use the DolphinLiveChat SDK library, please follow these steps :

  1. You need to import our library into yourr project

    • 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

    • 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.

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

Create Instance

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

It can be obtained from the Channel Connector menu of the live Chat channel

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

You need to initialize the Dolphin connector. Here's how to do it :

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

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

Explanation Component:

Nama
Deskripsi

DolphinProfile

Represents the identify of the user initiating the chat

username

Represents the name of the user

email

Represents the user's email

phoneNumber

Represents the user's phone number

uid

Used to determine whether this user session will create a new ticket or not

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

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

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 :

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 :

@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

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

Send Media Message

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

There are 4 media type, that is: IMAGE, DOCUMENT, AUDIO and VIDEO

Binaries

Required dependencies for the 3Dolphins application

implementation(libs.androidx.appcompat)
    implementation(libs.androidx.core.ktx)
    implementation(libs.converter.gson)
    implementation(libs.eventbus)
    implementation(libs.jackson.module.kotlin)
    implementation(libs.java.websocket)
    implementation(libs.logging.interceptor)
    implementation(libs.material)
    implementation(libs.okhttp)
    implementation(libs.retrofit)
    implementation(libs.rxandroid)
    implementation(libs.adapter.rxjava3)
    implementation(libs.rxjava3)
    implementation(libs.kotlin.reflect)
    implementation(libs.timber)

    testImplementation(libs.junit)
    testImplementation(libs.mockito.core)
    testImplementation(libs.mockito.kotlin)

    androidTestImplementation(libs.androidx.espresso.core)
    androidTestImplementation(libs.androidx.junit)

Last updated

Was this helpful?