DEV Community

Cover image for Sending SMS Messages for Form Data with RingCentral and Netlify
Raymond Camden
Raymond Camden

Posted on • Originally published at raymondcamden.com on

2 1

Sending SMS Messages for Form Data with RingCentral and Netlify

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.

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs