0

REPOST: I made a post yesterday looking for a solution to this problem, but I've put another 24 hours into and feel I'm a lot nearer a solution now.

The problem I believe I'm having is within the "Send Message to All Contacts" section. I'm running this through twilio functions, the basic premise of the code works when run locally, and does not produce errors when run with twilio functions, but it does not send the messages. It still sends the confirmation, the cancel, and deny work fine. Appreciate any insight into this.

// 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
      }
    ]
  })
};
  • Take a look at this, you need to nest the callback in a place where it does't execute before everything is completed. https://www.twilio.com/docs/runtime/functions/invocation#callback-and-asynchronous-limitations – Alan Sep 12 '21 at 01:15
  • I've closed this question as I saw the older one first and left a detailed answer there. I've also answered your more specific question about looping through asynchronous calls. – philnash Sep 13 '21 at 00:27

0 Answers0