Introducing Dialogflow's Node.js Fulfillment Library Beta

March 15, 2018

Fulfillment is a powerful way to connect Dialogflow’s natural language capabilities with your own backend, APIs, and databases to create contextual, personalized and actionable conversational experiences for your users. Dialogflow developers are using fulfillment to allow their users to order items, retrieve user-specific information such as emails, and control devices like a photobooth. Today, we’re simplifying Node.js fulfillment development with the beta release of the Dialogflow Fulfillment Library.

The new fulfillment library works seamlessly with text, card, image and suggestion responses across v1 and v2 agents, 8 chat and voice platforms, as well as Dialogflow’s own simulator. The library also supports custom payloads, which are platform-specific responses, for all 14 Dialogflow-supported platforms. It includes an integration with the new Actions on Google client library to easily create responses for the Google Assistant. Read on to see how you can use the Dialogflow Fulfillment Library to build agents across platforms and languages, and integrate with platforms like the Google Assistant.

Cross-platform responses

The fulfillment library supports text, card, image, and suggestion chip responses for Dialogflow’s simulator and these 8 platforms: the Google Assistant, Facebook, Slack, Telegram, Kik, Skype, Line, and Viber. See how to add text, card, and suggestions with the code below and what the responses look like on the Google Assistant, Dialogflow’s simulator, Slack, and Facebook Messenger:

const functions = require('firebase-functions');
const {WebhookClient, Card, Suggestion} = require('dialogflow-fulfillment');

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((req, res) => {
  const agent = new WebhookClient({ req, res });
  
  function intentHandler(agent) {
    agent.add('This message is from Dialogflow\'s Cloud Functions for Firebase editor!');
    agent.add(new Card({
        title: 'Title: this is a card title',
        imageUrl: 'https://developers.google.com/actions/assistant.png',
        text: 'This is the body text of a card.  You can even use line\n  breaks and emoji! 💁',
        buttonText: 'This is a button',
        buttonUrl: 'https://assistant.google.com/'
      })
    );
    agent.add(new Suggestion('Quick Reply'));
    agent.add(new Suggestion('Suggestion'));
  }

  agent.handleRequest(intentHandler);
});

Copy and paste the above code into Dialogflow’s inline editor to try it now or check out the full quick start sample here.

Google Assistant Dialogflow's simulator
alt_text alt_text
Slack Facebook Messenger
alt_text alt_text

See the reference documentation for other rich response types, such as images.

Multilingual responses

You can also build multilingual and locale fulfillment using the locale attribute. See how to say hello in French or English depending on the language of the request with the code below:

const functions = require('firebase-functions');
const {WebhookClient, Card, Suggestion} = require('dialogflow-fulfillment');

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((req, res) => {
  const agent = new WebhookClient({ req, res });
  function welcome(agent) {   // English handler function & intent map
    agent.add('Welcome to my agent!');
  } 
  function bienvenue(agent) {  // French handler function & intent map
    agent.add('Bienvenue à mon agent!');
  }
  
  if (agent.locale === 'en') {
    agent.handleRequest(welcome);
  } else if (agent.locale === 'fr') {
    agent.handleRequest(bienvenue);
  }
});

Copy and paste the above code into Dialogflow’s inline editor to try it now or check out the full multilingual and locale sample here.

Custom payloads and the Google Assistant Integration

The fulfillment library supports custom payload responses, which are platform-specific features such as authentication and transactions used by Dialogflow-supported platforms. These payloads will be sent to the target platform in place of other messages defined in the library. The example below shows how to add custom JSON payloads for the Google Assistant and Slack:

agent.add(new Payload(agent.ACTIONS_ON_GOOGLE, {/*your Google payload here*/});
agent.add(new Payload(agent.SLACK, {/*your Slack payload here*/});

Dialogflow’s fulfillment library also has an integration with the Google Assistant using Actions on Google’s v2 alpha client library. This integration enables creation of custom Actions on Google payloads through the Actions on Google v2 client library. Here is an example of how to ask for the user’s location through the Google Assistant:

const functions = require('firebase-functions');
const {WebhookClient, Card, Suggestion} = require('dialogflow-fulfillment');
const {Permission} = require('actions-on-google');

exports.dialogflowFirebaseFulfillment = functions.https.onRequest((req, res) => {
  const agent = new WebhookClient({ req, res });

  function intentHandler(agent) {
    let conv = agent.conv();
    conv.ask(new Permission({
      context: 'To give results in your area',
      permissions: 'DEVICE_PRECISE_LOCATION',
    }))
    agent.add(conv);
  }

  agent.handleRequest(intentHandler);
});

Copy and paste the above code into Dialogflow’s inline editor to try it now or check out the full Actions on Google sample here.

Next steps

Check out the Dialogflow fulfillment library on Github and npm, along with our quick start sample and getting started guide to get up and running today. Send us feedback, feature requests or bugs by opening an issue on GitHub.

We’ll be discussing the library, your feedback and feature requests on Dialogflow’s Google+ community. We look forward to hearing from you and seeing what you build with the Dialogflow fulfillment library!

Posted by Matt Carroll, Dialogflow Developer Relations





Subscribe Via Email

Enter your email address:

Delivered by FeedBurner

About

Welcome to the Dialogflow blog. Subscribe to get product updates, best practices, and tutorials directly in your inbox.

Resources

Follow us on social