Is it possible to read incoming sms and provide reply to that sms. I am using nodejs
© 1999-2024 RingCentral, Inc. All rights reserved.
Privacy Notice
Is it possible to read incoming sms and provide reply to that sms. I am using nodejs
Best answer by PhongVu
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)
}
}
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.