1. Home
  2. Docs
  3. Tutorials
  4. Using Serverless Backend Functions

Using Serverless Backend Functions

Conversation.one serverless backend functions bind into services to build rich, serverless scenarios by capitalizing on existing services and micro-services, as well as 3rd party services. This tutorial will guide you through the process of creating your own micro-service, that you can later consume in your conversational application.

Some of the information in this guide is more developer-centric, but don’t be discouraged. If you follow the steps as they’re outlined, you will have a deployed, working “what happened today” app.

Create a conversational app

A Conversation.one application represents your conversational interface through Amazon Alexa, Google Home or Facebook Messenger bot. To create an app:

  1. If you don’t already have a Conversation.one account, sign up. If you have an account, log in.
  2. Click on Create New App in the navigation. If it’s your first application, you will be redirected to the Create New App wizard automatically.
  3. Select your industry and enter your company name. For the purpose of this tutorial, select “Other” for the industry, and enter “What Happened Today” as your company name. Uncheck “Add demo data” to start with an empty application. Click Next to continue.
  4. Select the devices you want to support with your app. For this demo select Messenger, Amazon Alexa, Google Home, and Chatbot. Click Next to continue.
  5. Your new application is ready. Click Customize to start developing your app.

Add serverless backend functions

On the “Functions” tab, add the following code:

function whatHappendToday(context, req, res) {
    var today = new Date();
    var day = today.getDate();
    var month = today.getMonth() + 1;
    return fetch("http://history.muffinlabs.com/date/" + month + "/" + day)
        .then(function(res) {
            return res.json();
        }).then(function(json) {
            var events = json.data.Events;
            var randomEvent = Math.floor(Math.random() * (events.length));
            var selectedEvent = events[randomEvent];
            res.send(selectedEvent);
        });
}

The code above is running the following steps:

  1. Find today’s date
  2. Call the history API using fetch API
  3. Select random event and return it using “res.send” command

Try it out

You can test your new function using your browser. The link to your new function would be “https://engine.conversation.one/functions/{AppId}/{FunctionName}”. Where “AppId” is the ID of your conversational app and “FunctionName” is the name of your function, which in this case would be “whatHappendToday”.

For example, if your app id is 6638 the URL will be https://engine.conversation.one/functions/6638/whatHappendToday. Click on the link, and your browser should return a JSON representation of an interesting event that happened today:

{
  "year": "1979",
  "text": "The last Iranian Shah flees Iran with his family for good and relocates to Egypt.",
  "html": "The <a href=\"https://wikipedia.org/wiki/Mohammad_Reza_Pahlavi\" title=\"Mohammad Reza Pahlavi\">last Iranian Shah</a> flees <a href=\"https://wikipedia.org/wiki/Iran\" title=\"Iran\">Iran</a> with his family for good and relocates to <a href=\"https://wikipedia.org/wiki/Egypt\" title=\"Egypt\">Egypt</a>.",
  "links": [{
    "title": "Mohammad Reza Pahlavi",
    "link": "https://wikipedia.org/wiki/Mohammad_Reza_Pahlavi"
  }, {
    "title": "Iran",
    "link": "https://wikipedia.org/wiki/Iran"
  }, {
    "title": "Egypt",
    "link": "https://wikipedia.org/wiki/Egypt"
  }]
}

Create an intent

An intent maps what a user says with what your app does. The intent we will create now will be invoked when the user asks for information on what happened today.

To create an intent:

  1. Create a new intent named “whatHappenedToday”
  2. In the User Says section, enter examples of what you might expect a user to ask for. The more examples you provide, the more ways a user can ask a question and the conversational app will understand. Enter these examples:
    • What happened today in history
    • What happened on this day in the past
    • What took place today
    • What occurred today
  3. Click Save to apply your changes

Add code response

  1. In the Response section, select “From Code” and enter the code:
    return fetch("https://engine.conversation.one/functions/6638/whatHappendToday")
     .then(function(res) {
         return res.json();
     }).then(function(json) {
         res.say('On this day,'+json.year+' - '+json.text.replace(/(\(.*\))/g,''));
     });
  2. Click Save to apply your changes

Try it out

Now that your app can return real historical events to the user, try out what you have so far.

In the chat console on the right, type in a request. The request can be a little different than the examples you provided in the User Says section. This can be something like “what popped up today”. After you type the request, hit “Send”.

The chatbot will answer with some interesting event from the past. For example “On this day,1974 – The Rumble in the Jungle boxing match between Muhammad Ali and George Foreman takes place in Kinshasa, Zaire. Ali wins by KO in the eighth round, regaining the title of World Heavyweight Champion and causing Foreman´s first professional defeat.”

What’s next?

Was this article helpful to you? Yes No

How can we help?