Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/livechat/server/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,14 @@ Meteor.startup(function() {
i18nDescription: 'Accept_incoming_livechat_requests_even_if_there_are_no_online_agents',
});

settings.add('Livechat_assign_new_conversation_to_bot', false, {
type: 'boolean',
group: 'Livechat',
section: 'Routing',
i18nLabel: 'Assign_new_conversations_to_bot_agent',
i18nDescription: 'Assign_new_conversations_to_bot_agent_description',
});

settings.add('Livechat_guest_pool_max_number_incoming_livechats_displayed', 0, {
type: 'int',
group: 'Livechat',
Expand Down
16 changes: 16 additions & 0 deletions app/livechat/server/hooks/beforeGetNextAgent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

import { callbacks } from '../../../callbacks';
import { settings } from '../../../settings';
import { Users, LivechatDepartmentAgents } from '../../../models';

callbacks.add('livechat.beforeGetNextAgent', (department) => {
if (!settings.get('Livechat_assign_new_conversation_to_bot')) {
return null;
}

if (department) {
return LivechatDepartmentAgents.getNextBotForDepartment(department);
}

return Users.getNextBotAgent();
}, callbacks.priority.HIGH, 'livechat-before-get-next-agent');
1 change: 1 addition & 0 deletions app/livechat/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import './permissions';
import '../lib/messageTypes';
import './config';
import './roomType';
import './hooks/beforeGetNextAgent';
import './hooks/externalMessage';
import './hooks/leadCapture';
import './hooks/markRoomResponded';
Expand Down
23 changes: 22 additions & 1 deletion app/livechat/server/lib/Livechat.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,23 @@ export const Livechat = {
}),

online() {
if (settings.get('Livechat_accept_chats_with_no_agents')) {
return true;
}

if (settings.get('Livechat_assign_new_conversation_to_bot')) {
const botAgents = Livechat.getBotAgents();
if (botAgents && botAgents.count() > 0) {
return true;
}
}

const onlineAgents = Livechat.getOnlineAgents();
return (onlineAgents && onlineAgents.count() > 0) || settings.get('Livechat_accept_chats_with_no_agents');
},

getNextAgent(department) {
return RoutingManager.getMethod().getNextAgent(department);
return RoutingManager.getNextAgent(department);
},

getAgents(department) {
Expand All @@ -62,12 +73,22 @@ export const Livechat = {
}
return Users.findAgents();
},

getOnlineAgents(department) {
if (department) {
return LivechatDepartmentAgents.getOnlineForDepartment(department);
}
return Users.findOnlineAgents();
},

getBotAgents(department) {
if (department) {
return LivechatDepartmentAgents.getBotsForDepartment(department);
}

return Users.findBotAgents();
},

getRequiredDepartment(onlineRequired = true) {
const departments = LivechatDepartment.findEnabledWithAgents();

Expand Down
2 changes: 1 addition & 1 deletion app/livechat/server/lib/QueueManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const QueueManager = {
agent = RoutingManager.getMethod().delegateAgent(agent, inquiry);
}

inquiry = await callbacks.run('livechat.beforeRouteChat', inquiry);
inquiry = await callbacks.run('livechat.beforeRouteChat', inquiry, agent);
if (inquiry.status !== 'ready') {
return room;
}
Expand Down
15 changes: 12 additions & 3 deletions app/livechat/server/lib/RoutingManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,28 @@ export const RoutingManager = {
return this.getMethod().config || {};
},

async getNextAgent(department) {
let agent = callbacks.run('livechat.beforeGetNextAgent', department);

if (!agent) {
agent = await this.getMethod().getNextAgent(department);
}

return agent;
},

async delegateInquiry(inquiry, agent) {
// return Room Object
const { department, rid } = inquiry;
if (!agent || (agent.username && !Users.findOneOnlineAgentByUsername(agent.username))) {
agent = await this.getMethod().getNextAgent(department);
agent = await this.getNextAgent(department);
}

if (!agent) {
return LivechatRooms.findOneById(rid);
}

const room = this.takeInquiry(inquiry, agent);
return room;
return this.takeInquiry(inquiry, agent);
},

assignAgent(inquiry, agent) {
Expand Down
62 changes: 62 additions & 0 deletions app/models/server/models/LivechatDepartmentAgents.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export class LivechatDepartmentAgents extends Base {

this.tryEnsureIndex({ departmentId: 1 });
this.tryEnsureIndex({ agentId: 1 });
this.tryEnsureIndex({ username: 1 });
}

findByDepartmentId(departmentId) {
Expand Down Expand Up @@ -102,6 +103,67 @@ export class LivechatDepartmentAgents extends Base {
return this.find(query);
}

getBotsForDepartment(departmentId) {
const agents = this.findByDepartmentId(departmentId).fetch();

if (agents.length === 0) {
return;
}

const botUsers = Users.findBotAgents(_.pluck(agents, 'username'));
const botUsernames = _.pluck(botUsers.fetch(), 'username');

const query = {
departmentId,
username: {
$in: botUsernames,
},
};

return this.find(query);
}

getNextBotForDepartment(departmentId) {
const agents = this.findByDepartmentId(departmentId).fetch();

if (agents.length === 0) {
return;
}

const botUsers = Users.findBotAgents(_.pluck(agents, 'username'));
const botUsernames = _.pluck(botUsers.fetch(), 'username');

const query = {
departmentId,
username: {
$in: botUsernames,
},
};

const sort = {
count: 1,
order: 1,
username: 1,
};
const update = {
$inc: {
count: 1,
},
};

const collectionObj = this.model.rawCollection();
const findAndModify = Meteor.wrapAsync(collectionObj.findAndModify, collectionObj);

const bot = findAndModify(query, sort, update);
if (bot && bot.value) {
return {
agentId: bot.value.agentId,
username: bot.value.username,
};
}
return null;
}

findUsersInQueue(usersList) {
const query = {};

Expand Down
56 changes: 56 additions & 0 deletions app/models/server/models/Users.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,31 @@ export class Users extends Base {
return this.find(query);
}

findBotAgents(usernameList) {
const query = {
roles: {
$all: ['bot', 'livechat-agent'],
},
...usernameList && {
username: {
$in: [].concat(usernameList),
},
},
};

return this.find(query);
}

findOneBotAgent() {
const query = {
roles: {
$all: ['bot', 'livechat-agent'],
},
};

return this.findOne(query);
}

findOneOnlineAgentByUsername(username) {
const query = {
username,
Expand Down Expand Up @@ -172,6 +197,37 @@ export class Users extends Base {
return null;
}

getNextBotAgent() {
const query = {
roles: {
$all: ['bot', 'livechat-agent'],
},
};

const collectionObj = this.model.rawCollection();
const findAndModify = Meteor.wrapAsync(collectionObj.findAndModify, collectionObj);

const sort = {
livechatCount: 1,
username: 1,
};

const update = {
$inc: {
livechatCount: 1,
},
};

const user = findAndModify(query, sort, update);
if (user && user.value) {
return {
agentId: user.value._id,
username: user.value.username,
};
}
return null;
}

setLastRoutingTime(userId) {
const query = {
_id: userId,
Expand Down
2 changes: 2 additions & 0 deletions packages/rocketchat-i18n/i18n/en.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,8 @@
"assign-admin-role": "Assign Admin Role",
"assign-admin-role_description": "Permission to assign the admin role to other users",
"Assign_admin": "Assigning admin",
"Assign_new_conversations_to_bot_agent": "Assign new conversations to bot agent",
"Assign_new_conversations_to_bot_agent_description": "The routing system will attempt to find a bot agent before addressing new conversations to a human agent.",
"assign-roles": "Assign Roles",
"at": "at",
"At_least_one_added_token_is_required_by_the_user": "At least one added token is required by the user",
Expand Down
2 changes: 2 additions & 0 deletions packages/rocketchat-i18n/i18n/pt-BR.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,8 @@
"assign-admin-role": "Atribuir papel de administrador",
"assign-admin-role_description": "Permissão para atribuir a função de administrador a outros usuários",
"Assign_admin": "Atribuindo administrador",
"Assign_new_conversations_to_bot_agent": "Atribuir novas conversas para um agente bot",
"Assign_new_conversations_to_bot_agent_description": "O sistema de roteamento irá procurar por um agente bot antes de encaminhar novas conversas para um agente humano.",
"assign-roles": "Atribuir papéis",
"at": "em",
"At_least_one_added_token_is_required_by_the_user": "Pelo menos um token adicionado é requerido pelo usuário",
Expand Down