Skip to content

Instantly share code, notes, and snippets.

@brentschooley
Created May 4, 2020 06:10
Show Gist options
  • Save brentschooley/33895752c207b1e44b15b429c86daa5a to your computer and use it in GitHub Desktop.
Save brentschooley/33895752c207b1e44b15b429c86daa5a to your computer and use it in GitHub Desktop.
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