v1.6.7-experimental-08 - Documentation last updated on 07/04/2020
Chatbot Support Library is an optional Android library containing pure Java implementation for:
It allows application developers to create their own chatbot and executors.
Add this maven repository to your project’s build.gradle file:
maven { url 'https://qisdk.softbankrobotics.com/experimental/maven' }
Add the dependency to your module’s build.gradle file:
implementation 'com.aldebaran:qisdk-chatbot-support:1.6.7-experimental-08'
Create a custom ChatbotReaction:
class CustomChatbotReaction(private val toBeSaid: String) : JavaChatbotReaction() {
private var fsay: Future<Void>? = null
override fun runWith(speechEngine: SpeechEngine): Future<Void> {
val runFuture = SayBuilder.with(speechEngine)
.withText(toBeSaid)
.build()
.async()
.run()
fsay = runFuture
return runFuture
}
override fun stop() {
fsay?.cancel(true)
fsay = null
}
}
Create a custom Chatbot:
class CustomChatbot : JavaChatbot() {
override fun replyTo(phrase: Phrase, locale: Locale): JavaReplyReaction {
val reaction = CustomChatbotReaction("I understood ${phrase.text}")
return JavaReplyReaction(reaction, ReplyPriority.NORMAL)
}
override fun acknowledgeHeard(phrase: Phrase, locale: Locale) {
// Do something
}
override fun acknowledgeSaid(phrase: Phrase, locale: Locale) {
// Do something
}
}
Add your custom Chatbot to a Chat and run:
val chat = ChatBuilder.with(qiContext)
// Notice .asChatbot() conversion
.withChatbot(customChatbot.asChatbot())
.build()
chat.async().run()
Create a custom QiChatExecutor:
class CustomQiChatExecutor(qiContext: QiContext) : JavaQiChatExecutor() {
// Notice the WeakReference to avoid the QiContext leak
private var weakContext = WeakReference(qiContext)
private var animationFuture: Future<Void>? = null
override fun runWith(params: List<String>): Future<Void> { // This is called when execute is reached in the topic
val qiContext = weakContext.get()
if (params.isEmpty() || qiContext == null) {
return Future.of(null)
}
val param = params[0]
val runFuture = animate(qiContext, qiContext.resources.getIdentifier(param, "raw", qiContext.packageName))
animationFuture = runFuture
return runFuture
}
override fun stop() {
animationFuture?.cancel(true)
animationFuture = null
}
private fun animate(qiContext: QiContext, resource: Int): Future<Void> {
return AnimationBuilder.with(qiContext)
.withResources(resource)
.buildAsync()
.andThenCompose { animation ->
AnimateBuilder.with(qiContext)
.withAnimation(animation)
.buildAsync()
}
.andThenCompose { animate ->
animate.async().run()
}
}
}
Add the executor to a QiChatbot and run the associated Chat:
// Create a topic
val topic: Topic = TopicBuilder.with(qiContext)
.withResource(R.raw.execute)
.build()
// Create a qiChatbot
val qiChatbot: QiChatbot = QiChatbotBuilder.with(qiContext)
.withTopic(topic)
.build()
val executors = hashMapOf<String, QiChatExecutor>()
// Map the executor name from the topic to our qiChatExecutor
// Notice .asQiChatExecutor() conversion
executors.put("myExecutor", CustomQiChatExecutor(qiContext).asQiChatExecutor())
// Set the executors to the qiChatbot
qiChatbot.executors = executors
// Build chat with the chatbotBuilder
val chat: Chat = ChatBuilder.with(qiContext)
.withChatbot(qiChatbot)
.build()
// Run an action asynchronously
chat.async().run()