Skip to main content
The First Step
March 14, 2025
Solved

Is it possible to read incoming sms and provide reply to that sms

Is it possible to read incoming sms and provide reply to that sms. I am using nodejs

PhongVu
PhongVuAnswer
Community Manager
March 14, 2025

You can subscribe for the /instant message event to receive incoming SMS message. Then you can call the /sms API to send a reply message to the ‘from’ number you got from the message event.

Here is the simple code in Node JS.

const RingCentral = require('@ringcentral/sdk').SDK
const Subscriptions = require('@ringcentral/subscriptions').Subscriptions;

RC_APP_CLIENTID = "aaaa";
RC_APP_CLIENTSECRET = "bbbb";
RINGCENTRAL_SERVER = 'https://platform.ringcentral.com'

RC_USER_JWT = "xxxx"

const rcsdk = new RingCentral({
server: RINGCENTRAL_SERVER,
clientId: RC_APP_CLIENTID,
clientSecret: RC_APP_CLIENTSECRET
})

var platform = rcsdk.platform();
platform.login({ jwt : RC_USER_JWT })


platform.on(platform.events.loginSuccess, function(e){
console.log("Login success")
subscribe_for_sms_notification()
});

const subscriptions = new Subscriptions({
sdk: rcsdk
});
var subscription = subscriptions.createSubscription();

async function subscribe_for_sms_notification(){
var eventFilters = [
'/restapi/v1.0/account/~/extension/~/message-store/instant?type=SMS', // Authenticator
]
subscription.setEventFilters(eventFilters)
.register()
.then(function(subscriptionResponse) {
console.log("Ready to receive SMS message via WebSocket.")
})
.catch(function(e) {
console.error(e.message);
})
}

subscription.on(subscription.events.notification, async function(msg) {
console.log(JSON.stringify(msg));
send_reply(msg.body)
});

async function send_reply(body){
var text = 'Hi'
if (body.from.name)
text += ` ${body.from.name}.`

text += `\nThank you for your message! This is an auto reply.`
var params = {
from: {phoneNumber: body.to[0].phoneNumber},
to: [ {phoneNumber: body.from.phoneNumber}],
text: text
}

try{
var resp = await platform.post('/restapi/v1.0/account/~/extension/~/sms', params)
var jsonObj = await resp.json()
console.log("SMS sent. Message status: " + jsonObj.messageStatus)
}catch(e){
console.log(e.message)
}
}

 

2 replies

PhongVu
Community Manager
PhongVuAnswer
Community Manager
March 14, 2025

You can subscribe for the /instant message event to receive incoming SMS message. Then you can call the /sms API to send a reply message to the ‘from’ number you got from the message event.

Here is the simple code in Node JS.

const RingCentral = require('@ringcentral/sdk').SDK
const Subscriptions = require('@ringcentral/subscriptions').Subscriptions;

RC_APP_CLIENTID = "aaaa";
RC_APP_CLIENTSECRET = "bbbb";
RINGCENTRAL_SERVER = 'https://platform.ringcentral.com'

RC_USER_JWT = "xxxx"

const rcsdk = new RingCentral({
server: RINGCENTRAL_SERVER,
clientId: RC_APP_CLIENTID,
clientSecret: RC_APP_CLIENTSECRET
})

var platform = rcsdk.platform();
platform.login({ jwt : RC_USER_JWT })


platform.on(platform.events.loginSuccess, function(e){
console.log("Login success")
subscribe_for_sms_notification()
});

const subscriptions = new Subscriptions({
sdk: rcsdk
});
var subscription = subscriptions.createSubscription();

async function subscribe_for_sms_notification(){
var eventFilters = [
'/restapi/v1.0/account/~/extension/~/message-store/instant?type=SMS', // Authenticator
]
subscription.setEventFilters(eventFilters)
.register()
.then(function(subscriptionResponse) {
console.log("Ready to receive SMS message via WebSocket.")
})
.catch(function(e) {
console.error(e.message);
})
}

subscription.on(subscription.events.notification, async function(msg) {
console.log(JSON.stringify(msg));
send_reply(msg.body)
});

async function send_reply(body){
var text = 'Hi'
if (body.from.name)
text += ` ${body.from.name}.`

text += `\nThank you for your message! This is an auto reply.`
var params = {
from: {phoneNumber: body.to[0].phoneNumber},
to: [ {phoneNumber: body.from.phoneNumber}],
text: text
}

try{
var resp = await platform.post('/restapi/v1.0/account/~/extension/~/sms', params)
var jsonObj = await resp.json()
console.log("SMS sent. Message status: " + jsonObj.messageStatus)
}catch(e){
console.log(e.message)
}
}

 

ajfahimThe First StepAuthor
The First Step
March 16, 2025

Thanks a lot for your reply. it worked well.