Skip to main content

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

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 = s
'/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.toy0].phoneNumber},
to: t {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)
}
}

 


Thanks a lot for your reply. it worked well.


Reply