Question

Is there a way to send an SMS message from another user's account with their credentials?

  • 12 June 2022
  • 5 replies
  • 673 views

To add some context. We have multiple users that all have their own phone numbers. We would like to be able to automate some SMS notifications from their specific numbers when certain events occur in our system. Per a discussion with RC "support" they said the only way to do this is by generating a token for the user and then sending the SMS with the token. This can be done in our system by storing RC credentials, however, this is not a scalable solution as we add users and they may change their credentials.


Does anyone have any suggestions on how to approach this?


5 replies

I agree with the RC Support provided answer and can't think of another way as you need to authenticate against RC with that user account credentials. Curious why you say this is not scalable as you can do that programmatically in the backend.

From what I can understand, Subscriptions would best suit your use case.
Now with my un derstanding, I see 2 options available here

Option 1

1. Use the super admin credentials to log in using the API. Get the access token. Any logic mechanism should work here (3 Legged OAuth,JWT, Password)
2. Once you generate the access token, get a list of all the extensions using the below API
https://developers.ringcentral.com/api-reference/Extensions/listExtensions
3. Loop over all the extensions, extract the ids and create an event filter for all these extensions of the below notification type
https://developers.ringcentral.com/api-reference/Instant-Message-Event
4. Create a webhook subscription with the above eventType (one line item in the array for each extension)
5. The webhook will be called every time there is a new SMS for any of the extensions

NOTE: You need super admin credentials to create notifications for other extensions

Option 2

1. Let each user log into the app individually and get the access token for the user
2. Once you generate the access token, get the extensions using the below API
https://developers.ringcentral.com/api-reference/User-Settings/readExtension
3. Extract the extension id from the response and create an event filter for the below notification type
https://developers.ringcentral.com/api-reference/Instant-Message-Event
4. Create a webhook subscription with the above eventType
5. Use the same webhook URLs for all the users and make sure
6. The webhook will be called every time there is a new SMS for any of the extensions

NOTE: This will require an additional step where every user will first have to log in individually so that the subscriptions can be created for each one of them

NOTE

Sharing a sample code for the Option 1

const serverUrl = process.env.RC_SERVER_URL;
const clientId = process.env.RC_CLIENT_ID;
const clientSecret = process.env.RC_CLIENT_SECRET;
const username = process.env.RC_USERNAME;
const password = process.env.RC_PASSWORD;

const sdk = new SDK({ server: serverUrl, clientId, clientSecret });
const platform = sdk.platform();
await platform.login({ username, password });
const extensionListResponse = await platform.get('/restapi/v1.0/account/~/extension');
const extensionListBody = await extensionListResponse.json();
const extensionIds = extensionListBody.records.filter(({ type }) => type === 'User').map(({ id }) => id);
const eventFilters = extensionIds.map((id) => `/restapi/v1.0/account/~/extension/${id}/message-store/instant?type=SMS`);
await platform.post('/restapi/v1.0/subscription', {
    deliveryMode: {
        transportType: 'WebHook',
        address: 'your host nam
    },
    expiresIn: 7776000,
    eventFilters,
});

@Yatin Gera thank you for the response. I think option #2 is close to what I am trying to do, however the problem is that the users may not be logged in at the time. For example, if the "event" that should trigger an automated SMS message occurs during non-business hours the user will not be logged in.


The only solution I can think of storing RC credentials for each user in our DB, but this is not scalable as we add new users with RC accounts or if users change their RC credentials.

@Yatin Gera I understand that I can receive a notification when an SMS is received, but I was to send out an SMS upon an internal event in the system that has nothing to do with SMS exchanges.

For example, if an order is shipped, I want to be able to send an SMS notification from the users direct line notifying them that the order is shipped even if there has never been an SMS communication with the user before. Is this still possible given your proposed solution?

Ah. My bad. I mistook it as wanting to get notified when a user receives an SMS.
Option 2 I provided above will not work when sending SMS on another user's behalf.

I am not sure if there is any other way to send SMS on behalf of another user. Let me check with other folks if there is a way that can be done without having to log in every time on behalf of the user

Reply