Skip to content

Commit

Permalink
Merge pull request #9053 from RocketChat/livechat-external-queue
Browse files Browse the repository at this point in the history
[NEW] Add support to external livechat queue service provider
  • Loading branch information
sampaiodiego authored Dec 27, 2017
2 parents ec8679c + 338e65a commit 02b6b79
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 28 deletions.
4 changes: 4 additions & 0 deletions packages/rocketchat-i18n/i18n/en.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,9 @@
"Example_s": "Example: <code class=\"inline\">%s</code>",
"Exclude_Botnames": "Exclude Bots",
"Exclude_Botnames_Description": "Do not propagate messages from bots whose name matches the regular expression above. If left empty, all messages from bots will be propagated.",
"External_Service": "External Service",
"External_Queue_Service_URL": "External Queue Service URL",
"Facebook_Page": "Facebook Page",
"False": "False",
"Favorite_Rooms": "Enable Favorite Rooms",
"Favorites": "Favorites",
Expand Down Expand Up @@ -714,6 +717,7 @@
"Food_and_Drink": "Food & Drink",
"Footer": "Footer",
"Footer_Direct_Reply": "Footer When Direct Reply is Enabled",
"For_more_details_please_check_our_docs": "For more details please check our docs.",
"For_your_security_you_must_enter_your_current_password_to_continue": "For your security, you must enter your current password to continue",
"force-delete-message": "Force Delete Message",
"force-delete-message_description": "Permission to delete a message bypassing all restrictions",
Expand Down
74 changes: 49 additions & 25 deletions packages/rocketchat-livechat/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,31 +186,6 @@ Meteor.startup(function() {
]
});

RocketChat.settings.add('Livechat_Routing_Method', 'Least_Amount', {
type: 'select',
group: 'Livechat',
public: true,
values: [
{key: 'Least_Amount', i18nLabel: 'Least_Amount'},
{key: 'Guest_Pool', i18nLabel: 'Guest_Pool'}
]
});

RocketChat.settings.add('Livechat_guest_pool_with_no_agents', false, {
type: 'boolean',
group: 'Livechat',
i18nLabel: 'Accept_with_no_online_agents',
i18nDescription: 'Accept_incoming_livechat_requests_even_if_there_are_no_online_agents',
enableQuery: { _id: 'Livechat_Routing_Method', value: 'Guest_Pool' }
});

RocketChat.settings.add('Livechat_show_queue_list_link', false, {
type: 'boolean',
group: 'Livechat',
public: true,
i18nLabel: 'Show_queue_list_to_all_agents'
});

RocketChat.settings.add('Livechat_enable_office_hours', false, {
type: 'boolean',
group: 'Livechat',
Expand Down Expand Up @@ -285,4 +260,53 @@ Meteor.startup(function() {
section: 'RD Station',
i18nLabel: 'RDStation_Token'
});

RocketChat.settings.add('Livechat_Routing_Method', 'Least_Amount', {
type: 'select',
group: 'Livechat',
public: true,
section: 'Routing',
values: [
{key: 'External', i18nLabel: 'External_Service'},
{key: 'Least_Amount', i18nLabel: 'Least_Amount'},
{key: 'Guest_Pool', i18nLabel: 'Guest_Pool'}
]
});

RocketChat.settings.add('Livechat_guest_pool_with_no_agents', false, {
type: 'boolean',
group: 'Livechat',
section: 'Routing',
i18nLabel: 'Accept_with_no_online_agents',
i18nDescription: 'Accept_incoming_livechat_requests_even_if_there_are_no_online_agents',
enableQuery: { _id: 'Livechat_Routing_Method', value: 'Guest_Pool' }
});

RocketChat.settings.add('Livechat_show_queue_list_link', false, {
type: 'boolean',
group: 'Livechat',
public: true,
section: 'Routing',
i18nLabel: 'Show_queue_list_to_all_agents',
enableQuery: { _id: 'Livechat_Routing_Method', value: { $ne: 'External' } }
});

RocketChat.settings.add('Livechat_External_Queue_URL', '', {
type: 'string',
group: 'Livechat',
public: false,
section: 'Routing',
i18nLabel: 'External_Queue_Service_URL',
i18nDescription: 'For_more_details_please_check_our_docs',
enableQuery: { _id: 'Livechat_Routing_Method', value: 'External' }
});

RocketChat.settings.add('Livechat_External_Queue_Token', '', {
type: 'string',
group: 'Livechat',
public: false,
section: 'Routing',
i18nLabel: 'Secret_token',
enableQuery: { _id: 'Livechat_Routing_Method', value: 'External' }
});
});
33 changes: 30 additions & 3 deletions packages/rocketchat-livechat/server/lib/Livechat.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,38 @@ RocketChat.Livechat = {
}),

getNextAgent(department) {
if (department) {
if (RocketChat.settings.get('Livechat_Routing_Method') === 'External') {
for (let i = 0; i < 10; i++) {
try {
const queryString = department ? `?departmentId=${ department }` : '';
const result = HTTP.call('GET', `${ RocketChat.settings.get('Livechat_External_Queue_URL') }${ queryString }`, {
headers: {
'User-Agent': 'RocketChat Server',
'Accept': 'application/json',
'X-RocketChat-Secret-Token': RocketChat.settings.get('Livechat_External_Queue_Token')
}
});

if (result && result.data && result.data.username) {
const agent = RocketChat.models.Users.findOneOnlineAgentByUsername(result.data.username);

if (agent) {
return {
agentId: agent._id,
username: agent.username
};
}
}
} catch (e) {
console.error('Error requesting agent from external queue.', e);
break;
}
}
throw new Meteor.Error('no-agent-online', 'Sorry, no online agents');
} else if (department) {
return RocketChat.models.LivechatDepartmentAgents.getNextAgentForDepartment(department);
} else {
return RocketChat.models.Users.getNextAgent();
}
return RocketChat.models.Users.getNextAgent();
},
getAgents(department) {
if (department) {
Expand Down
3 changes: 3 additions & 0 deletions packages/rocketchat-livechat/server/lib/QueueMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,8 @@ RocketChat.QueueMethods = {
RocketChat.models.Rooms.insert(room);

return room;
},
'External'(guest, message, roomInfo) {
return this['Least_Amount'](guest, message, roomInfo); // eslint-disable-line
}
};
18 changes: 18 additions & 0 deletions packages/rocketchat-livechat/server/models/Users.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ RocketChat.models.Users.findOnlineAgents = function() {
return this.find(query);
};

/**
* Find an online agent by his username
* @return
*/
RocketChat.models.Users.findOneOnlineAgentByUsername = function(username) {
const query = {
username,
status: {
$exists: true,
$ne: 'offline'
},
statusLivechat: 'available',
roles: 'livechat-agent'
};

return this.findOne(query);
};

/**
* Gets all agents
* @return
Expand Down

0 comments on commit 02b6b79

Please sign in to comment.