我正在 Android 上开发 Pepper 应用程序,该应用程序使用 QiChatbot 与用户就矿物进行交流。我想让聊天机器人也可供有听力和/或言语障碍的人在屏幕上使用。所以我正在构建一个像 facebook Messenger 一样的用户界面。虽然我能够从胡椒那里得到回复并将其显示在屏幕上,但我找不到一种方法来强制将用户从 EditText 获取的字符串传递给聊天机器人。
我尝试过使用:
Future<ReplyReaction> replyToFuture = chatbot.async().replyTo(userPhrase, localeEN);
和
Future<Void> acknowledgeHeardFuture = chatbot.async().acknowledgeHeard(userPhrase,localeEN);
没有成功
下面您可以看到整个聊天和聊天机器人设置:
private QiChatbot buildQiChatbot() {
Topic lexicon = TopicBuilder
.with(qiContext)
.withResource(R.raw.lexicon)
.build();
Topic topic_minerals = TopicBuilder
.with(qiContext)
.withResource(R.raw.topic_minerals)
.build();
Topic test = TopicBuilder
.with(qiContext)
.withResource(R.raw.test_topic)
.build();
List<Topic> topicList = new LinkedList<Topic>();
topicList.add(test);
//topicList.add(lexicon);
//topicList.add(topic_minerals);
chatbot = QiChatbotBuilder
.with(qiContext)
.withTopics(topicList)
.withLocale(localeEN)
.build();
chatbot.addOnBookmarkReachedListener(bookmark -> {
Log.i(TAG, "Bookmark " + bookmark.getName() + " reached.");
});
chatbot.addOnEndedListener(endReason -> {
Log.i(TAG, "Chatbot ended for reason: " + endReason);
chatFuture.requestCancellation();
});
return chatbot;
}
private Chat buildChat(QiChatbot chatbot) {
chat = ChatBuilder
.with(qiContext)
.withChatbot(chatbot)
.build();
chat.addOnStartedListener(() -> {
Log.i(TAG, "[CHAT] Chat started.");
});
chat.addOnListeningChangedListener(listening -> {
if (listening) {
Log.i(TAG, "[CHAT] Listening START.");
} else {
Log.i(TAG, "[CHAT] Listening END.");
}
});
chat.addOnHeardListener(heardPhrase -> {
Log.i(TAG, "[CHAT] Heard phrase: " + heardPhrase.getText());
});
chat.addOnNormalReplyFoundForListener(input -> {
Log.i(TAG, "[CHAT] Reply found for user message: " + input.getText());
});
chat.addOnNoPhraseRecognizedListener(() -> {
Log.i(TAG, "[CHAT] No phrase recognized.");
});
chat.addOnSayingChangedListener(sayingPhrase -> {
if (!sayingPhrase.getText().isEmpty()) {
Log.i(TAG, "[CHAT] Pepper Reply: " + sayingPhrase.getText());
messageItemList.add(new MessageItem(LayoutRobot, R.drawable.icons8_user_100, sayingPhrase.getText()));
messageAdapter.notifyItemInserted(messageItemList.size());
}
});
chat.addOnFallbackReplyFoundForListener(input -> {
Log.i(TAG, "[CHAT] Fallback Reply found for user message: " + input.getText());
});
chat.addOnNoReplyFoundForListener(input -> {
Log.i(TAG, "[CHAT] NO Reply found for user message: " + input.getText());
});
return chat;
}
我尝试使用回复方式@Victor Paléologue提到:
Future<ReplyReaction> replyFuture = chatbot.async().replyTo(userPhrase, localeEN);
replyFuture.thenConsume(future -> {
if (future.hasError()) {
Log.e(TAG, "Reply Future [ERROR]: " + future.getErrorMessage());
} else {
Log.i(TAG, "Reply Future [SUCCESS]: ");
ReplyReaction replyReaction = future.get();
ChatbotReaction chatbotReaction = replyReaction.getChatbotReaction();
chatbotReaction.runWith(speechEngine);
}
});
此代码不会产生任何错误。如果聊天机器人运行的
userPhrase
中不存在 Topic
,则会在第一个 if
子句中捕获错误。
如果聊天机器人知道该短语,则以下代码似乎可以运行,但我没有收到任何反馈。由于某种原因,聊天机器人实际上并没有得到这个短语,因为如果是的话,我会通过以下方法看到日志。
chatbot.addOnBookmarkReachedListener(bookmark -> {
Log.i(TAG, "Bookmark " + bookmark.getName() + " reached.");
});
chatbot.replyTo
返回回复对象。它有一个 chatbotReaction
,您可以 runWith(SpeechEngine engine)
。 语音引擎由 Conversation
服务提供。
如何使用它很棘手并且没有很好的文档记录。 理论上这是可行的:
RobotContext robotContext = qiContext.getRobotContext();
Conversation conversation = qiContext.getConversation();
SpeechEngine speechEngine = conversation.makeSpeechEngine(robotContext);
ReplyReaction replyReaction = chatbot.replyTo(userPhrase, localeEN);
ChatbotReaction reaction = replyReaction.getChatbotReaction();
reaction.runWith(speechEngine);