diff --git a/packages/rocketchat-i18n/i18n/en.i18n.json b/packages/rocketchat-i18n/i18n/en.i18n.json index 706fefe310ed..6e661df1cb94 100644 --- a/packages/rocketchat-i18n/i18n/en.i18n.json +++ b/packages/rocketchat-i18n/i18n/en.i18n.json @@ -1709,6 +1709,8 @@ "Send_request_on_chat_close": "Send Request on Chat Close", "Send_request_on_lead_capture": "Send request on lead capture", "Send_request_on_offline_messages": "Send Request on Offline Messages", + "Send_request_on_visitor_message": "Send Request on Visitor Messages", + "Send_request_on_agent_message": "Send Request on Agent Messages", "Send_Test": "Send Test", "Send_welcome_email": "Send welcome email", "Send_your_JSON_payloads_to_this_URL": "Send your JSON payloads to this URL.", diff --git a/packages/rocketchat-i18n/i18n/pt-BR.i18n.json b/packages/rocketchat-i18n/i18n/pt-BR.i18n.json index d869357e085b..ca1915becd44 100644 --- a/packages/rocketchat-i18n/i18n/pt-BR.i18n.json +++ b/packages/rocketchat-i18n/i18n/pt-BR.i18n.json @@ -1033,6 +1033,8 @@ "Send_invitation_email_success": "Você enviou com sucesso um convite por e-mail para os seguintes endereços:", "Send_request_on_chat_close": "Enviar requisição ao fechar conversa", "Send_request_on_offline_messages": "Enviar requisição para mensagens off-line", + "Send_request_on_visitor_message": "Enviar requisição para mensagens do Visitante", + "Send_request_on_agent_message": "Enviar requisição para mensagens do Agente", "Send_Test": "Enviar teste", "Send_welcome_email": "Enviar e-mail de boas-vindas", "Send_your_JSON_payloads_to_this_URL": "Envie seu payload JSON para esta URL.", diff --git a/packages/rocketchat-livechat/client/views/app/integrations/livechatIntegrationWebhook.html b/packages/rocketchat-livechat/client/views/app/integrations/livechatIntegrationWebhook.html index 04bb8832d1ff..119d0f7859da 100644 --- a/packages/rocketchat-livechat/client/views/app/integrations/livechatIntegrationWebhook.html +++ b/packages/rocketchat-livechat/client/views/app/integrations/livechatIntegrationWebhook.html @@ -32,6 +32,18 @@

{{_ "Webhooks"}}

{{_ "Send_request_on_offline_messages"}} +
+ +
+
+ +
diff --git a/packages/rocketchat-livechat/client/views/app/integrations/livechatIntegrationWebhook.js b/packages/rocketchat-livechat/client/views/app/integrations/livechatIntegrationWebhook.js index 2a5602328364..cc373fb2f442 100644 --- a/packages/rocketchat-livechat/client/views/app/integrations/livechatIntegrationWebhook.js +++ b/packages/rocketchat-livechat/client/views/app/integrations/livechatIntegrationWebhook.js @@ -22,6 +22,14 @@ Template.livechatIntegrationWebhook.helpers({ sendOnOfflineChecked() { const setting = LivechatIntegration.findOne('Livechat_webhook_on_offline_msg'); return setting && setting.value; + }, + sendOnVisitorMessageChecked() { + const setting = LivechatIntegration.findOne('Livechat_webhook_on_visitor_message'); + return setting && setting.value; + }, + sendOnAgentMessageChecked() { + const setting = LivechatIntegration.findOne('Livechat_webhook_on_agent_message'); + return setting && setting.value; } }); @@ -62,11 +70,15 @@ Template.livechatIntegrationWebhook.events({ const secretToken = LivechatIntegration.findOne('Livechat_secret_token'); const webhookOnClose = LivechatIntegration.findOne('Livechat_webhook_on_close'); const webhookOnOfflineMsg = LivechatIntegration.findOne('Livechat_webhook_on_offline_msg'); + const webhookOnVisitorMessage = LivechatIntegration.findOne('Livechat_webhook_on_visitor_message'); + const webhookOnAgentMessage = LivechatIntegration.findOne('Livechat_webhook_on_agent_message'); instance.$('#webhookUrl').val(webhookUrl && webhookUrl.value); instance.$('#secretToken').val(secretToken && secretToken.value); instance.$('#sendOnClose').get(0).checked = webhookOnClose && webhookOnClose.value; instance.$('#sendOnOffline').get(0).checked = webhookOnOfflineMsg && webhookOnOfflineMsg.value; + instance.$('#sendOnVisitorMessage').get(0).checked = webhookOnVisitorMessage && webhookOnVisitorMessage.value; + instance.$('#sendOnAgentMessage').get(0).checked = webhookOnAgentMessage && webhookOnAgentMessage.value; instance.disableTest.set(!webhookUrl || _.isEmpty(webhookUrl.value)); }, @@ -77,7 +89,9 @@ Template.livechatIntegrationWebhook.events({ 'Livechat_webhookUrl': s.trim(instance.$('#webhookUrl').val()), 'Livechat_secret_token': s.trim(instance.$('#secretToken').val()), 'Livechat_webhook_on_close': instance.$('#sendOnClose').get(0).checked, - 'Livechat_webhook_on_offline_msg': instance.$('#sendOnOffline').get(0).checked + 'Livechat_webhook_on_offline_msg': instance.$('#sendOnOffline').get(0).checked, + 'Livechat_webhook_on_visitor_message': instance.$('#sendOnVisitorMessage').get(0).checked, + 'Livechat_webhook_on_agent_message': instance.$('#sendOnAgentMessage').get(0).checked }; Meteor.call('livechat:saveIntegration', settings, (err) => { if (err) { diff --git a/packages/rocketchat-livechat/client/views/app/livechatIntegrations.html b/packages/rocketchat-livechat/client/views/app/livechatIntegrations.html index 61670e4cad50..3fdc69550387 100644 --- a/packages/rocketchat-livechat/client/views/app/livechatIntegrations.html +++ b/packages/rocketchat-livechat/client/views/app/livechatIntegrations.html @@ -32,6 +32,18 @@

{{_ "Webhooks"}}

{{_ "Send_request_on_offline_messages"}}
+
+ +
+
+ +
diff --git a/packages/rocketchat-livechat/config.js b/packages/rocketchat-livechat/config.js index 3a7815a4d6e8..c69df7adcea9 100644 --- a/packages/rocketchat-livechat/config.js +++ b/packages/rocketchat-livechat/config.js @@ -131,6 +131,20 @@ Meteor.startup(function() { i18nLabel: 'Send_request_on_offline_messages' }); + RocketChat.settings.add('Livechat_webhook_on_visitor_message', false, { + type: 'boolean', + group: 'Livechat', + section: 'CRM_Integration', + i18nLabel: 'Send_request_on_visitor_message' + }); + + RocketChat.settings.add('Livechat_webhook_on_agent_message', false, { + type: 'boolean', + group: 'Livechat', + section: 'CRM_Integration', + i18nLabel: 'Send_request_on_agent_message' + }); + RocketChat.settings.add('Livechat_webhook_on_capture', false, { type: 'boolean', group: 'Livechat', diff --git a/packages/rocketchat-livechat/server/hooks/sendToCRM.js b/packages/rocketchat-livechat/server/hooks/sendToCRM.js index 8ae0073ff27b..46e48dcc70fe 100644 --- a/packages/rocketchat-livechat/server/hooks/sendToCRM.js +++ b/packages/rocketchat-livechat/server/hooks/sendToCRM.js @@ -5,15 +5,24 @@ function sendToCRM(type, room, includeMessages = true) { postData.messages = []; - if (includeMessages) { - RocketChat.models.Messages.findVisibleByRoomId(room._id, { sort: { ts: 1 } }).forEach((message) => { + let messages; + if (typeof includeMessages === 'boolean' && includeMessages) { + messages = RocketChat.models.Messages.findVisibleByRoomId(room._id, { sort: { ts: 1 } }); + } else if (includeMessages instanceof Array) { + messages = includeMessages; + } + + if (messages) { + messages.forEach((message) => { if (message.t) { return; } const msg = { + _id: message._id, username: message.u.username, msg: message.msg, - ts: message.ts + ts: message.ts, + editedAt: message.editedAt }; if (message.u.username !== postData.visitor.username) { @@ -49,6 +58,31 @@ RocketChat.callbacks.add('livechat.saveInfo', (room) => { return sendToCRM('LivechatEdit', room); }, RocketChat.callbacks.priority.MEDIUM, 'livechat-send-crm-save-info'); +RocketChat.callbacks.add('afterSaveMessage', function(message, room) { + // only call webhook if it is a livechat room + if (room.t !== 'l' || room.v == null || room.v.token == null) { + return message; + } + + // if the message has a token, it was sent from the visitor + // if not, it was sent from the agent + if (message.token) { + if (!RocketChat.settings.get('Livechat_webhook_on_visitor_message')) { + return message; + } + } else if (!RocketChat.settings.get('Livechat_webhook_on_agent_message')) { + return message; + } + + // if the message has a type means it is a special message (like the closing comment), so skips + if (message.t) { + return message; + } + + sendToCRM('Message', room, [message]); + return message; +}, RocketChat.callbacks.priority.MEDIUM, 'livechat-send-crm-message'); + RocketChat.callbacks.add('livechat.leadCapture', (room) => { if (!RocketChat.settings.get('Livechat_webhook_on_capture')) { return room; diff --git a/packages/rocketchat-livechat/server/lib/Livechat.js b/packages/rocketchat-livechat/server/lib/Livechat.js index 99079cb7f6b9..b614a7096bba 100644 --- a/packages/rocketchat-livechat/server/lib/Livechat.js +++ b/packages/rocketchat-livechat/server/lib/Livechat.js @@ -415,6 +415,7 @@ RocketChat.Livechat = { customFields: room.livechatData, visitor: { _id: visitor._id, + token: visitor.token, name: visitor.name, username: visitor.username, email: null, diff --git a/packages/rocketchat-livechat/server/methods/saveIntegration.js b/packages/rocketchat-livechat/server/methods/saveIntegration.js index f839f8adbe65..023d4ce95c1e 100644 --- a/packages/rocketchat-livechat/server/methods/saveIntegration.js +++ b/packages/rocketchat-livechat/server/methods/saveIntegration.js @@ -22,6 +22,14 @@ Meteor.methods({ RocketChat.settings.updateById('Livechat_webhook_on_offline_msg', !!values['Livechat_webhook_on_offline_msg']); } + if (typeof values['Livechat_webhook_on_visitor_message'] !== 'undefined') { + RocketChat.settings.updateById('Livechat_webhook_on_visitor_message', !!values['Livechat_webhook_on_visitor_message']); + } + + if (typeof values['Livechat_webhook_on_agent_message'] !== 'undefined') { + RocketChat.settings.updateById('Livechat_webhook_on_agent_message', !!values['Livechat_webhook_on_agent_message']); + } + return; } }); diff --git a/packages/rocketchat-livechat/server/publications/livechatIntegration.js b/packages/rocketchat-livechat/server/publications/livechatIntegration.js index 656ea19712d4..455c5388c715 100644 --- a/packages/rocketchat-livechat/server/publications/livechatIntegration.js +++ b/packages/rocketchat-livechat/server/publications/livechatIntegration.js @@ -9,7 +9,7 @@ Meteor.publish('livechat:integration', function() { const self = this; - const handle = RocketChat.models.Settings.findByIds(['Livechat_webhookUrl', 'Livechat_secret_token', 'Livechat_webhook_on_close', 'Livechat_webhook_on_offline_msg']).observeChanges({ + const handle = RocketChat.models.Settings.findByIds(['Livechat_webhookUrl', 'Livechat_secret_token', 'Livechat_webhook_on_close', 'Livechat_webhook_on_offline_msg', 'Livechat_webhook_on_visitor_message', 'Livechat_webhook_on_agent_message']).observeChanges({ added(id, fields) { self.added('livechatIntegration', id, fields); },