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] Twilio MMS support for LiveChat integration #7964

Merged
merged 8 commits into from
Apr 17, 2018
24 changes: 24 additions & 0 deletions packages/rocketchat-livechat/imports/server/rest/sms.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,30 @@ RocketChat.API.v1.addRoute('livechat/sms-incoming/:service', {
sendMessage.message.msg = sms.body;
sendMessage.guest = visitor;

if (sms.hasMedia) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it will always enter on this if because hasMedia is always true, so it is not really needed.

sendMessage.message.attachments = [];
for (let mediaIndex = 0; mediaIndex < sms.media.length; mediaIndex++) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to use sms.media.map() to populate the attachments field to help code reading. but not really a must.

const attachment = {};
const contenttype = sms.media[mediaIndex].contenttype;

switch (contenttype.substr(0, contenttype.indexOf('/'))) {
case 'image':
attachment.image_url = sms.media[mediaIndex].url;
break;
case 'video':
attachment.video_url = sms.media[mediaIndex].url;
break;
case 'audio':
attachment.audio_url = sms.media[mediaIndex].url;
break;
}

attachment.message_link = sms.media[mediaIndex].url;

sendMessage.message.attachments.push(attachment);
}
}

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

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

const returndata = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use camel case variables, i.e: returnData

from: data.From,
to: data.To,
body: data.Body,
Expand All @@ -21,6 +23,35 @@ 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.hasMedia = true;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are always setting hasMedia as true which is not correct.

returndata.media = [];

for (let mediaIndex = 0; mediaIndex < NumMedia; mediaIndex++) {
const media = {
'url': '',
'contenttype': ''
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use camel case properties, i.e: 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 = Npm.require('twilio')(this.accountSid, this.authToken);
Expand Down