Search in sources :

Example 1 with FormElement

use of org.jivesoftware.openfire.fastpath.dataforms.FormElement in project Openfire by igniterealtime.

the class Chatbot method sendQuestion.

private void sendQuestion(Message message, ChatbotSession session, int position) {
    FormElement field = getForm().getFormElementAt(position);
    if (field == null) {
        return;
    }
    if (field.getAnswerType() == WorkgroupForm.FormEnum.hidden) {
        // Auto accept hidden fields
        Message fakeMessage = message.createCopy();
        StringBuilder builder = new StringBuilder();
        for (Iterator<String> it = field.getAnswers().iterator(); it.hasNext(); ) {
            builder.append(it.next());
            if (it.hasNext()) {
                builder.append("/");
            }
        }
        fakeMessage.setBody(builder.toString());
        // Set that we are currently waiting for a response to the next question
        session.setCurrentSubstep(position);
        // Simulate that the user sent this message (with the hidden field)
        onMessage(session, fakeMessage);
    }
    String text = field.getLabel();
    if (field.getAnswerType() == WorkgroupForm.FormEnum.radio_button || field.getAnswerType() == WorkgroupForm.FormEnum.dropdown_box || field.getAnswerType() == WorkgroupForm.FormEnum.checkbox) {
        // Append the options to the message body
        if (!field.getAnswers().isEmpty()) {
            StringBuilder builder = new StringBuilder(text);
            builder.append(" [");
            builder.append(Request.encodeMetadataValue(field.getAnswers()));
            builder.append("]");
            text = builder.toString();
        }
    }
    sendReply(message, text);
    // Set that we are currently waiting for a response to the next question
    session.setCurrentSubstep(position);
}
Also used : Message(org.xmpp.packet.Message) FormElement(org.jivesoftware.openfire.fastpath.dataforms.FormElement)

Example 2 with FormElement

use of org.jivesoftware.openfire.fastpath.dataforms.FormElement in project Openfire by igniterealtime.

the class Chatbot method userAnsweredField.

/**
     * Returns true if the received message contains a valid answer for the current question.
     *
     * @param message the sent message by the user.
     * @param session the session that keeps the state of the user chat.
     * @return true if the received message contains a valid answer for the current question.
     */
private boolean userAnsweredField(Message message, ChatbotSession session) {
    boolean validAnswer = false;
    List<String> answers = Request.decodeMetadataValue(message.getBody().trim());
    FormElement field = getForm().getFormElementAt(session.getCurrentSubstep());
    List<String> options = field.getAnswers();
    if (!options.isEmpty()) {
        for (String answer : answers) {
            // Check that the each answer is an allowed option
            validAnswer = false;
            for (String option : options) {
                // Skip any CR character present in the option
                option = option.replace("\r", "");
                if (option.equalsIgnoreCase(answer)) {
                    validAnswer = true;
                    break;
                }
            }
            if (!validAnswer) {
                return false;
            }
        }
    } else {
        // The question allows any value so we accept this answer
        validAnswer = true;
    }
    if (validAnswer) {
        // Store the answer since it's a valid answer
        session.putAttribute(field.getVariable(), answers);
    }
    return validAnswer;
}
Also used : FormElement(org.jivesoftware.openfire.fastpath.dataforms.FormElement)

Aggregations

FormElement (org.jivesoftware.openfire.fastpath.dataforms.FormElement)2 Message (org.xmpp.packet.Message)1