Can Tito Eat That?
@dylan
code:
data:privatelast updated:4 years ago
today
Build integrations remarkably fast!
You're viewing a public workflow template.
Sign up to customize, add steps, modify code and more.
Join 1,000,000+ developers using the Pipedream platform
steps.
trigger
HTTP API
Deploy to generate unique URL
This workflow runs on Pipedream's servers and is triggered by HTTP / Webhook requests.
steps.
cache_google_sheet_in_$checkpoint
auth
to use OAuth tokens and API keys in code via theauths object
(auths.google_sheets)
params
SpreadhsheetId
 
string ·params.spreadhsheetId
Range
 
string ·params.range
code
Write any Node.jscodeand use anynpm package. You can alsoexport datafor use in later steps via return or this.key = 'value', pass input data to your code viaparams, and maintain state across executions with$checkpoint.
async (event, steps, params, auths) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
}
27
// 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")
}
steps.
exit_if_handler_not_defined
auth
to use OAuth tokens and API keys in code via theauths object
code
Write any Node.jscodeand use anynpm package. You can alsoexport datafor use in later steps via return or this.key = 'value', pass input data to your code viaparams, and maintain state across executions with$checkpoint.
async (event, steps) => {
1
2
3
4
}
5
if (steps.trigger.event.body.handler.name !== "get_data") {
  $end("Not for the main handler, exiting")
}
steps.
find_item_and_return_answer
auth
to use OAuth tokens and API keys in code via theauths object
code
Write any Node.jscodeand use anynpm package. You can alsoexport datafor use in later steps via return or this.key = 'value', pass input data to your code viaparams, and maintain state across executions with$checkpoint.
async (event, steps) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
}
32
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]
steps.
return_response
auth
to use OAuth tokens and API keys in code via theauths object
code
Write any Node.jscodeand use anynpm package. You can alsoexport datafor use in later steps via return or this.key = 'value', pass input data to your code viaparams, and maintain state across executions with$checkpoint.
async (event, steps) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
}
24
$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: {}
    }
  }
})