I need help setting up an automated SMS response system. I already have the daily SMS sending part working fine.
Basically I want to create a Twilio function that checks my Airtable database every day to process incoming SMS replies. The workflow should be:
- Function searches Airtable table for today’s date entries
- Gets the incoming SMS text content
- Checks if the reply matches what we expect and sends success message from database
- Sends different message from database if reply doesn’t match
Here’s my current function code:
const airtableLib = require("airtable");
const twilioLib = require("twilio");
exports.handler = function (context, event, callback) {
const db = new airtableLib({
apiKey: context.AIRTABLE_API_KEY,
}).base(context.AIRTABLE_BASE_ID);
const response = new twilioLib.twiml.MessagingResponse();
const today = new Date();
const userReply = event.Body.toLowerCase();
return db("daily_questions")
.select(entry.get("date") === today)
.all()
.then((entries) => {
entries.forEach((entry) => {
if (entry.get("expected_answer") === userReply) {
response.message(entry.get("success_msg"));
callback(null, response);
}
});
response.message(entry.get("failure_msg"));
callback(null, response);
})
.catch((err) => {
callback(err);
});
};
The function isn’t working as expected. Any ideas what might be wrong with my approach?