Created
May 4, 2020 06:10
-
-
Save brentschooley/33895752c207b1e44b15b429c86daa5a to your computer and use it in GitHub Desktop.
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
const { GoogleSpreadsheet } = require("google-spreadsheet"); | |
let fs = require('fs'); | |
let credsFile = Runtime.getAssets()['/creds.json'].path; | |
let creds = JSON.parse(fs.readFileSync(credsFile, 'utf8')); | |
async function getCritter(critterName) { | |
const doc = new GoogleSpreadsheet(process.env.GOOGLE_SPREADSHEET_KEY); | |
await doc.useServiceAccountAuth(creds); | |
await doc.loadInfo(); | |
const fishSheet = doc.sheetsByIndex[0]; | |
const bugSheet = doc.sheetsByIndex[1]; | |
const bugs = await bugSheet.getRows(); | |
const fish = await fishSheet.getRows(); | |
const rows = bugs.concat(fish); | |
const critter = rows.find(row => row.Name.toLowerCase() === critterName.toLowerCase()); | |
return critter; | |
} | |
exports.handler = async function(context, event, callback) { | |
const twiml = new Twilio.twiml.MessagingResponse(); | |
const critterName = event.Body.trim(); | |
console.log(critterName); | |
const critter = await getCritter(critterName); | |
if(critter) { | |
twiml.message(`The price of your ${critterName} is: ${critter.Price} bells.`); | |
} else { | |
twiml.message(`Sorry, I can't find a price for ${event.Body}. Please try again.`) | |
} | |
callback(null, twiml); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment