Created
February 10, 2018 20:15
-
-
Save ikasamah/904e075f46e9418c6df569cfdc6755a2 to your computer and use it in GitHub Desktop.
Dialogflow fulfillment to post to slack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const SlackBot = require('slackbots'); | |
const channel = '<CHANNEL_TO_POST>'; | |
const bot = new SlackBot({ | |
token: '<SLACK_BOT_TOKEN>' | |
}); | |
const functions = require('firebase-functions'); // Cloud Functions for Firebase library | |
const DialogflowApp = require('actions-on-google').DialogflowApp; // Google Assistant helper library | |
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => { | |
console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers)); | |
console.log('Dialogflow Request body: ' + JSON.stringify(request.body)); | |
if (request.body.result) { | |
processV1Request(request, response); | |
} else { | |
console.log('Invalid Request'); | |
return response.status(400).end('Invalid Webhook Request (expecting v1 request)'); | |
} | |
}); | |
/* | |
* Function to handle v1 webhook requests from Dialogflow | |
*/ | |
function processV1Request (request, response) { | |
let action = request.body.result.action; // https://dialogflow.com/docs/actions-and-parameters | |
let parameters = request.body.result.parameters; // https://dialogflow.com/docs/actions-and-parameters | |
let inputContexts = request.body.result.contexts; // https://dialogflow.com/docs/contexts | |
let requestSource = (request.body.originalRequest) ? request.body.originalRequest.source : undefined; | |
const googleAssistantRequest = 'google'; // Constant to identify Google Assistant requests | |
const app = new DialogflowApp({request: request, response: response}); | |
// Create handlers for Dialogflow actions as well as a 'default' handler | |
const actionHandlers = { | |
// Default handler for unknown or undefined actions | |
'default': () => { | |
const body = request.body.result.parameters.any; | |
bot.postMessageToChannel(channel, body, {as_user:true}); | |
const message = 'スラックに' + body + 'とポストしました'; | |
let responseToUser = { | |
speech: message, // spoken response | |
text: message // displayed response | |
}; | |
sendGoogleResponse(responseToUser); | |
} | |
}; | |
// If undefined or unknown action use the default handler | |
if (!actionHandlers[action]) { | |
action = 'default'; | |
} | |
// Run the proper handler function to handle the request from Dialogflow | |
actionHandlers[action](); | |
// Function to send correctly formatted Google Assistant responses to Dialogflow which are then sent to the user | |
function sendGoogleResponse (responseToUser) { | |
if (typeof responseToUser === 'string') { | |
app.ask(responseToUser); // Google Assistant response | |
} else { | |
// If speech or displayText is defined use it to respond | |
let googleResponse = app.buildRichResponse().addSimpleResponse({ | |
speech: responseToUser.speech || responseToUser.displayText, | |
displayText: responseToUser.displayText || responseToUser.speech | |
}); | |
// Optional: Overwrite previous response with rich response | |
if (responseToUser.googleRichResponse) { | |
googleResponse = responseToUser.googleRichResponse; | |
} | |
// Optional: add contexts (https://dialogflow.com/docs/contexts) | |
if (responseToUser.googleOutputContexts) { | |
app.setContext(...responseToUser.googleOutputContexts); | |
} | |
console.log('Response to Dialogflow (AoG): ' + JSON.stringify(googleResponse)); | |
app.tell(googleResponse); // Send response to Dialogflow and Google Assistant | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment