Skip to content

Instantly share code, notes, and snippets.

@netomarin
Created October 29, 2018 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save netomarin/95940e4a5c5338a62749630b9938130d to your computer and use it in GitHub Desktop.
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
// 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);
@dungpak
Copy link

dungpak commented Dec 26, 2018

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment