BaseQiChatExecutor


level_3


BaseQiChatExecutor

A BaseQiChatbotExecutor allows you to define a piece of code that will be triggered from a QiChat topic thanks to an ^execute function.

For Further details, see: ^execute.

How to use it

Topic Example

topic: ~execute()

u: (raise your _[right left] arm) I raise my $parameter1 arm ^execute(myExecutor, $parameter1, constantParameter2) then I resume talking

When reaching the ^execute in the conversation, myExecutor’s runWith(List<String> params) method will be called passing $parameter1 and constantParameter2. You can have as many parameters as you want.

Mapping the QiChatExecutor to QiChatbot

Create a Topic containing ^execute then make a QiChatbot.

Add one or more executors using different keywords used in the topic. Finally run the chat asynchronously.

override fun onRobotFocusGained(qiContext: QiContext) {

    // 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
    executors.put("myExecutor", MyQiChatExecutor(qiContext))

    // 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()
}
@Override
public void onRobotFocusGained(QiContext qiContext) {

    // Create a topic.
    final Topic topic = TopicBuilder.with(qiContext)
            .withResource(R.raw.execute)
            .build();

    // Create a qiChatbot
    QiChatbot qiChatbot = QiChatbotBuilder.with(qiContext).withTopic(topic).build();

    Map<String, QiChatExecutor> executors = new HashMap<>();

    // Map the executor name from the topic to our qiChatExecutor
    executors.put("myExecutor", new MyQiChatExecutor(qiContext));

    // Set the executors to the qiChatbot
    qiChatbot.setExecutors(executors);

    // Build chat with the chatbotBuilder
    Chat chat = ChatBuilder.with(qiContext).withChatbot(qiChatbot).build();

    // Run an action asynchronously.
    chat.async().run();
}

QiChatExecutor class implementation

Create a class MyQiChatExecutor that extends from BaseQiChatExecutor, and implements runWith and stop.

  • runWith will be called when ^execute is reached in the topic.
  • stop will be called when the chat that handles the qiChatbot is canceled.
class MyQiChatExecutor(context: QiContext) : BaseQiChatExecutor(context) {

    override fun runWith(params: List<String>) {
        // This is called when execute is reached in the topic
         Log.i(TAG, "Arm raised = ${params[0]}")
    }

    override fun stop() {
            // This is called when chat is canceled or stopped.
            Log.i(TAG, "execute stopped")
    }
}
class MyQiChatExecutor extends BaseQiChatExecutor {

    MyQiChatExecutor(QiContext context) {
        super(context);
    }

    @Override
    public void runWith(List<String> params) {
        // This is called when execute is reached in the topic
         Log.i(TAG, "Arm raised = " + params.get(0));
    }

    @Override
    public void stop() {
        // This is called when chat is canceled or stopped.
        Log.i(TAG, "execute stopped");
    }
}

More