diff --git a/packages/rocketchat-i18n/i18n/en.i18n.json b/packages/rocketchat-i18n/i18n/en.i18n.json index 71ffa15e24f9..a420e7eb096f 100644 --- a/packages/rocketchat-i18n/i18n/en.i18n.json +++ b/packages/rocketchat-i18n/i18n/en.i18n.json @@ -1430,6 +1430,7 @@ "quote": "quote", "Quote": "Quote", "Random": "Random", + "RDStation_Token": "RD Station Token", "React_when_read_only": "Allow Reacting", "React_when_read_only_changed_successfully": "Allow reacting when read only changed successfully", "Reacted_with": "Reacted with", diff --git a/packages/rocketchat-livechat/config.js b/packages/rocketchat-livechat/config.js index 014dfef94013..b9ea597aa5ae 100644 --- a/packages/rocketchat-livechat/config.js +++ b/packages/rocketchat-livechat/config.js @@ -235,4 +235,12 @@ Meteor.startup(function() { i18nLabel: 'Livechat_AllowedDomainsList', i18nDescription: 'Domains_allowed_to_embed_the_livechat_widget' }); + + RocketChat.settings.add('Livechat_RDStation_Token', '', { + type: 'string', + group: 'Livechat', + public: false, + section: 'RD Station', + i18nLabel: 'RDStation_Token' + }); }); diff --git a/packages/rocketchat-livechat/package.js b/packages/rocketchat-livechat/package.js index 26451fc650ac..c90187ef6f3c 100644 --- a/packages/rocketchat-livechat/package.js +++ b/packages/rocketchat-livechat/package.js @@ -127,6 +127,7 @@ Package.onUse(function(api) { api.addFiles('server/hooks/externalMessage.js', 'server'); api.addFiles('server/hooks/markRoomResponded.js', 'server'); api.addFiles('server/hooks/offlineMessage.js', 'server'); + api.addFiles('server/hooks/RDStation.js', 'server'); api.addFiles('server/hooks/sendToCRM.js', 'server'); // methods diff --git a/packages/rocketchat-livechat/server/hooks/RDStation.js b/packages/rocketchat-livechat/server/hooks/RDStation.js new file mode 100644 index 000000000000..796a6ade7802 --- /dev/null +++ b/packages/rocketchat-livechat/server/hooks/RDStation.js @@ -0,0 +1,53 @@ +function sendToRDStation(room) { + if (!RocketChat.settings.get('Livechat_RDStation_Token')) { + return room; + } + + const livechatData = RocketChat.Livechat.getLivechatRoomGuestInfo(room); + + if (!livechatData.visitor.email) { + return room; + } + + const options = { + headers: { + 'Content-Type': 'application/json' + }, + data: { + token_rdstation: RocketChat.settings.get('Livechat_RDStation_Token'), + identificador: 'rocketchat-livechat', + client_id: livechatData.visitor._id, + email: livechatData.visitor.email + } + }; + + options.data.nome = livechatData.visitor.name || livechatData.visitor.username; + + if (livechatData.visitor.phone) { + options.data.telefone = livechatData.visitor.phone; + } + + if (livechatData.tags) { + options.data.tags = livechatData.tags; + } + + Object.keys(livechatData.customFields || {}).forEach(field => { + options.data[field] = livechatData.customFields[field]; + }); + + Object.keys(livechatData.visitor.customFields || {}).forEach(field => { + options.data[field] = livechatData.visitor.customFields[field]; + }); + + try { + HTTP.call('POST', 'https://www.rdstation.com.br/api/1.3/conversions', options); + } catch (e) { + console.error('Error sending lead to RD Station ->', e); + } + + return room; +} + +RocketChat.callbacks.add('livechat.closeRoom', sendToRDStation, RocketChat.callbacks.priority.MEDIUM, 'livechat-rd-station-close-room'); + +RocketChat.callbacks.add('livechat.saveInfo', sendToRDStation, RocketChat.callbacks.priority.MEDIUM, 'livechat-rd-station-save-info');