forked from RocketChat/Rocket.Chat
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from WideChat/sms-invite
[New] SMS & Email Invite for Contacts
- Loading branch information
Showing
5 changed files
with
127 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
packages/rocketchat-lib/server/methods/sendInvitationSMS.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { Meteor } from 'meteor/meteor'; | ||
import { TAPi18n } from 'meteor/tap:i18n'; | ||
import { check } from 'meteor/check'; | ||
import _ from 'underscore'; | ||
|
||
Meteor.methods({ | ||
sendInvitationSMS(phones) { | ||
const twilioService = RocketChat.SMS.getService('twilio'); | ||
if (!RocketChat.SMS.enabled || !twilioService) { | ||
throw new Meteor.Error('error-twilio-not-active', 'Twilio service not active', { | ||
method: 'sendInvitationSMS', | ||
}); | ||
} | ||
|
||
const messageFrom = RocketChat.settings.get('Invitation_SMS_Twilio_From'); | ||
if (!twilioService.accountSid || ! twilioService.authToken || !messageFrom) { | ||
throw new Meteor.Error('error-twilio-not-configured', 'Twilio service not configured', { | ||
method: 'sendInvitationSMS', | ||
}); | ||
} | ||
|
||
check(phones, [String]); | ||
if (!Meteor.userId()) { | ||
throw new Meteor.Error('error-invalid-user', 'Invalid user', { | ||
method: 'sendInvitationSMS', | ||
}); | ||
} | ||
|
||
// to be replaced by a seperate permission specific to SMS later | ||
if (!RocketChat.authz.hasPermission(Meteor.userId(), 'bulk-register-user')) { | ||
throw new Meteor.Error('error-not-allowed', 'Not allowed', { | ||
method: 'sendInvitationSMS', | ||
}); | ||
} | ||
const phonePattern = /^\+?[1-9]\d{1,14}$/; | ||
const validPhones = _.compact(_.map(phones, function(phone) { | ||
if (phonePattern.test(phone)) { | ||
return phone; | ||
} | ||
})); | ||
const user = Meteor.user(); | ||
let body; | ||
if (RocketChat.settings.get('Invitation_SMS_Customized')) { | ||
body = RocketChat.settings.get('Invitation_SMS_Customized_Body'); | ||
} else { | ||
const lng = user.language || RocketChat.settings.get('language') || 'en'; | ||
body = TAPi18n.__('Invitation_SMS_Default_Body', { | ||
lng, | ||
}); | ||
} | ||
body = RocketChat.placeholders.replace(body); | ||
validPhones.forEach((phone) => { | ||
try { | ||
twilioService.send(messageFrom, phone, body); | ||
} catch ({ message }) { | ||
throw new Meteor.Error('error-sms-send-failed', `Error trying to send SMS: ${ message }`, { | ||
method: 'sendInvitationSMS', | ||
message, | ||
}); | ||
} | ||
}); | ||
return validPhones; | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters