Last active
January 22, 2022 15:57
-
-
Save BennyJW/d27186e421bc5cdd8c7daf9c4689808f to your computer and use it in GitHub Desktop.
Example Twilio function to query Firebase Firestore and return result
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 axios = require('axios'); | |
exports.handler = async (context, event, callback) => { | |
// The event object should include any parameters from Twilio Studio | |
// as event.<parameter> | |
console.log(`Query_firebase function running. Event:`); | |
console.log(event); | |
try { | |
const response = await axios.post( | |
`https://firestore.googleapis.com/v1beta1/projects/georgia-courtbot/databases/(default)/documents:runQuery`, | |
{ structuredQuery: | |
{ | |
from: [{ collectionId: 'users' }], | |
where: { | |
fieldFilter: { | |
field: { | |
fieldPath: 'phone' | |
}, | |
op: 'EQUAL', | |
value: { | |
stringValue: event.phone.trim() | |
} | |
} | |
}, | |
limit: 1 | |
} | |
}); | |
console.log(`Response data from firebase:`); | |
console.log(response.data) | |
// Assume we get at most one document back | |
if (response.data[0].document) { | |
return callback(null, { | |
user_exists: true, | |
user_fields: response.data[0].document.fields | |
}); | |
} else { | |
return callback(null, {user_exists: false}) | |
} | |
} catch (error) { | |
console.log(error); | |
return callback(error); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment