Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 7 commits into from
Sep 25, 2019
6 changes: 4 additions & 2 deletions app/livechat/server/sendMessageBySMS.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand All @@ -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');
46 changes: 45 additions & 1 deletion app/sms/server/services/twilio.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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 },
});
}

Expand Down
19 changes: 19 additions & 0 deletions app/sms/server/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down