auths objectreturn or this.key = 'value', pass input data to your code viaparams, and maintain state across executions with$checkpoint.async (event, steps, params, auths) => {}// On warming requests, we also cache the sheet in $checkpoint
// so we don't need to make the API request to Google each time
this.path = (new URL(event.url)).pathname
if (this.path === "/warm") {
const { google } = require('googleapis')
const auth = new google.auth.OAuth2()
auth.setCredentials({ access_token: auths.google_sheets.oauth_access_token })
const sheets = await google.sheets({ version: 'v4', auth });
const response = await sheets.spreadsheets.values.get({
spreadsheetId: params.spreadhsheetId,
range: params.range
})
$checkpoint = response.data.values
$respond({
body: {
warmed: true,
}
})
$end("Warming request, ending early")
}auths objectreturn or this.key = 'value', pass input data to your code viaparams, and maintain state across executions with$checkpoint.async (event, steps) => {}if (steps.trigger.event.body.handler.name !== "get_data") {
$end("Not for the main handler, exiting")
}auths objectreturn or this.key = 'value', pass input data to your code viaparams, and maintain state across executions with$checkpoint.async (event, steps) => {}const levenshtein = require('fast-levenshtein')
const dataWithoutHeaders = $checkpoint.slice(1)
// The item is in the first element (column) of each row's data
const items = dataWithoutHeaders.map(el => el[0])
let itemIndex
let lowestDistance = Infinity
for (const [index, item] of items.entries()) {
const distance = levenshtein.get(item, steps.trigger.event.body.intent.params.FoodItem.resolved);
console.log(`Distance for ${item}: ${distance}`)
if (distance < lowestDistance) {
lowestDistance = distance
itemIndex = index
}
}
console.log(`Lowest distance: ${lowestDistance}`)
console.log(`Index: ${itemIndex}`)
// Consider long distances to be no match - we don't
// want Cauliflower to win if the query is "Brocolli"
const DISTANCE_THRESHOLD = 3
if (lowestDistance >= 3 || itemIndex === undefined) {
return "This isn't in my database"
}
// Yes / No is in the second column of the sheet
return dataWithoutHeaders[itemIndex][1]auths objectreturn or this.key = 'value', pass input data to your code viaparams, and maintain state across executions with$checkpoint.async (event, steps) => {}$respond({
status: 200,
headers: {
"Content-Type": "application/json",
},
body: {
session: {
id: steps.trigger.event.body.session.id,
params: {}
},
prompt: {
firstSimple: {
speech: steps.find_item_and_return_answer.$return_value,
}
},
scene: {
name: "GetData",
slotFillingStatus: "UNSPECIFIED",
slots: {}
}
}
})