Initialization¶
Initialize the SDK¶
The first step to use the Kindly SDK is to initialize it with your bot key:
You can also provide an authentication callback if your bot requires authentication:
KindlySDK.start(
application = application,
botKey = "YOUR_BOT_KEY",
languageCode = "en",
authTokenCallback = { chatId, promise ->
// Generate JWT token
// On success, call
promise.fulfill("JWT_TOKEN")
// On error
promise.reject(error)
}
)
For more details on authentication, see Using Authentication.
Change Language at Runtime¶
You can change the language after initialization using:
This method will: - Call the language switch API - Update the cached settings - Refresh UI translations - Notify all UI components of the language change
The language setting will persist across chat sessions until explicitly changed.
Bot Switching and Reinitialization¶
The SDK intelligently handles bot switching and reinitialization scenarios when you call start() multiple times with different parameters.
Automatic Bot Key Switching¶
When you call start() with a different bot key, the SDK automatically:
- Detects the change: Compares the new bot key with the current one
- Performs cleanup: Calls
endChat()internally to end the current session - Resets state: Clears SDK initialization flags and session data
- Reinitializes: Connects to the new bot seamlessly
// First initialization
KindlySDK.start(application = this, botKey = "bot-key-1")
KindlySDK.launchChat(context = this)
// Later, switch to different bot - automatic cleanup happens
KindlySDK.start(application = this, botKey = "bot-key-2") // SDK handles cleanup internally
KindlySDK.launchChat(context = this)
Connection State Handling¶
When the SDK is disconnected (e.g., after calling endChat()), calling start() with the same bot key will reconnect:
KindlySDK.start(application = this, botKey = "YOUR_BOT_KEY")
KindlySDK.launchChat(context = this)
// Later, end the chat
KindlySDK.endChat()
// Reconnect with same bot key - allowed reconnection
KindlySDK.start(application = this, botKey = "YOUR_BOT_KEY") // Reconnects instead of skipping
KindlySDK.launchChat(context = this)
Skipping Duplicate Initialization¶
The SDK skips initialization only when all conditions are met: - Same bot key - Still connected (not disconnected)
KindlySDK.start(application = this, botKey = "YOUR_BOT_KEY")
// This second call will be skipped - no changes needed
KindlySDK.start(application = this, botKey = "YOUR_BOT_KEY")
Best Practices for Bot Switching¶
Option 1: Automatic Switching (Recommended)¶
// SDK handles cleanup automatically
KindlySDK.start(application = this, botKey = "new-bot-key")
KindlySDK.launchChat(context = this)
Option 2: Explicit End-then-Start¶
// Explicit control over the lifecycle
KindlySDK.endChat()
// endChat() completes synchronously, so you can immediately call start()
KindlySDK.start(application = this, botKey = "new-bot-key")
KindlySDK.launchChat(context = this)
Both approaches work identically - choose based on your preference for explicit vs automatic lifecycle management.