Created
October 29, 2018 19:38
-
-
Save netomarin/95940e4a5c5338a62749630b9938130d to your computer and use it in GitHub Desktop.
Sample code for the blog post Handling Action’s No Match errors in Dialogflow: Three strikes and you’re out
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
// Copyright 2018, Google, Inc. | |
// | |
// Licensed under the Apache License, Version 2.0 (the "License"); | |
// you may not use this file except in compliance with the License. | |
// You may obtain a copy of the License at | |
// | |
// http://www.apache.org/licenses/LICENSE-2.0 | |
// | |
// Unless required by applicable law or agreed to in writing, software | |
// distributed under the License is distributed on an "AS IS" BASIS, | |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
// See the License for the specific language governing permissions and | |
// limitations under the License. | |
'use strict'; | |
const {dialogflow} = require('actions-on-google'); | |
const functions = require('firebase-functions'); | |
const app = dialogflow({debug: true}); | |
// Map the intent 'Default Fallback Intent' from the Dialogflow agent to this function. | |
app.intent('Default Fallback Intent', (conv) => { | |
// 'ticketCount' holds the count of how many tickets are left for the | |
// requested event. In this sample, ‘ticketCount’ has a fixed value of 17, | |
// but in a real application, you need to obtain the actual ticket count from | |
// the ticketing service via API and assign the value to ‘ticketCount’. | |
const ticketCount = 17; | |
// If 'fallbackCount' doesn't exist, create it and set the value to 0. | |
if (!conv.data.fallbackCount) { | |
conv.data.fallbackCount = 0; | |
} | |
// Increment the value of 'fallbackCount'. | |
conv.data.fallbackCount++; | |
// Adjust the fallback response according to the error count. | |
if (conv.data.fallbackCount === 1) { | |
return conv.ask('Sorry, how many was that?'); | |
} else if (conv.data.fallbackCount === 2) { | |
return conv.ask(`Sorry, I didn't get it. How many tickets would you like to purchase?`); | |
} else { | |
// If 'fallbackCount' is greater than 2, send out the final message and terminate the conversation. | |
return conv.close(`This seems like beyond my expertise. Let’s stop here. There are currently ${ ticketCount } seats available. Bye for now.`); | |
} | |
}); | |
// Required by Firebase | |
exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have tried the above fallback intent but it doesn't work on my end.
Does changing the language within the fulfillment make the code broke?
I use bahasa indonesia inside the (
sorry
)