📄 chatbot.java
字号:
validAnswer = true;
}
if (validAnswer) {
// Store the answer since it's a valid answer
session.putAttribute(field.getVariable(), answers);
}
return validAnswer;
}
/**
* Returns true if the message sent by the user requested to run a command. Depending on the
* stage of the conversation different commands may be executed.
*
* @param message the message.
* @param session the session.
* @return true if the message sent by the user requested to run a command.
*/
private boolean handleCommand(Message message, ChatbotSession session) {
String command = message.getBody().trim();
if (getHelpCommand().equalsIgnoreCase(command)) {
sendHelpMessage(message);
return true;
}
else if (getByeCommand().equalsIgnoreCase(command)) {
userDepartQueue(message);
return true;
}
if (session.getCurrentStep() == 1) {
if (getRepeatCommand().equalsIgnoreCase(command)) {
// Send the join question
sendJoinQuestion(message, session);
return true;
}
else if (getPositionCommand().equalsIgnoreCase(command)) {
// Tell the user that he is not waiting in the queue
sendReply(message, getNotInQueueMessage());
return true;
}
}
else if (session.getCurrentStep() == 2) {
if (getBackCommand().equalsIgnoreCase(command)) {
sendPreviousQuestion(message, session);
return true;
}
else if (getRepeatCommand().equalsIgnoreCase(command)) {
// Resend the last question
repeatQuestion(message, session);
return true;
}
else if (getPositionCommand().equalsIgnoreCase(command)) {
// Tell the user that he is not waiting in the queue
sendReply(message, getNotInQueueMessage());
return true;
}
}
else if (session.getCurrentStep() == 3) {
if (getPositionCommand().equalsIgnoreCase(command)) {
try {
UserRequest request = UserRequest.getRequest(workgroup, message.getFrom());
request.updateQueueStatus(true);
}
catch (NotFoundException e) {
// Tell the user that he is not waiting in the queue
sendReply(message, getNotInQueueMessage());
}
return true;
}
}
else if (session.getCurrentStep() == 6) {
if (getRepeatCommand().equalsIgnoreCase(command)) {
// Resend the last question
sendEmailQuestion(message.getFrom(), session);
return true;
}
}
else if (session.getCurrentStep() == 7) {
if (getRepeatCommand().equalsIgnoreCase(command)) {
// Resend the last question
sendGetEmailQuestion(message, session);
return true;
}
}
return false;
}
private void sendReply(Message message, String reply) {
Message packet = new Message();
packet.setTo(message.getFrom());
packet.setFrom(message.getTo());
packet.setThread(message.getThread());
packet.setType(message.getType());
packet.setBody(reply);
send(packet);
}
private void sendMessage(JID receiver, String thread, String body) {
Message packet = new Message();
packet.setTo(receiver);
packet.setFrom(workgroup.getJID());
packet.setThread(thread);
if (thread != null) {
packet.setType(Message.Type.chat);
}
packet.setBody(body);
send(packet);
}
private void send(Message packet) {
InterceptorManager interceptorManager = ChatbotInterceptorManager.getInstance();
try {
interceptorManager.invokeInterceptors(workgroup.getJID().toBareJID(), packet, false,
false);
workgroup.send(packet);
interceptorManager.invokeInterceptors(workgroup.getJID().toBareJID(), packet, false,
true);
}
catch (PacketRejectedException e) {
ComponentManagerFactory.getComponentManager().getLog().warn("Packet was not sent " +
"due to interceptor REJECTION: " + packet.toXML(), e);
}
}
public void notifyQueueStatus(JID sender, JID receiver, UserRequest request, boolean isPolling) {
// Get the chatbot session of the user
ChatbotSession session = getSession(receiver, false);
if (session != null) {
Message packet = new Message();
packet.setTo(receiver);
packet.setFrom(sender);
packet.setThread(session.getMessageThread());
if (session.getMessageThread() != null) {
packet.setType(Message.Type.chat);
}
String body = getPositionMessage().replace("${position}",
String.valueOf(request.getPosition() + 1));
body = body.replace("${waitTime}", String.valueOf(request.getTimeStatus()));
packet.setBody(body);
send(packet);
}
}
public void notifyQueueDepartued(JID sender, JID receiver, UserRequest request,
Request.CancelType type) {
// Get the chatbot session of the user
ChatbotSession session = getSession(receiver, false);
if (session != null) {
Message packet = new Message();
packet.setTo(receiver);
packet.setFrom(sender);
packet.setThread(session.getMessageThread());
if (session.getMessageThread() != null) {
packet.setType(Message.Type.chat);
}
packet.setBody(getDepartureConfirmedMessage());
send(packet);
// Remove the session of this user
removeSession(receiver);
}
}
public void invitationsSent(UserRequest request) {
JID receiver = request.getUserJID();
// Get the chatbot session of the user
ChatbotSession session = getSession(receiver, false);
if (session != null) {
session.setCurrentStep(4);
// Send a notification saying that an invitation has been sent to start a chat
// with an agent
sendMessage(receiver, session.getMessageThread(), getInvitationSentMessage());
}
}
public void checkInvitation(UserRequest request) {
JID receiver = request.getUserJID();
// Get the chatbot session of the user
ChatbotSession session = getSession(receiver, false);
if (session != null) {
// Ask the user if he wants to receive a new invitation
sendInvitationQuestion(receiver, session);
}
}
public void supportStarted(UserRequest request) {
JID receiver = request.getUserJID();
// Get the chatbot session of the user
ChatbotSession session = getSession(receiver, false);
if (session != null) {
session.setStartedSupport(true);
session.setCurrentStep(5);
}
}
public void supportEnded(UserRequest request) {
JID receiver = request.getUserJID();
// Get the chatbot session of the user
ChatbotSession session = getSession(receiver, false);
if (session != null) {
// Set that the session's user has finished the chat with an agent
session.setStartedSupport(false);
// Ask the user if he wants to receive the chat transcript by email
sendEmailQuestion(receiver, session);
}
}
/**
* Returns the welcome message to send to the user after the user sent his first message.
*
* @return the welcome message to send to the user after the user sent his first message.
*/
private String getWelcomeMessage() {
return settings.getChatSetting(KeyEnum.welcome_message).getValue();
}
/**
* Returns the question to send to the user asking if he wants to join the workgroup.
*
* @return the question to send to the user asking if he wants to join the workgroup.
*/
private String getJoinQuestion() {
return settings.getChatSetting(KeyEnum.join_question).getValue();
}
/**
* Returns the message to send if the user does not want to join the workgroup.
*
* @return the message to send if the user does not want to join the workgroup.
*/
private String getByeMessage() {
return settings.getChatSetting(KeyEnum.bye_message).getValue();
}
/**
* Returns the message to send to the user informing that a form must be filled out. This
* message is optional.
*
* @return the message to send to the user informing that a form must be filled out.
*/
private String getFilloutFormMessage() {
return settings.getChatSetting(KeyEnum.fillout_form_message).getValue();
}
public WorkgroupForm getForm() {
return FormManager.getInstance().getWebForm(workgroup);
}
/**
* Returns the message to inform the user that he has entered a waiting queue and
* that an agent will be with him in a moment.
*
* @return the message to inform the user that he has entered a waiting queue and
* that an agent will be with him in a moment.
*/
private String getRoutingMessage() {
return settings.getChatSetting(KeyEnum.routing_message).getValue();
}
/**
* Returns the message that informs the user his position in the waiting queue.
*
* @return the message that informs the user his position in the waiting queue.
*/
private String getPositionMessage() {
return settings.getChatSetting(KeyEnum.position_message).getValue();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -