[NEW] Allow file sharing through Twilio (WhatsApp) integration (#15415)

pull/15376/head^2
Renato Becker 6 years ago committed by Diego Sampaio
parent a9b8fba197
commit 8bfd1db622
  1. 6
      app/livechat/server/sendMessageBySMS.js
  2. 46
      app/sms/server/services/twilio.js
  3. 19
      app/sms/server/settings.js
  4. 2
      package.json

@ -29,9 +29,11 @@ callbacks.add('afterSaveMessage', function(message, room) {
return message;
}
let extraData;
if (message.file) {
message = normalizeMessageFileUpload(message);
const { fileUpload, rid, u: { _id: userId } = {} } = message;
extraData = Object.assign({}, { rid, userId, fileUpload });
}
const SMSService = SMS.getService(settings.get('SMS_Service'));
@ -46,7 +48,7 @@ callbacks.add('afterSaveMessage', function(message, room) {
return message;
}
SMSService.send(room.sms.from, visitor.phone[0].phoneNumber, message.msg);
SMSService.send(room.sms.from, visitor.phone[0].phoneNumber, message.msg, extraData);
return message;
}, callbacks.priority.LOW, 'sendMessageBySms');

@ -1,12 +1,29 @@
import { Meteor } from 'meteor/meteor';
import twilio from 'twilio';
import { Random } from 'meteor/random';
import { TAPi18n } from 'meteor/rocketchat:tap-i18n';
import filesize from 'filesize';
import { settings } from '../../../settings';
import { SMS } from '../SMS';
import { Notifications } from '../../../notifications';
import { fileUploadIsValidContentType } from '../../../utils/lib/fileUploadRestrictions';
const MAX_FILE_SIZE = 5242880;
const notifyAgent = (userId, rid, msg) => Notifications.notifyUser(userId, 'message', {
_id: Random.id(),
rid,
ts: new Date(),
msg,
});
class Twilio {
constructor() {
this.accountSid = settings.get('SMS_Twilio_Account_SID');
this.authToken = settings.get('SMS_Twilio_authToken');
this.fileUploadEnabled = settings.get('SMS_Twilio_FileUpload_Enabled');
this.mediaTypeWhiteList = settings.get('SMS_Twilio_FileUpload_MediaTypeWhiteList');
}
parse(data) {
@ -58,13 +75,40 @@ class Twilio {
return returnData;
}
send(fromNumber, toNumber, message) {
send(fromNumber, toNumber, message, extraData) {
const client = twilio(this.accountSid, this.authToken);
let mediaUrl;
if (extraData && extraData.fileUpload) {
const { rid, userId, fileUpload: { size, type, publicFilePath } } = extraData;
const user = userId ? Meteor.users.findOne(userId) : null;
const lng = (user && user.language) || settings.get('Language') || 'en';
let reason;
if (!this.fileUploadEnabled) {
reason = TAPi18n.__('FileUpload_Disabled', { lng });
} else if (size > MAX_FILE_SIZE) {
reason = TAPi18n.__('File_exceeds_allowed_size_of_bytes', {
size: filesize(MAX_FILE_SIZE),
lng,
});
} else if (!fileUploadIsValidContentType(type, this.fileUploadMediaTypeWhiteList)) {
reason = TAPi18n.__('File_type_is_not_accepted', { lng });
}
if (reason) {
rid && userId && notifyAgent(userId, rid, reason);
return console.error(`(Twilio) -> ${ reason }`);
}
mediaUrl = [publicFilePath];
}
// return
client.messages.create({
to: toNumber,
from: fromNumber,
body: message,
...mediaUrl && { mediaUrl },
});
}

@ -47,6 +47,25 @@ Meteor.startup(function() {
i18nLabel: 'Auth_Token',
secret: true,
});
this.add('SMS_Twilio_FileUpload_Enabled', true, {
type: 'boolean',
enableQuery: {
_id: 'SMS_Service',
value: 'twilio',
},
i18nLabel: 'FileUpload_Enabled',
secret: true,
});
this.add('SMS_Twilio_FileUpload_MediaTypeWhiteList', 'image/*,audio/*,video/*,text/*,application/pdf', {
type: 'string',
enableQuery: {
_id: 'SMS_Service',
value: 'twilio',
},
i18nLabel: 'FileUpload_MediaTypeWhiteList',
i18nDescription: 'FileUpload_MediaTypeWhiteListDescription',
secret: true,
});
});
this.section('Voxtelesys', function() {

@ -220,7 +220,7 @@
"tar-stream": "^1.6.2",
"toastr": "^2.1.4",
"turndown": "^5.0.1",
"twilio": "^3.25.0",
"twilio": "^3.35.0",
"twit": "^2.2.11",
"ua-parser-js": "^0.7.19",
"underscore": "^1.9.1",

Loading…
Cancel
Save