Why are my messages not sending?

// Broadcasting Brain
exports.handler = async function(context, event, callback) {

//Twilio and Deta, Etc Const
const { Deta } = require("deta");
const client = require('twilio')(context.ACCOUNT_SID, context.AUTH_TOKEN);
const deta = Deta(context.DETA_PROJECT_KEY);
const db = deta.Base("users2");
const messaging_service = context.MESSAGING_SERVICE;
const memory = JSON.parse(event.Memory);
const from = event.UserIdentifier;
const current_user = await db.get(from); // Check if verified and set name.
const user_name = current_user.name || "Friend"; // Get the current users name or set a default value

// These will be filled in later.
let responseObject;
let message;

// Figure out if message came from short circuit broadcast or normal
if (memory.triggered) {
message = memory.message;
} else {
message = memory.twilio.collected_data.broadcast_message.answers.message_input.answer;
}

if (from === context.BROADCAST_NUMBER) { // Is user is an authorized broadcaster.

// Decide if the sending of a message should be cancelled.
if (message.toLowerCase() === "c" || message.toLowerCase() === "cancel") {
  responseObject = { "actions": [ { "say": `${user_name}, you have canceled your request and no messages have been sent.`},{"listen": false}]}
callback(null, responseObject);

} else { // Move forward with sending a message.
// Read records from database
let res = await db.fetch();
let allItems = res.items;
while (res.last){
res = await db.fetch({}, {last: res.last});
allItems = allItems.concat(res.items);
}
// End read from database

** // Send message to each contact.
await allItems.forEach(contact => {
client.messages
.create({
body: "Hey " + contact.name + "!" + " " + message,
messagingServiceSid: messaging_service,
to: contact.key
})
});
// End send message to each contact**

// Get total number of Contacts
let number_of_contacts = allItems.length;
// End get total number of contacts

// Set Confirmation Message to Broadcaster
responseObject = {
"actions": [
{
"say": ${user_name}, your broadcast has successfully sent to ${number_of_contacts} contacts.
},
{
"listen": false
}
]
}
// End set confirmation message

callback(null, responseObject);
}

// The user is not authorized so return this.
}
return callback(null, {
"actions": [
{
"say": "You are not authorized to broadcast."
},
{
"listen": false
}
]
})
};

Above is my code and I've highlighted what I believe to be my problem. This is a repost from stack overflow but with another 24 hours of work on it, I feel code is much cleaner and closer to the solution I'm after.

Right now, everything works except broadcasting messages out to those on the list. I am getting no errors. I'm able to send messages in this fashion when running locally using node, but the sending functionality seems to break when integrating everything together.

I am new to node, so it's likely I'm overlooking something rather simple.

Thanks for any feedback.

Answers