Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Connecting the Dots Between Firebase and Microsoft Bot Framework: Part I

DZone's Guide to

Connecting the Dots Between Firebase and Microsoft Bot Framework: Part I

To use the NodeJS Azure function, you simply need to configure an environment in AWS serverless so that you can use Firebase from a NodeJS Azure function.

· Integration Zone
Free Resource

Learn how API management supports better integration in Achieving Enterprise Agility with Microservices and API Management, brought to you in partnership with 3scale

I've been working in an example that you can check out here. This example uses Firebase, AngularJs and Azure Functions. I used a bunch of cool tools to create rapidly powerful front-end apps. Today I'll add a couple of new tools like Microsoft Bot Framework and Azure Service Bus to enhance our app in a couple of ways.

The idea is to create a bot that receives a place name and response with the current date and time at that location. After the bot parses the input and generates the proper response, it puts into the Azure Service Bus a message that will trigger our NodeJs Azure Function to store the data in Firebase. Pretty cool, isn't it? You can check the working version here.

NodeJS Azure Function

Today, we are going to focus only on our Azure Function. I'll talk about how to create a NodeJS Azure function and how to configure it in order to be able to use Firebase from a NodeJS Azure function.

As of the last article, our Azure function is composed of two files: the function.json (function metadata) and an index.js file to place our code to execute because this one is a NodeJS function. All JavaScript functions must export a single function via module.exports for the runtime to find the function and run it. This function must always include a context object. In the example below, you can see how I configure my function.

module.exports = function(context, mySbMsg) {
   if(!app) {
      context.log('Initializing Firebase App');
      app = firebase.initializeApp(config);
   }
   var defaultAuth = app.auth();
   if(!_user){
      defaultAuth.signInAnonymously()
                 .catch(function(error) {
                    context.log('Anonymous login failed: ' + error.message);
                    context.done();
                 });
   }

   defaultAuth.onAuthStateChanged(function(user) {
   if (user) {
      _user = user;
      context.log('User is signed in. user uid: ' + _user.uid);
      var defaultDatabase = app.database().ref().child('requests').child(_user.uid).child('userRequests');
      mySbMsg['userName'] = 'Anonymous';
      defaultDatabase.push(mySbMsg)
                     .then(function() {
                          context.log('JavaScript ServiceBus queue trigger function processed message', mySbMsg);
                          context.done();
                     })
                     .catch(function(error) {
                          context.log('Synchronization failed');
                          context.done();
                      });
    } else {
       context.log('User is signed out.');
       context.done();
    }});
};

Configuring the Environment

Before going further with the explanation of the function, I want to explain first how to configure the Azure function to use Firebase node package. Let's remember that this function is going to run in Azure serverless. That only means that we don't have full control over our server but we still can run NPM in the function app's SCM (Kudu) command line interface in order to add packages to our app:

  1. Navigate to: https://<function_app_name>.scm.azurewebsites.net.
  2. Click Debug Console > CMD.
  3. Navigate to D:\home\site\wwwroot\<function_name>.
  4. Run npm install firebase --save.

kudu

After doing this we will be able to import our Firebase library like this: var firebase = require('firebase');.

After that, we can use our Firebase object as we did before. Let's dive directly into our function so I can explain to you what's going on. First, I use the method signInAnonymously under the object auth because I need a logged-in user to write in my database. Don't worry — we haven't talked about this yet, but believe me, I'm going to explain to you more about database rules and authentication with Firebase.

Then, I use the object that I receive as a parameter from my Azure Service Bus and directly store it in our database. I'll be posting about the other parts of this system so you can know more about Microsoft Bot Framework and Azure Service Bus.

For more details about the Azure functions, you can use this link. For more details about the NodeJs Azure functions, you can use this link. For mode details about the Firebase NPM package, us this link.

If you found this post useful, please don't forget to press the like button and share it. If you are in doubt, don't hesitate to ask a question and, as always, thank you for reading.

Unleash the power of your APIs with future-proof API management - Create your account and start your free trial today, brought to you in partnership with 3scale.

Topics:
firebase ,azure service bus ,nodejs ,integration ,tutorial ,microsoft bot framework

Published at DZone with permission of Ezequiel Reyno. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}