Add secret token field to livechat webhooks

pull/3441/head
Diego Sampaio 10 years ago
parent b32eb82e0a
commit 2aadb28c1e
  1. 1
      packages/rocketchat-lib/i18n/en.i18n.json
  2. 8
      packages/rocketchat-livechat/client/views/app/livechatIntegrations.html
  3. 12
      packages/rocketchat-livechat/client/views/app/livechatIntegrations.js
  4. 7
      packages/rocketchat-livechat/config.js
  5. 11
      packages/rocketchat-livechat/server/methods/webhookTest.js
  6. 15
      packages/rocketchat-livechat/server/setupWebhook.js

@ -906,6 +906,7 @@
"Search_Messages" : "Search Messages",
"Search_Private_Groups" : "Search Private Groups",
"seconds" : "seconds",
"Secret_token" : "Secret token",
"Select_a_department" : "Select a department",
"Select_an_avatar" : "Select an avatar",
"Select_file" : "Select file",

@ -3,7 +3,7 @@
<h2>{{_ "Webhooks"}}</h2>
<p>
{{_ "You_can_use_webhooks_to_easily_integrate_livechat_with_your_CRM"}}
<a href="https://rocket.chat/docs/">{{_ "Click_here"}}</a> {{_ "to_see_more_details_on_how_to_integrate"}}
<a href="https://rocket.chat/docs/administrator-guides/livechat/#integrations">{{_ "Click_here"}}</a> {{_ "to_see_more_details_on_how_to_integrate"}}
</p>
<form id="integration-form">
@ -13,6 +13,12 @@
<input type="url" name="webhookUrl" id="webhookUrl" value="{{webhookUrl}}" placeholder="https://yourdomain.com/webhook/entrypoint">
</div>
</div>
<div class="input-line">
<label for="secretToken">{{_ "Secret_token"}}</label>
<div>
<input type="text" name="secretToken" id="secretToken" value="{{secretToken}}">
</div>
</div>
<div class="input-line">
<label for="sendOnClose">
<input type="checkbox" name="sendOnClose" id="sendOnClose" value="1" checked="{{sendOnCloseChecked}}">

@ -2,6 +2,9 @@ Template.livechatIntegrations.helpers({
webhookUrl() {
return Template.instance().settingValue.get();
},
secretToken() {
return Template.instance().secretToken.get();
},
disableTest() {
return Template.instance().disableTest.get();
},
@ -16,6 +19,7 @@ Template.livechatIntegrations.helpers({
Template.livechatIntegrations.onCreated(function() {
this.disableTest = new ReactiveVar(true);
this.settingValue = new ReactiveVar();
this.secretToken = new ReactiveVar();
this.sendOnClose = new ReactiveVar();
this.sendOnOffline = new ReactiveVar();
@ -24,6 +28,10 @@ Template.livechatIntegrations.onCreated(function() {
this.settingValue.set(RocketChat.settings.get('Livechat_webhookUrl'));
});
this.autorun(() => {
this.secretToken.set(RocketChat.settings.get('Livechat_secret_token'));
});
this.autorun(() => {
this.sendOnClose.set(RocketChat.settings.get('Livechat_webhook_on_close'));
});
@ -62,6 +70,10 @@ Template.livechatIntegrations.events({
_id: 'Livechat_webhookUrl',
value: s.trim(instance.$('#webhookUrl').val())
},
{
_id: 'Livechat_secret_token',
value: s.trim(instance.$('#secretToken').val())
},
{
_id: 'Livechat_webhook_on_close',
value: instance.$('#sendOnClose').get(0).checked

@ -62,6 +62,13 @@ Meteor.startup(function() {
i18nLabel: 'Webhook_URL'
});
RocketChat.settings.add('Livechat_secret_token', false, {
type: 'string',
group: 'Livechat',
section: 'CRM Integration',
i18nLabel: 'Secret_token'
});
RocketChat.settings.add('Livechat_webhook_on_close', false, {
type: 'boolean',
group: 'Livechat',

@ -13,8 +13,6 @@ Meteor.methods({
'livechat:webhookTest'() {
this.unblock();
const url = RocketChat.settings.get('Livechat_webhookUrl');
const sampleData = {
data: {
type: 'LivechatSession',
@ -65,7 +63,14 @@ Meteor.methods({
}
};
let response = postCatchError(url, { data: sampleData });
let options = {
headers: {
'X-RocketChat-Livechat-Token': RocketChat.settings.get('Livechat_secret_token')
},
data: sampleData
};
let response = postCatchError(RocketChat.settings.get('Livechat_webhookUrl'), options);
console.log('response ->', response);

@ -2,6 +2,7 @@
let sendOnCloseLivechat = false;
let sendOnOfflineMessage = false;
let webhookURL = '';
let secretToken = '';
RocketChat.settings.get('Livechat_webhook_on_close', function(key, value) {
sendOnCloseLivechat = value;
@ -15,14 +16,24 @@ RocketChat.settings.get('Livechat_webhookUrl', function(key, value) {
webhookURL = value;
});
RocketChat.settings.get('Livechat_secret_token', function(key, value) {
secretToken = value;
});
const sendRequest = (postData, trying = 1) => {
try {
HTTP.post(webhookURL, { data: postData });
let options = {
headers: {
'X-RocketChat-Livechat-Token': secretToken
},
data: postData
};
HTTP.post(webhookURL, options);
} catch (e) {
RocketChat.Livechat.logger.webhook.error('Response error on ' + trying + ' try ->', e);
// try 10 times after 10 seconds each
if (trying < 10) {
RocketChat.Livechat.logger.webhook.info('Try again in 10 seconds.');
RocketChat.Livechat.logger.webhook.warn('Will try again in 10 seconds ...');
trying++;
setTimeout(Meteor.bindEnvironment(() => {
sendRequest(postData, trying);

Loading…
Cancel
Save