A few days ago I blogged about using RingCentral's APIs to send a SMS message when your Netlify-based site builds ("Adding a SMS Alert for Netlify Builds with RingCentral"). I thought I'd follow it up with a related example - sending a SMS with form data. To be clear, this post isn't too much different from the previous one, but I thought it was an interesting enough example to share.

Last year I blogged about using Netlify serverless functions for form submissions. In that post I detail the data sent to the serverless payload (since, ahem, Netlify still doesn't document this). Based on that, here's the code I used to take the form submission and send it as a SMS:

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

// used for sms
RECIPIENT = process.env.SMS_RECIP;
RINGCENTRAL_CLIENTID = process.env.RINGCENTRAL_CLIENTID;
RINGCENTRAL_CLIENTSECRET = process.env.RINGCENTRAL_CLIENTSECRET;
RINGCENTRAL_SERVER = process.env.RINGCENTRAL_SERVER;
RINGCENTRAL_USERNAME = process.env.RINGCENTRAL_USERNAME;
RINGCENTRAL_PASSWORD = process.env.RINGCENTRAL_PASSWORD;
RINGCENTRAL_EXTENSION = process.env.RINGCENTRAL_EXTENSION;

var rcsdk = new SDK({
  server: RINGCENTRAL_SERVER,
  clientId: RINGCENTRAL_CLIENTID,
  clientSecret: RINGCENTRAL_CLIENTSECRET
});
var platform = rcsdk.platform();

exports.handler = async (event, context) => {

  let payload = JSON.parse(event.body).payload;
  let name = payload.data.name;
  let email = payload.data.email;
  let comments = payload.data.comments;

  console.log(`name, ${name}, email, ${email}, comments, ${comments}`);

  const text = `
A form was sent by ${name} (email address of ${email}), with these comments: 
${comments}`;
  await sendSMS(text);

}

async function sendSMS(text) {

  await platform.login({
    username: RINGCENTRAL_USERNAME,
    password: RINGCENTRAL_PASSWORD,
    extension: RINGCENTRAL_EXTENSION
  });

  let resp = await platform.post('/restapi/v1.0/account/~/extension/~/sms', {
    from: {'phoneNumber': RINGCENTRAL_USERNAME},
    to: [{'phoneNumber': RECIPIENT}],
    text: text
  });
	
  let data = await resp.json();
  return data;
}

Basically - I create a formatted string and pass it to a function to handle sending the SMS. The result is much like my previous example:

Screen shot of text sent by RingCentral

As a reminder, that text watermark in front would not be there in a production-released RingCentral application.