From ec99c0757cad73fcd79295c4c41c0bdd205d3431 Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Wed, 20 Sep 2017 14:55:55 -0300 Subject: [PATCH] Add RDStation integration to livechat --- packages/rocketchat-i18n/i18n/en.i18n.json | 1 + packages/rocketchat-livechat/config.js | 8 +++ packages/rocketchat-livechat/package.js | 1 + .../server/hooks/RDStation.js | 53 +++++++++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 packages/rocketchat-livechat/server/hooks/RDStation.js diff --git a/packages/rocketchat-i18n/i18n/en.i18n.json b/packages/rocketchat-i18n/i18n/en.i18n.json index 2dff5bac7fb2..6fde30228d2c 100644 --- a/packages/rocketchat-i18n/i18n/en.i18n.json +++ b/packages/rocketchat-i18n/i18n/en.i18n.json @@ -1352,6 +1352,7 @@ "post-readonly_description": "Permission to post a message in a read-only channel", "preview-c-room": "Preview Public Channel", "preview-c-room_description": "Permission to view the contents of a public channel before joining", + "RDStation_Token": "RD Station Token", "remove-user": "Remove User", "remove-user_description": "Permission to remove a user from a room", "run-import": "Run Import", 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');