Search code examples
chatbotpepper

How can I programmatically end a Chat on Pepper?


I have a simple chatbot with the below topic file and would like to end the chat (and stop Pepper from listening) when the user says a key phrase (e.g. "bye).

u: (hello) hi

u: (my name is _*) nice to meet you $1

u: (bye) goodbye %endChat

However i haven't found any action (smth like chat.end()) and the docs state "If you want to stop the chat when a qiChatbot is stopped you must track the state of the QiChatbot and manually stop the chat when the QiChatbot ends."

Any idea how / what event to listen on to do this? I'm using the Android / Java based version of Pepper.


Solution

  • You can use the ^endDiscuss keyword in QiChat to end the current chat.

    In QiChat:

    u: (hello) hi
    u: (my name is _*) nice to meet you $1
    u: (bye) goodbye ^endDiscuss(endReason)
    

    Then listen for the QiChatbot ended event like this in your app and, when it is triggered, close the chat. Like the docs say, you can pass an ending reason from the QiChat if you have multiple ending conditions and want to do different things.

    In Java:

    // Create a QiChatbot
    final QiChatbot qichatbot = QiChatbotBuilder.with(qiContext)
                                                .withTopic(topic)
                                                .build();
    
    // Create a Chat
    final Chat chat = ChatBuilder.with(qiContext)
                                 .withChatbot(qichatbot)
                                 .build();
    
    // Execute the chat asynchronously
    final Future<Void> fchat = chat.async().run();
    
    // Stop the chat when the qichatbot is done
    qichatbot.addOnEndedListener(endReason -> {
        Log.i(TAG, "qichatbot end reason = " + endReason);
        fchat.requestCancellation();
    });