question

Barak Poker avatar image
Barak Poker asked Gurpreet Singh commented

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

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?

sms and text messaging
1 comment
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Suyash Joshi avatar image Suyash Joshi ♦ commented ·

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.

0 Likes 0 ·
Yatin Gera avatar image
Yatin Gera Deactivated answered Barak Poker published

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,
});
3 comments
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Barak Poker avatar image Barak Poker commented ·

@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.

0 Likes 0 ·
Yatin Gera avatar image Yatin Gera ♦♦ Barak Poker commented ·

@Barak Poker
You do not need the user's credentials stored in your DB.
When you create a subscription, there is an option to set the expiry of the subscription and you can put that value to 10 years (for example)
That means that once the user has created the subscription, they do not need to be logged in, and even when logged in, the notification will be sent to the webhook whenever a new SMS is received. You can add custom logic to check the timestamp of the SMS and decide to send a notification to the user depending on that.

As I said, the only caveat is that the user will have to set up the subscription one time by login in to the app using their credentials.


I hope this is helpful.
Feel free to reach out in case you have any other queries regarding this.

0 Likes 0 ·
Barak Poker avatar image Barak Poker Yatin Gera ♦♦ commented ·

@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?

0 Likes 0 ·
Barak Poker avatar image
Barak Poker answered Gurpreet Singh commented

@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?

3 comments
1 |3000

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Yatin Gera avatar image Yatin Gera ♦♦ commented ·

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

0 Likes 0 ·
Yatin Gera avatar image Yatin Gera ♦♦ Yatin Gera ♦♦ commented ·

UPDATE
Sending SMS on another user's behalf is not allowed
I don't see any other way to send SMS on another user's behalf other than saving their credentials (which is not recommended unless you are following proper security guidelines).

You could look into JWT and check if you can create a JWT for every user and give it access to only that app so that you end up not saving users' plain passwords in your database

https://developers.ringcentral.com/guide/authentication/jwt/create-jwt

0 Likes 0 ·
Gurpreet Singh avatar image Gurpreet Singh Yatin Gera ♦♦ commented ·

Yes, JWT is another way lot of our customers are using to bring this use case to light. Only caveat is that you have to create JWT for every extension/user before hand and map it to the from_number, so that we can use that in your code before sending SMS for that number.

Also JWT doesn't change if those users change their credentials, which is a plus.


2 Likes 2 ·

Developer sandbox tools

Using the RingCentral Phone for Desktop, you can dial or receive test calls, send and receive test SMS or Fax messages in your sandbox environment.

Download RingCentral Phone for Desktop:

Tip: switch to the "sandbox mode" before logging in the app:

  • On MacOS: press "fn + command + f2" keys
  • On Windows: press "Ctrl + F2" keys