package com.reactnativezendesksdk import android.app.Activity import android.content.Context import android.util.Log import com.facebook.react.bridge.* import zendesk.chat.Chat import zendesk.chat.ChatConfiguration import zendesk.chat.ChatEngine import zendesk.chat.ChatProvider import zendesk.chat.ChatSessionStatus import zendesk.chat.ChatState import zendesk.chat.ObservationScope import zendesk.chat.Observer import zendesk.chat.PreChatFormFieldStatus import zendesk.chat.ProfileProvider import zendesk.chat.PushNotificationsProvider import zendesk.chat.Providers import zendesk.chat.VisitorInfo import zendesk.core.JwtIdentity import zendesk.core.AnonymousIdentity import zendesk.core.Identity import zendesk.messaging.MessagingActivity import zendesk.core.Zendesk import zendesk.support.Support import zendesk.support.Guide import zendesk.support.guide.HelpCenterActivity import zendesk.support.guide.ViewArticleActivity import zendesk.support.requestlist.RequestListActivity import zendesk.answerbot.AnswerBot import zendesk.answerbot.AnswerBotEngine import zendesk.support.SupportEngine class ZendeskSdk(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) { private val appContext: ReactContext override fun getName(): String { return "RNZendeskChat" } @ReactMethod fun setVisitorInfo(options: ReadableMap) { val providers: Providers? = Chat.INSTANCE.providers() if (providers == null) { Log.d(TAG, "Can't set visitor info, provider is null") return } val profileProvider: ProfileProvider = providers.profileProvider() if (profileProvider == null) { Log.d(TAG, "Profile provider is null") return } val chatProvider: ChatProvider = providers.chatProvider() if (chatProvider == null) { Log.d(TAG, "Chat provider is null") return } var builder: VisitorInfo.Builder = VisitorInfo.builder() if (options.hasKey("name")) { builder = builder.withName(options.getString("name")) } if (options.hasKey("email")) { builder = builder.withEmail(options.getString("email")) } if (options.hasKey("phone")) { builder = builder.withPhoneNumber(options.getString("phone")) } val visitorInfo: VisitorInfo = builder.build() profileProvider.setVisitorInfo(visitorInfo, null) if (options.hasKey("department")) options.getString("department")?.let { chatProvider.setDepartment(it, null) } } @ReactMethod fun init(options: ReadableMap) { val appId = options.getString("appId") val clientId = options.getString("clientId") val url = options.getString("url") val key = options.getString("key") val context: Context = appContext if (url != null && appId != null) { Zendesk.INSTANCE.init(context, url, appId, clientId) } Support.INSTANCE.init(Zendesk.INSTANCE) AnswerBot.INSTANCE.init(Zendesk.INSTANCE, Guide.INSTANCE) if (key != null) { Chat.INSTANCE.init(context, key) } } @ReactMethod fun initChat(key: String) { val context: Context = appContext Chat.INSTANCE.init(context, key) } @ReactMethod fun setUserIdentity(options: ReadableMap) { if (options.hasKey("token")) { val identity: Identity = JwtIdentity(options.getString("token")) Zendesk.INSTANCE.setIdentity(identity) } else { val name = options.getString("name") val email = options.getString("email") val identity: Identity = AnonymousIdentity.Builder() .withNameIdentifier(name).withEmailIdentifier(email).build() Zendesk.INSTANCE.setIdentity(identity) } } @ReactMethod fun showHelpCenter(options: ReadableMap) { val botName = if (options.hasKey("botName")) options.getString("botName") else "Chat Bot" val activity = currentActivity if (activity != null) { if (options.hasKey("withChat")) { HelpCenterActivity.builder() .withEngines(ChatEngine.engine()) .show(activity) } else if (options.hasKey("disableTicketCreation")) { HelpCenterActivity.builder() .withContactUsButtonVisible(false) .withShowConversationsMenuButton(false) .show(activity, ViewArticleActivity.builder() .withContactUsButtonVisible(false) .config()) } else { HelpCenterActivity.builder() .show(activity) } } } @ReactMethod fun startChat(options: ReadableMap) { val providers: Providers? = Chat.INSTANCE.providers() setUserIdentity(options) setVisitorInfo(options) setUserIdentity(options) val botName = options.getString("botName") val chatConfiguration: ChatConfiguration = ChatConfiguration.builder() .withAgentAvailabilityEnabled(true) .withOfflineFormEnabled(true) .build() val activity = currentActivity if (activity != null){ if (options.hasKey("chatOnly")) { MessagingActivity.builder() .withBotLabelString(botName) .withEngines(ChatEngine.engine(), SupportEngine.engine()) .show(activity, chatConfiguration) } else { MessagingActivity.builder() .withBotLabelString(botName) .withEngines(AnswerBotEngine.engine(), ChatEngine.engine(), SupportEngine.engine()) .show(activity, chatConfiguration) } } } @ReactMethod fun setNotificationToken(token: String) { val pushProvider: PushNotificationsProvider? = Chat.INSTANCE.providers()?.pushNotificationsProvider() pushProvider?.registerPushToken(token) } companion object { private const val TAG = "ZendeskSdk" } init { appContext = reactContext } }