Skip to content

Commit

Permalink
[NEW] Twilio MMS support for LiveChat integration (#7964)
Browse files Browse the repository at this point in the history
* Added ability for MMS messaging to push through attachments

* Fixed issues for codacy https://www.codacy.com/app/RocketChat/Rocket-Chat/pullRequest?prid=875706

* Fixed more codacy issues

* Changes concerning feedback from dev

* Switch to camelcase
* Changed hasMedia (removed)
* use map instead of for loop for populating attachments

* Save map return on message attachments
  • Loading branch information
t3hchipmunk authored and rodrigok committed Apr 17, 2018
1 parent 3115ec6 commit 2c856c0
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
21 changes: 21 additions & 0 deletions packages/rocketchat-livechat/imports/server/rest/sms.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,27 @@ RocketChat.API.v1.addRoute('livechat/sms-incoming/:service', {
sendMessage.message.msg = sms.body;
sendMessage.guest = visitor;

sendMessage.message.attachments = sms.media.map(curr => {
const attachment = {
message_link: curr.url
};

const contentType = curr.contentType;
switch (contentType.substr(0, contentType.indexOf('/'))) {
case 'image':
attachment.image_url = curr.url;
break;
case 'video':
attachment.video_url = curr.url;
break;
case 'audio':
attachment.audio_url = curr.url;
break;
}

return attachment;
});

try {
const message = SMSService.response.call(this, RocketChat.Livechat.sendMessage(sendMessage));

Expand Down
32 changes: 31 additions & 1 deletion packages/rocketchat-sms/services/twilio.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ class Twilio {
this.authToken = RocketChat.settings.get('SMS_Twilio_authToken');
}
parse(data) {
return {
let numMedia = 0;

const returnData = {
from: data.From,
to: data.To,
body: data.Body,
Expand All @@ -23,6 +25,34 @@ class Twilio {
fromZip: data.FromZip
}
};

if (data.NumMedia) {
numMedia = parseInt(data.NumMedia, 10);
}

if (isNaN(numMedia)) {
console.error(`Error parsing NumMedia ${ data.NumMedia }`);
return returnData;
}

returnData.media = [];

for (let mediaIndex = 0; mediaIndex < numMedia; mediaIndex++) {
const media = {
'url': '',
'contentType': ''
};

const mediaUrl = data[`MediaUrl${ mediaIndex }`];
const contentType = data[`MediaContentType${ mediaIndex }`];

media.url = mediaUrl;
media.contentType = contentType;

returnData.media.push(media);
}

return returnData;
}
send(fromNumber, toNumber, message) {
const client = twilio(this.accountSid, this.authToken);
Expand Down

0 comments on commit 2c856c0

Please sign in to comment.