The Firebase platform allows developers to build an application backend rapidly. It's also enjoyable to use as well. For this tutorial, I wanted to start using it for some SMS messaging with Nexmo. After this walk-through, you will be able to create an SMS message log, and a response to the sender using Firebase Functions and Firestore alongside the Nexmo SMS API.
Before You Get Started
You will need a few items to get going - so take a moment and make sure you have both of these ready to go.
Setting up Firebase
The first step is to set up a Firebase project. The following will walk you through setting up a new project using the Firebase console.
Create the Firebase Project
- Go to Firebase console
- Click add project
- Add a name and click continue
- Leave Google Analytics on and click continue (not required)
- Select a Google Analytics account and then click Create Project (if added)
- Wait a bit for the project to be created - takes less than a minute
- Set the Billing type under ⚙️ -> Usage and Billing -> Details & Settings to Blaze. The Pay-as-you-go plan is required to use a third-party API. For more details regarding billing with Google, go here.
- Set the
Google Cloud Platform (GCP) resource location
in⚙️ -> Project Settings
Install Firebase Tools
Most everything you will need to do with Firebase can be done directly from the command line with the toolset they provide.
- Install the Firebase tools with npm
npm install -g firebase-tools
- Log in to Firebase using
firebase login
. The login process will open your browser for authentication.
Setup Local Environment
Writing Firebase functions requires some initialization work to get started, but it's mostly done for you using Firebase Tools commands.
Create a project folder
mkdir nexmo-project && cd nexmo-project
Initialize Firebase Functions
firebase init functions
######## #### ######## ######## ######## ### ###### ########
## ## ## ## ## ## ## ## ## ## ##
###### ## ######## ###### ######## ######### ###### ######
## ## ## ## ## ## ## ## ## ## ##
## #### ## ## ######## ######## ## ## ###### ########
You're about to initialize a Firebase project in this directory:
/your_folders/your-project-name
=== Project Setup
First, let's associate this project directory with a Firebase project.
You can create multiple project aliases by running firebase use --add,
but for now, we'll just set up a default project.
? Please select an option: (Use arrow keys)
❯ Use an existing project
Create a new project
Add Firebase to an existing Google Cloud Platform project
Don't set up a default project
Since you already created a project in the dashboard, you can select Use an existing project
which will prompt you to choose the desired project. If you haven't done this, use Create a new project
and give it a unique name to create one. You would still need to go to the console to update the location and billing, but it is another option to create Firebase projects.
- Select the project name you created
- Select JavaScript
- Choose Y for ESLint if you desire (I recommend it)
- Install all dependencies now
These steps will create the folders and files required to build Firebase Functions and installs all dependencies. Once NPM completes, switch to the functions
directory and open index.js
in your favorite editor to start adding code.
Create Your First Function
The first function you create will act as a webhook to capture and log incoming SMS messages from Nexmo.
The index.js
file has some example code provided you won't need. Delete everything and start at the top to add the following code.
const functions = require('firebase-functions');
const admin = require('firebase-admin');
// Initialize Firebase app for database access
admin.initializeApp();
Calling admin.initializeApp();
allows the functions to read and write to the Firebase Real-Time database. Next, use the following method to create your function.
// This function will serve as the webhook for incoming SMS messages,
// and will log the message into the Firebase Realtime Database
exports.inboundSMS = functions.https.onRequest(async (req, res) => {
await admin.database().ref('/msgq').push(req.body);
res.send(200);
});
The inboundSMS
method listens for HTTPS requests - which is precisely what Nexmo webhook needs. The Firebase Function will capture the req.body
and send it to the /msgq
object in the Real-Time Database as a log.
Since we are using req.body
, the webhook will need to be a POST Method
. If you'd prefer to (or you have to) use the GET
method for Nexmo webhooks, just replace it with req.query
, and the GET method will work the same way.
Now that you have some code written be sure to save your file an deploy the function to Firebase:
firebase deploy --only functions
=== Deploying to 'nexmo-project'...
i deploying functions
Running command: npm --prefix "$RESOURCE_DIR" run lint
> functions@ lint /Users/kellyjandrews/Google Drive/Apps/nexmo-project/functions
> eslint .
✔ functions: Finished running predeploy script.
i functions: ensuring necessary APIs are enabled...
✔ functions: all necessary APIs are enabled
i functions: preparing functions directory for uploading...
i functions: packaged functions (38.78 KB) for uploading
✔ functions: functions folder uploaded successfully
i functions: creating Node.js 8 function inboundSMS(us-central1)...
✔ functions[inboundSMS(us-central1)]: Successful create operation.
Function URL (inboundSMS): https://us-central1-nexmo-project.cloudfunctions.net/inboundSMS
✔ Deploy complete!
Project Console: https://console.firebase.google.com/project/nexmo-project/overview
The vital piece from the output is Function URL (inboundSMS)
. This URL is required to set up the webhook in Nexmo, which you will do next.
Setting up Nexmo
There are a few quick steps to setting up Nexmo - all done from the command line.
- Install the CLI
npm install -g nexmo-cli
- Setup the CLI with your API key and secret from the dashboard https://dashboard.nexmo.com/getting-started-guide
- Purchase a new phone number
nexmo number:buy --country_code US
- This command retrieves the first available phone number in the US. For more info on Nexmo numbers, you can view the [numbers documentation](https://developer.nexmo.com/numbers/overview
- Type confirm, then copy the number
- Link the phone number to the webhook using this command
nexmo link:sms YOUR_NUMBER YOUR_FUNCTION_URL
Give the process a few seconds to completely provision, then test out the new function to make sure the message is getting logged.
Grab your phone and send a message to the phone number. Open up the Firebase console and navigate to database
page, and you should see something like this:
Now that there is a way to log incoming messages, you can write a function to do something with the incoming message.
Create the Send Function
So far, you have created a Firebase Function linked to a Nexmo phone number for capturing inbound SMS messages. Firebase Functions can also react to database updates. Upon a new entry, the code sends an echo of the original text.
Start by adding Nexmo to the dependency list - make sure you do this in the functions
directory:
npm i nexmo --save
Add the following environment variables to the Firebase config
firebase functions:config:set nexmo.api_key="YOUR_KEY" nexmo.api_secret="YOUR_SECRET"
Next, open index.js
add nexmo
to the requirements at the top, and import the environment variables to initialize Nexmo:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const Nexmo = require('nexmo');
// Initialize Firebase app for database access
admin.initializeApp();
// get Firebase environment variables for Nexmo
const {
api_key,
api_secret
} = functions.config().nexmo;
// Initialize Nexmo with application credentials
const nexmo = new Nexmo({
apiKey: api_key,
apiSecret: api_secret
});
Now you can create the new function for Firebase to send the response:
// This function listens for updates to the Firebase Realtime Database
// and sends a message back to the original sender
exports.sendSMS = functions.database.ref('/msgq/{pushId}')
.onCreate((message) => {
const { msisdn, text, to } = message.val();
// the incoming object - 'msisdn' is the your phone number, and 'to' is the Nexmo number
// nexmo.message.sendSms(to, msisdn, text);
return nexmo.message.sendSms(to, msisdn, `You sent the following text: ${text}`, (err, res) => {
if (err) {
console.log(err);
} else {
if (res.messages[0]['status'] === "0") {
console.log("Message sent successfully.");
} else {
console.log(`Message failed with error: ${res.messages[0]['error-text']}`);
}
}
})
});
The new function will watch for new messages added to the /msgq
database object. When triggered, the full Nexmo object gets passed as message
. This object includes msisdn
, which is the originating phone number - yours in this case, and the to
number, which is the Nexmo virtual number you purchased.
With the phone numbers in hand, as well as the text message, you can now do any number of things. You can create a lookup table to respond with specific data based on the keyword, forward to another system, or in our case, send the original message.
Deploy the Firebase Functions again from the command line:
firebase deploy --only functions
Grab your phone, send another message, and then you should get a response back that looks something like You sent the following text: Test message
.
Wrap Up
You have now completed all the steps for this tutorial. You can see the full code on Github.
Now that the initial steps to send and receive messages are complete, my next few posts will take this concept and expand it into controlling some of my home automation via text messages. I would love to hear what you plan to do as well so send me a message on Twitter and let me know.
Further Reading
- Check out the Developer Documentation at https://developer.nexmo.com
- Details about Nexmo SMS Functionality https://developer.nexmo.com/messaging/sms/overview
- Getting started with Firebase Functions https://firebase.google.com/docs/functions/get-started
The post Send and Receive SMS Messages with Firebase Functions appeared first on Nexmo Developer Blog.
Top comments (0)