Google Dialogflow Chatbot & Slack Tutorial: Trigger a Slack Notification from a Firebase Webhook

In this blog post I’m going to share the code from my video tutorial on how to trigger a Slack message from a Dialogflow chatbot:

Here is the index.js:


// See https://github.com/dialogflow/dialogflow-fulfillment-nodejs
// for Dialogflow fulfillment library docs, samples, and to report issues
'use strict';
 
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const requestLib = require('request');

// initialise DB connection
const admin = require('firebase-admin');
admin.initializeApp();
 
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
 
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
  const agent = new WebhookClient({ request, response });
  console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
  console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
 
  function saveName(agent) {
    const nameParam = agent.parameters.name;
    const context = agent.getContext('awaiting_name_confirm');
    const name = nameParam || context.parameters.name;
    
    agent.add(`Thank you, ` + name + `!`);
    sendSlackMessage(name);
    
    return admin.database().ref('/names').push({name: name}).then((snapshot) => {
    // Redirect with 303 SEE OTHER to the URL of the pushed object in the Firebase console.
    console.log('database write sucessful: ' + snapshot.ref.toString());
  });
  }
  
  function sendSlackMessage(name) {
    let slackMessageBody = {
      "username": "Get Name Chatbot",
      "text": "New submission from: " + name,
      "icon_emoji": ":tada:"
    };
    
    requestLib.post({
      headers: {'content-type' : 'application/json'},
      url:     "https://hooks.slack.com/services/[...]",
      body:    JSON.stringify(slackMessageBody)
    }, function(error, response, body) {
      console.log('Slack notification response body: ' + JSON.stringify(body) + ', error: ' + error);
    });
  }

  // Run the proper function handler based on the matched Dialogflow intent name
  let intentMap = new Map();
  intentMap.set('Get Name', saveName);
  intentMap.set('Confirm Name Yes', saveName);
  // intentMap.set('Confirm Name Yes', getName);
  agent.handleRequest(intentMap);
});

And then we have the package.json for completeness:


{
  "name": "dialogflowFirebaseFulfillment",
  "description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase",
  "version": "0.0.1",
  "private": true,
  "license": "Apache Version 2.0",
  "author": "Google Inc.",
  "engines": {
    "node": "~6.0"
  },
  "scripts": {
    "start": "firebase serve --only functions:dialogflowFirebaseFulfillment",
    "deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment"
  },
  "dependencies": {
    "actions-on-google": "2.0.0-alpha.4",
    "firebase-admin": "^5.12.1",
    "firebase-functions": "^1.0.3",
    "dialogflow": "^0.1.0",
    "dialogflow-fulfillment": "0.3.0-beta.3",
    "request": "^2.83.0"
  }
}

Enjoy!