仮想チャットと ライブエージェント チャットの実装
このモバイル SDKを使用すると、Androidアプリケーション内に仮想およびライブエージェントチャットサービスを簡単に実装できます。
NowChatSDK API を使用してチャットサービスを作成します。サービスが作成されたら、チャットユーザーインターフェイスを開始する必要があります。
チャットサービスを作成してインターフェイスを開始する方法を示すスニペットを次に示します。
/**
* Helper class used to handle different Now service instances.
*/
@Singleton
class SdkManager @Inject constructor() {
private var chatService: NowChatService? = null
/**
* Create the NowChatService once in the lifetime of the application, inside the Application class or another manager class
* that will be injected into other classes via dagger/hilt.
* NowChatService should be created after initializing the NowSDK.
*/
suspend fun getNowChatService(): NowChatService? {
if (chatService != null) return chatService
return NowChatSDK.makeChatService(URL("https://instance-name.service-now.com"),
object : NowChatSdkCallbacks {})
.getOrThrow()
.also { this.chatService = it }
}
}
//Activity that will start the NowChat
class MainActivity : AppCompatActivity() {
@Inject
lateinit var sdkManager: SdkManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//Start NowChat using the activity
lifecycleScope.launch {
sdkManager.getNowChatService()?.start(this@MainActivity)
}
}
}
アプリケーション内でチャット機能を利用する前に、ServiceNow インスタンス内でチャット機能を構成仮想エージェント必要があります。詳細については、「Virtual Agent」を参照してください。
ライブエージェントチャットと仮想エージェントチャットにコンテキスト変数を渡す
NowChatService-start() 関数で contextData パラメーターを渡すことで、チャットセッションを開始するときにチャットコンテキスト変数を渡すことができます。チャットコンテキスト変数の詳細については、「 Live agent chat context variables」を参照してください。
class MainActivity : AppCompatActivity() {
@Inject
lateinit var sdkManager: SdkManager
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val contextData = mapOf("sys_id" to "123456789", "table" to "wm_task")
//Start NowChat using the activity
lifecycleScope.launch {
sdkManager.getNowChatService()?.start(this@MainActivity, contextData = contextData)
}
}
}チャットユーザーインターフェイスのテーマ設定
start() 呼び出しでテーマオブジェクトを渡すことで、ライブエージェント と 仮想エージェント チャット UI の色をカスタマイズできます。カスタマイズできる要素のリストについては、「 NowChatService - launchIntent(context:Context, nowChatTheme:NowChatTheme):Intent」を参照してください。デフォルトでは、チャット UI はすべての NowSDK UI 要素に nowUIColor テーマを使用します。チャット UI にテーマを適用する方法の例については、サンプルアプリケーションを参照してください。