Setting SMS notifications with MessageBird

⏱ 15 min build time || Download the Code

Why build SMS order notifications?

In this MessageBird Developer Tutorial we'll show you how to keep your customers up to date with this easy-to-build runnable order notification application powered by the MessageBird SMS Messaging API. Have you ever ordered home delivery to find yourself wondering whether your order was received correctly and how long it will take to arrive? Some experiences are seamless and others... not so much.

For on-demand industries such as food delivery, ride-sharing and logistics, excellent customer service during the ordering process is essential. One easy way to stand out from the crowd is providing proactive communication to keep your customers in the loop about the status of their orders. Regardless of whether your customer is waiting for a package delivery or growing hangry (a combination of hungry and angry) awaiting their food delivery, sending timely SMS order notifications is a great strategy to create a seamless user experience.

The MessageBird SMS Messaging API provides an easy way to fully automate and integrate a notifications application into your order handling software. Busy employees can trigger the notifications application with the push of a single button. No more confused hangry customers and great user experience, just like that!

Getting started

The application is a prototype order management system build in Node.js for our fictitious food delivery company, Birdie NomNom Foods.

Birdie NomNom Foods have set up the following workflow:

  • New incoming orders are in a pending state.
  • Once the kitchen starts preparing an order, it moves to the confirmed state. A message is sent to the customer to inform them about this.
  • When the food is made and handed over to the delivery driver, staff marks the order as delivered. A message is sent to the customer to let them know it will arrive briefly.
  • If preparation takes longer than expected, it can be moved to a delayed_state. A message is sent to the customer asking them to hang on just a little while longer.

Thanks to this workflow, Birdie NomNom Foods saves time, money, and energy that would otherwise be spent answering "Where's my order?" calls. To run the application, you’ll need Node and npm, you can easily install them for free.

Pro-tip: Follow this tutorial to build the whole application from scratch or, if you want to see it in action right away, you can download, clone or fork the sample application from the MessageBird Developer Tutorials GitHub repository.

Let's open a console pointed to the directory into which you've placed the sample application and run the following command to install it:

npm install

This command will install the MessageBird SDK for Node.js and other dependencies from npm as defined in package.json.

Configuring the MessageBird SDK

Now, let's open index.js, the main file of the sample application. You'll find the following lines:

// Load and initialize MessageBird SDK
var messagebird = require('messagebird')('YOUR_API_KEY');

Next, we'll replace the string YOUR_API_KEY with a live access key. You can create or retrieve a key from the API access (REST) tab in the Developers section in the MessageBird Dashboard. It's also possible to use a test key to test the application. In this case, you can see the API output on the console, but no live SMS messages will be sent.

Pro-tip: Hardcoding your credentials in the code is a risky practice that should never be used in production applications. A better method, also recommended by the Twelve-Factor App Definition, is to use environment variables.

We've added dotenv to the sample application, so that you can supply your API key in a file named .env too:

MESSAGEBIRD_API_KEY=YOUR-API-KEY

Notifying customers by triggering an SMS:

The sample application triggers SMS delivery in the /updateOrder route together with updating the stored data.

Sending a message with the MessageBird SDK is straightforward—we simply call the messagebird.messages.create() method with a parameters object.

// Send the message through MessageBird API
messagebird.messages.create({
originator : 'Birdy NomNom',
recipients : [ order.phone ],
body : body
}, function (err, response) {
if (err) {
// Request has failed
console.log(err);
res.send("Error occured while sending message!");
} else {
// Request was successful
console.log(response);
res.redirect('/');
}
});

You should set at least the following attributes in this object:

  • originator: A sender ID for the SMS, either a telephone number including country code, or an alphanumeric string with at most 11 characters. Keep in mind that alphanumeric senders are not supported in every country including the United States, so it’s important to check the country restrictions. If you can't use alphanumeric IDs, use a real phone number instead. You can check our originator article in Help Center to learn more about this topic.
  • recipients: An array of one or more phone numbers to send the message to.
  • body: The content of the message.

Make sure to replace the values in the sample code with adequate data for testing. There are additional optional attributes as well; you can find them in the SMS Messaging API documentation.

The MessageBird API call is asynchronous and executes a callback function once finished. This callback function takes two parameters, err and response. If err is defined, it indicates that something went wrong with the request. If everything went well, response contains… you guessed it, the response from the API.

Testing

You’re done! The sample application works on a set of test data defined in a variable called OrderDatabase in index.js. To test the full flow, replace one of the phone numbers with your own to receive the message on your phone:

// Set up Order "Database"
var OrderDatabase = [
{
name : 'Hannah Hungry',
phone : '+319876543210', // <- put your number here for testing
items : '1 x Hipster Burger + Fries',
status : 'pending'
},
{
name : 'Mike Madeater',
phone : '+319876543211', // <- put your number here for testing
items : '1 x Chef Special Mozzarella Pizza',
status : 'pending'
}
];

It's time to run the following command from your console:

node index.js

Open http://localhost:8080/ in your browser to see the list of orders.

Click on one of the buttons in the Action column to trigger a status change and, at the same time, automatically send a message.

Awesome! You can use the flow, code snippets, and UI examples from this tutorial as an inspiration to build your own SMS Notifications system. Don't forget to download the code from the MessageBird Developer Tutorial GitHub repository.

Nice work! 🎉

You now have a running SMS Notifications application with Node.js!

Start building!

Want to build something similar but not quite sure how to get started? Feel free to let us know at support@messagebird.com; we'd love to help!

Questions?

We’re always happy to help with code or other doubts you might have! Check out our Quickstarts, API Reference, Tutorials, SDKs, or contact our Support team.

Cookie Settings