⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chatbot.java

📁 openfire 服务器源码下载
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
                        closeSession(message);
                    }
                    else {
                        // The user sent an unknown answer so repeat the email question
                        sendEmailQuestion(message.getFrom(), session);
                    }
                }
                else if (session.getCurrentStep() == 7) {
                    // Send the transcript by email
                    sendTranscriptByMail(message.getBody().trim(), message, session);
                    // Send a goodbye message and close the chat session
                    closeSession(message);
                }
                else {
                    // User is waiting in a queue and the sent message contains an unkown content
                    // so send message saying that the command was not acceptable (i.e. unknown command)
                    sendReply(message, getNotAcceptableMessage());
                }
            }
            interceptorManager.invokeInterceptors(workgroup.getJID().toBareJID(), message, true,
                    true);
        }
        catch (PacketRejectedException e) {
            workgroup.rejectPacket(message, e);
        }
    }

    private void userJoinQueue(Message message, ChatbotSession session) {
        InterceptorManager interceptorManager = QueueInterceptorManager.getInstance();
        try {
            interceptorManager.invokeInterceptors(workgroup.getJID().toBareJID(), message, true,
                    false);
            if (getRoutingMessage() != null && getRoutingMessage().length() > 0) {
                sendReply(message, getRoutingMessage());
            }
            // Set that we are currently joining a waiting queue
            session.setCurrentStep(3);

            // Received a Join Queue request from a visitor, create a new request.
            UserRequest request = new UserRequest(session, workgroup);
            // Let the workgroup process the new request
            if (!workgroup.queueRequest(request)) {
                // It was not possible to add the request to a queue so send message saying that
                // the workgroup is not accepting new join requests
                sendReply(message, getCannotJoinMessage());
                // Send the goodbye message and close the session
                closeSession(message);
            }
            else {
                session.setRequest(request);
            }
            interceptorManager.invokeInterceptors(workgroup.getJID().toBareJID(), message, true,
                    true);
        }
        catch (PacketRejectedException e) {
            workgroup.rejectPacket(message, e);
        }
    }

    private void userDepartQueue(Message message) {
        // Remove the user from the queue if he was waiting in the queue
        try {
            Request request = UserRequest.getRequest(workgroup, message.getFrom());
            InterceptorManager interceptorManager = QueueInterceptorManager.getInstance();
            try {
                interceptorManager.invokeInterceptors(workgroup.getJID().toBareJID(), message, true,
                        false);
                request.cancel(Request.CancelType.DEPART);
                // Remove the session (the goodbye message is sent when leaving the queue)
                removeSession(message.getFrom());
                interceptorManager.invokeInterceptors(workgroup.getJID().toBareJID(), message, true,
                        true);
            }
            catch (PacketRejectedException e) {
                workgroup.rejectPacket(message, e);
            }
        }
        catch (NotFoundException e) {
            // Send the goodbye message and close the session
            closeSession(message);
        }
    }

    private void sendWelcomeMessage(Message message) {
        String welcomeMessage = getWelcomeMessage();
        welcomeMessage = welcomeMessage.replace("${workgroup}", workgroup.getJID().toString());
        sendReply(message, welcomeMessage);
    }

    private void sendJoinQuestion(Message message, ChatbotSession session) {
        sendReply(message, getJoinQuestion());
        // Set that we are currently waiting for a response to the join question
        session.setCurrentStep(1);
    }

    private void sendPreviousQuestion(Message message, ChatbotSession session) {
        if (session.getCurrentSubstep() == 0) {
            sendJoinQuestion(message, session);
        }
        else {
            // Send the next question to the user
            sendQuestion(message, session, session.getCurrentSubstep()-1);
        }
    }

    private void sendNextQuestion(Message message, ChatbotSession session) {
        // Send the next question to the user
        sendQuestion(message, session, session.getCurrentSubstep()+1);
    }

    private void sendEmailQuestion(JID receiver, ChatbotSession session) {
        // Ask the user if he wants to receive the chat transcript by email
        session.setCurrentStep(6);
        sendMessage(receiver, session.getMessageThread(), getSendEmailQuestion());
    }

    private void sendRoomInvitation(Message message, ChatbotSession session) {
        UserRequest request = session.getRequest();
        // Send again the room invitation to the user
        workgroup.sendUserInvitiation(request, request.getInvitedRoomID());
        // Send a confirmation to the user notifying that the invitation was sent
        sendMessage(message.getFrom(), session.getMessageThread(), getInvitationResentMessage());
    }

    public void sendInvitationQuestion(JID receiver, ChatbotSession session) {
        sendMessage(receiver, session.getMessageThread(), getSendInvitationQuestion());
    }

    private void sendGetEmailQuestion(Message message, ChatbotSession session) {
        // Ask the user for his email address
        session.setCurrentStep(7);
        sendMessage(message.getFrom(), session.getMessageThread(), getGetEmailQuestion());
    }

    private void sendTranscriptByMail(String email, Message message, ChatbotSession session) {
        // Send the transcript by email
        ChatTranscriptManager.sendTranscriptByMail(session.getRequest().getSessionID(), email);
        // Confirm the user that the email was sent to his email address
        sendEmailSentConfirmation(email, message);
    }

    private void sendEmailSentConfirmation(String email, Message message) {
        String emailSentMessage = getEmailSentMessage();
        emailSentMessage = emailSentMessage.replace("${email}", email);
        sendReply(message, emailSentMessage);
    }

    private void sendHelpMessage(Message message) {
        sendReply(message, getHelpHelpMessage());
        sendReply(message, getBackHelpMessage());
        sendReply(message, getRepeatHelpMessage());
        sendReply(message, getByeHelpMessage());
        sendReply(message, getPositionHelpMessage());
    }

    /**
     * Repeat the last question to the user. This may happen either if because the user requested
     * it or because the user sent an invalid answer.
     *
     * @param message the sent message by the user.
     * @param session the session that keeps the state of the user chat.
     */
    private void repeatQuestion(Message message, ChatbotSession session) {
        // Resend the last question to the user
        sendQuestion(message, session, session.getCurrentSubstep());
    }

    private void userAcceptedJoining(Message message, ChatbotSession session) {
        if (getForm() != null && !getForm().getFormElements().isEmpty()) {
            if (getFilloutFormMessage() != null && getFilloutFormMessage().length() > 0) {
                // Send the message informing that a form must be filled out
                sendReply(message, getFilloutFormMessage());
            }
            session.setCurrentStep(2);
            // Send the first question to the user
            sendQuestion(message, session, 0);
        }
        else {
            // Since there is no form to fill out just join the queue
            userJoinQueue(message, session);
        }
    }

    private void closeSession(Message message) {
        sendReply(message, getByeMessage());
        // Remove the session of this user
        removeSession(message.getFrom());
    }

    private void removeSession(JID user) {
        // Remove the session of this user
        sessions.remove(user.toString());
    }

    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);
    }

    /**
     * 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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -