How to properly Loop through SMS sending via Twilio FunctionsResolve promises one after another (i.e. in sequence)?Twilio Function - WhisperTwilio Studio Function ParameterTwilio functions calling other Twilio functionsTwilio Studio: Forward SMS conversation log to emailVoice and Message in same response using Twilio FunctionsHow do I retrieve the Twilio fax PDF and attach it to an email using Node.js inside a Twilio function?Twilio functions realTimeStatistics is not a functionTwilio Function End runtimeTimer function using Twilio functionsTwilio - Send email from function
What happens if the limit of 4 billion files was exceeded in an ext4 partition?
Do intermediate subdomains need to exist?
When is one 'Ready' to make Original Contributions to Mathematics?
Why no parachutes in the Orion AA2 abort test?
Does the Milky Way orbit around anything?
Is this standard Japanese employment negotiations, or am I missing something?
How important is it for multiple POVs to run chronologically?
Tiny URL creator
Why do most airliners have underwing engines, while business jets have rear-mounted engines?
Can you take the Dodge action while prone?
Why do we need a bootloader separate from our application program in microcontrollers?
Can a USB hub be used to access a drive from two devices?
Machine Learning Golf: Multiplication
What is the fundamental difference between catching whales and hunting other animals?
Do I need transit visa for Dublin?
Do grungs have a written language?
How to say "just a precision" properly in English?
Did William Shakespeare hide things in his writings?
Why would "dead languages" be the only languages that spells could be written in?
How to play a D major chord lower than the open E major chord on guitar?
How to delete multiple process id of a single process?
White's last move?
is this a question or an affirmation?
Why does mean tend be more stable in different samples than median?
How to properly Loop through SMS sending via Twilio Functions
Resolve promises one after another (i.e. in sequence)?Twilio Function - WhisperTwilio Studio Function ParameterTwilio functions calling other Twilio functionsTwilio Studio: Forward SMS conversation log to emailVoice and Message in same response using Twilio FunctionsHow do I retrieve the Twilio fax PDF and attach it to an email using Node.js inside a Twilio function?Twilio functions realTimeStatistics is not a functionTwilio Function End runtimeTimer function using Twilio functionsTwilio - Send email from function
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have Twilio Studio calling a Twilio Function and need it to send email to a variable list (small list) of emails. This question is mostly around looping through them as I can pass variables just fine. I have an array of emails to send a text to and am in a Twilio Function. But all examples I find online are about sending to just ONE. Part of me thinks this needs to be a Twilio Function calling another Twilio function (one loops, the other sends Emails)... but I can't figure out a way to do that. If I could contain it to one Twilio function, that would be great.
I have Twilio Studio calling a Twilio function. I need to keep this all on Twilio... so looping through via PHP and running functions one at a time through there doesn't work. I need this to run on Twilio's serverless setup.
Here's an example of what I have that works:
exports.handler = function(context, event, callback)
// using SendGrid's v3 Node.js Library
// https://github.com/sendgrid/sendgrid-nodejs
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(context.SENDGRID_API_KEY);
const msg =
to: 'me@example.com',
from: 'noreply@example.com',
templateId: 'my-id-goes-here',
dynamic_template_data:
recipient_name: 'John Smith'
;
sgMail.send(msg).then(response =>
let twiml = new Twilio.twiml.MessagingResponse();
callback(null, twiml);
)
.catch(err =>
callback(err);
);
;
Here's me trying to loop through in similar fashion and failing
exports.handler = function(context, event, callback)
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(context.SENDGRID_API_KEY);
var responder_emails = 'me@example.com,me+test1@example.com';
var emails_a = responder_emails.split(',');
emails_a.forEach(function(responder_email)
const msg =
to: responder_email,
from: 'noreply@example.com',
templateId: 'my-id-goes-here',
dynamic_template_data:
recipient_name: 'John Smith'
;
sgMail.send(msg);
);
callback();
;
I can pass in multiple emails into a Twilio function... I'm just not sure how to loop through correctly.
twilio twilio-functions
add a comment |
I have Twilio Studio calling a Twilio Function and need it to send email to a variable list (small list) of emails. This question is mostly around looping through them as I can pass variables just fine. I have an array of emails to send a text to and am in a Twilio Function. But all examples I find online are about sending to just ONE. Part of me thinks this needs to be a Twilio Function calling another Twilio function (one loops, the other sends Emails)... but I can't figure out a way to do that. If I could contain it to one Twilio function, that would be great.
I have Twilio Studio calling a Twilio function. I need to keep this all on Twilio... so looping through via PHP and running functions one at a time through there doesn't work. I need this to run on Twilio's serverless setup.
Here's an example of what I have that works:
exports.handler = function(context, event, callback)
// using SendGrid's v3 Node.js Library
// https://github.com/sendgrid/sendgrid-nodejs
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(context.SENDGRID_API_KEY);
const msg =
to: 'me@example.com',
from: 'noreply@example.com',
templateId: 'my-id-goes-here',
dynamic_template_data:
recipient_name: 'John Smith'
;
sgMail.send(msg).then(response =>
let twiml = new Twilio.twiml.MessagingResponse();
callback(null, twiml);
)
.catch(err =>
callback(err);
);
;
Here's me trying to loop through in similar fashion and failing
exports.handler = function(context, event, callback)
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(context.SENDGRID_API_KEY);
var responder_emails = 'me@example.com,me+test1@example.com';
var emails_a = responder_emails.split(',');
emails_a.forEach(function(responder_email)
const msg =
to: responder_email,
from: 'noreply@example.com',
templateId: 'my-id-goes-here',
dynamic_template_data:
recipient_name: 'John Smith'
;
sgMail.send(msg);
);
callback();
;
I can pass in multiple emails into a Twilio function... I'm just not sure how to loop through correctly.
twilio twilio-functions
add a comment |
I have Twilio Studio calling a Twilio Function and need it to send email to a variable list (small list) of emails. This question is mostly around looping through them as I can pass variables just fine. I have an array of emails to send a text to and am in a Twilio Function. But all examples I find online are about sending to just ONE. Part of me thinks this needs to be a Twilio Function calling another Twilio function (one loops, the other sends Emails)... but I can't figure out a way to do that. If I could contain it to one Twilio function, that would be great.
I have Twilio Studio calling a Twilio function. I need to keep this all on Twilio... so looping through via PHP and running functions one at a time through there doesn't work. I need this to run on Twilio's serverless setup.
Here's an example of what I have that works:
exports.handler = function(context, event, callback)
// using SendGrid's v3 Node.js Library
// https://github.com/sendgrid/sendgrid-nodejs
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(context.SENDGRID_API_KEY);
const msg =
to: 'me@example.com',
from: 'noreply@example.com',
templateId: 'my-id-goes-here',
dynamic_template_data:
recipient_name: 'John Smith'
;
sgMail.send(msg).then(response =>
let twiml = new Twilio.twiml.MessagingResponse();
callback(null, twiml);
)
.catch(err =>
callback(err);
);
;
Here's me trying to loop through in similar fashion and failing
exports.handler = function(context, event, callback)
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(context.SENDGRID_API_KEY);
var responder_emails = 'me@example.com,me+test1@example.com';
var emails_a = responder_emails.split(',');
emails_a.forEach(function(responder_email)
const msg =
to: responder_email,
from: 'noreply@example.com',
templateId: 'my-id-goes-here',
dynamic_template_data:
recipient_name: 'John Smith'
;
sgMail.send(msg);
);
callback();
;
I can pass in multiple emails into a Twilio function... I'm just not sure how to loop through correctly.
twilio twilio-functions
I have Twilio Studio calling a Twilio Function and need it to send email to a variable list (small list) of emails. This question is mostly around looping through them as I can pass variables just fine. I have an array of emails to send a text to and am in a Twilio Function. But all examples I find online are about sending to just ONE. Part of me thinks this needs to be a Twilio Function calling another Twilio function (one loops, the other sends Emails)... but I can't figure out a way to do that. If I could contain it to one Twilio function, that would be great.
I have Twilio Studio calling a Twilio function. I need to keep this all on Twilio... so looping through via PHP and running functions one at a time through there doesn't work. I need this to run on Twilio's serverless setup.
Here's an example of what I have that works:
exports.handler = function(context, event, callback)
// using SendGrid's v3 Node.js Library
// https://github.com/sendgrid/sendgrid-nodejs
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(context.SENDGRID_API_KEY);
const msg =
to: 'me@example.com',
from: 'noreply@example.com',
templateId: 'my-id-goes-here',
dynamic_template_data:
recipient_name: 'John Smith'
;
sgMail.send(msg).then(response =>
let twiml = new Twilio.twiml.MessagingResponse();
callback(null, twiml);
)
.catch(err =>
callback(err);
);
;
Here's me trying to loop through in similar fashion and failing
exports.handler = function(context, event, callback)
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(context.SENDGRID_API_KEY);
var responder_emails = 'me@example.com,me+test1@example.com';
var emails_a = responder_emails.split(',');
emails_a.forEach(function(responder_email)
const msg =
to: responder_email,
from: 'noreply@example.com',
templateId: 'my-id-goes-here',
dynamic_template_data:
recipient_name: 'John Smith'
;
sgMail.send(msg);
);
callback();
;
I can pass in multiple emails into a Twilio function... I'm just not sure how to loop through correctly.
twilio twilio-functions
twilio twilio-functions
asked Mar 25 at 20:29
eahoneteahonet
84 bronze badges
84 bronze badges
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Heyo. Twilio Evangelist here. 👋
In your first example, you rightfully waited for the send
call to be done by using then
. In your second example, you missed that. You run several send
calls but immediately call callback
without waiting.
A fixed (roughly prototyped version) could look as follows.
exports.handler = function(context, event, callback)
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(context.SENDGRID_API_KEY);
var responder_emails = 'me@example.com,me+test1@example.com';
var emails_a = responder_emails.split(',');
Promise.all(emails_a.map(function(responder_email)
const msg =
to: responder_email,
from: 'noreply@example.com',
templateId: 'my-id-goes-here',
dynamic_template_data:
recipient_name: 'John Smith'
;
return sgMail.send(msg);
)).then(function()
callback();
).catch(function(e)
callback(e);
)
);
You have already an array of emails because you called split
. You can use this array in combination with Array.map
and Promise.all
.
Map basically iterates over your array and lets you create a new array with whatever you return from the function inside of map. What the code above does is that it transforms [email, email]
to [Promise, Promise]
. The promises are the return value of sgMail.send
.
Now, that you have an array holding promises that will resolve when sendgrid accepted your call, you can use Promise.all
. This method waits for all the promises to be resolved (or rejected) and returns itself a new promise which you can use then
with. When all sendgrid calls are done it's time to finish the function by calling the function callback
.
Side note: this "map/Promise.all" trick performs all send grid calls in parallel. There might be situations where you want to call them one after another (saying you are doing a lot of calls and run into rate limiting).
Hope that helps and let me know how it goes. :)
My coworkers just saw me to a happy dance. Using Promise was the missing piece for me. I do understand what you mean by rate limiting, we're already planning for increases in those with Twilio. But in this function, it's fairly contained to a handful each time it's called. Thank you!
– eahonet
Mar 26 at 19:03
Happy I could help. 😊
– stefan judis
Mar 26 at 22:12
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55345928%2fhow-to-properly-loop-through-sms-sending-via-twilio-functions%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Heyo. Twilio Evangelist here. 👋
In your first example, you rightfully waited for the send
call to be done by using then
. In your second example, you missed that. You run several send
calls but immediately call callback
without waiting.
A fixed (roughly prototyped version) could look as follows.
exports.handler = function(context, event, callback)
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(context.SENDGRID_API_KEY);
var responder_emails = 'me@example.com,me+test1@example.com';
var emails_a = responder_emails.split(',');
Promise.all(emails_a.map(function(responder_email)
const msg =
to: responder_email,
from: 'noreply@example.com',
templateId: 'my-id-goes-here',
dynamic_template_data:
recipient_name: 'John Smith'
;
return sgMail.send(msg);
)).then(function()
callback();
).catch(function(e)
callback(e);
)
);
You have already an array of emails because you called split
. You can use this array in combination with Array.map
and Promise.all
.
Map basically iterates over your array and lets you create a new array with whatever you return from the function inside of map. What the code above does is that it transforms [email, email]
to [Promise, Promise]
. The promises are the return value of sgMail.send
.
Now, that you have an array holding promises that will resolve when sendgrid accepted your call, you can use Promise.all
. This method waits for all the promises to be resolved (or rejected) and returns itself a new promise which you can use then
with. When all sendgrid calls are done it's time to finish the function by calling the function callback
.
Side note: this "map/Promise.all" trick performs all send grid calls in parallel. There might be situations where you want to call them one after another (saying you are doing a lot of calls and run into rate limiting).
Hope that helps and let me know how it goes. :)
My coworkers just saw me to a happy dance. Using Promise was the missing piece for me. I do understand what you mean by rate limiting, we're already planning for increases in those with Twilio. But in this function, it's fairly contained to a handful each time it's called. Thank you!
– eahonet
Mar 26 at 19:03
Happy I could help. 😊
– stefan judis
Mar 26 at 22:12
add a comment |
Heyo. Twilio Evangelist here. 👋
In your first example, you rightfully waited for the send
call to be done by using then
. In your second example, you missed that. You run several send
calls but immediately call callback
without waiting.
A fixed (roughly prototyped version) could look as follows.
exports.handler = function(context, event, callback)
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(context.SENDGRID_API_KEY);
var responder_emails = 'me@example.com,me+test1@example.com';
var emails_a = responder_emails.split(',');
Promise.all(emails_a.map(function(responder_email)
const msg =
to: responder_email,
from: 'noreply@example.com',
templateId: 'my-id-goes-here',
dynamic_template_data:
recipient_name: 'John Smith'
;
return sgMail.send(msg);
)).then(function()
callback();
).catch(function(e)
callback(e);
)
);
You have already an array of emails because you called split
. You can use this array in combination with Array.map
and Promise.all
.
Map basically iterates over your array and lets you create a new array with whatever you return from the function inside of map. What the code above does is that it transforms [email, email]
to [Promise, Promise]
. The promises are the return value of sgMail.send
.
Now, that you have an array holding promises that will resolve when sendgrid accepted your call, you can use Promise.all
. This method waits for all the promises to be resolved (or rejected) and returns itself a new promise which you can use then
with. When all sendgrid calls are done it's time to finish the function by calling the function callback
.
Side note: this "map/Promise.all" trick performs all send grid calls in parallel. There might be situations where you want to call them one after another (saying you are doing a lot of calls and run into rate limiting).
Hope that helps and let me know how it goes. :)
My coworkers just saw me to a happy dance. Using Promise was the missing piece for me. I do understand what you mean by rate limiting, we're already planning for increases in those with Twilio. But in this function, it's fairly contained to a handful each time it's called. Thank you!
– eahonet
Mar 26 at 19:03
Happy I could help. 😊
– stefan judis
Mar 26 at 22:12
add a comment |
Heyo. Twilio Evangelist here. 👋
In your first example, you rightfully waited for the send
call to be done by using then
. In your second example, you missed that. You run several send
calls but immediately call callback
without waiting.
A fixed (roughly prototyped version) could look as follows.
exports.handler = function(context, event, callback)
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(context.SENDGRID_API_KEY);
var responder_emails = 'me@example.com,me+test1@example.com';
var emails_a = responder_emails.split(',');
Promise.all(emails_a.map(function(responder_email)
const msg =
to: responder_email,
from: 'noreply@example.com',
templateId: 'my-id-goes-here',
dynamic_template_data:
recipient_name: 'John Smith'
;
return sgMail.send(msg);
)).then(function()
callback();
).catch(function(e)
callback(e);
)
);
You have already an array of emails because you called split
. You can use this array in combination with Array.map
and Promise.all
.
Map basically iterates over your array and lets you create a new array with whatever you return from the function inside of map. What the code above does is that it transforms [email, email]
to [Promise, Promise]
. The promises are the return value of sgMail.send
.
Now, that you have an array holding promises that will resolve when sendgrid accepted your call, you can use Promise.all
. This method waits for all the promises to be resolved (or rejected) and returns itself a new promise which you can use then
with. When all sendgrid calls are done it's time to finish the function by calling the function callback
.
Side note: this "map/Promise.all" trick performs all send grid calls in parallel. There might be situations where you want to call them one after another (saying you are doing a lot of calls and run into rate limiting).
Hope that helps and let me know how it goes. :)
Heyo. Twilio Evangelist here. 👋
In your first example, you rightfully waited for the send
call to be done by using then
. In your second example, you missed that. You run several send
calls but immediately call callback
without waiting.
A fixed (roughly prototyped version) could look as follows.
exports.handler = function(context, event, callback)
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(context.SENDGRID_API_KEY);
var responder_emails = 'me@example.com,me+test1@example.com';
var emails_a = responder_emails.split(',');
Promise.all(emails_a.map(function(responder_email)
const msg =
to: responder_email,
from: 'noreply@example.com',
templateId: 'my-id-goes-here',
dynamic_template_data:
recipient_name: 'John Smith'
;
return sgMail.send(msg);
)).then(function()
callback();
).catch(function(e)
callback(e);
)
);
You have already an array of emails because you called split
. You can use this array in combination with Array.map
and Promise.all
.
Map basically iterates over your array and lets you create a new array with whatever you return from the function inside of map. What the code above does is that it transforms [email, email]
to [Promise, Promise]
. The promises are the return value of sgMail.send
.
Now, that you have an array holding promises that will resolve when sendgrid accepted your call, you can use Promise.all
. This method waits for all the promises to be resolved (or rejected) and returns itself a new promise which you can use then
with. When all sendgrid calls are done it's time to finish the function by calling the function callback
.
Side note: this "map/Promise.all" trick performs all send grid calls in parallel. There might be situations where you want to call them one after another (saying you are doing a lot of calls and run into rate limiting).
Hope that helps and let me know how it goes. :)
answered Mar 25 at 21:24
stefan judisstefan judis
5984 silver badges11 bronze badges
5984 silver badges11 bronze badges
My coworkers just saw me to a happy dance. Using Promise was the missing piece for me. I do understand what you mean by rate limiting, we're already planning for increases in those with Twilio. But in this function, it's fairly contained to a handful each time it's called. Thank you!
– eahonet
Mar 26 at 19:03
Happy I could help. 😊
– stefan judis
Mar 26 at 22:12
add a comment |
My coworkers just saw me to a happy dance. Using Promise was the missing piece for me. I do understand what you mean by rate limiting, we're already planning for increases in those with Twilio. But in this function, it's fairly contained to a handful each time it's called. Thank you!
– eahonet
Mar 26 at 19:03
Happy I could help. 😊
– stefan judis
Mar 26 at 22:12
My coworkers just saw me to a happy dance. Using Promise was the missing piece for me. I do understand what you mean by rate limiting, we're already planning for increases in those with Twilio. But in this function, it's fairly contained to a handful each time it's called. Thank you!
– eahonet
Mar 26 at 19:03
My coworkers just saw me to a happy dance. Using Promise was the missing piece for me. I do understand what you mean by rate limiting, we're already planning for increases in those with Twilio. But in this function, it's fairly contained to a handful each time it's called. Thank you!
– eahonet
Mar 26 at 19:03
Happy I could help. 😊
– stefan judis
Mar 26 at 22:12
Happy I could help. 😊
– stefan judis
Mar 26 at 22:12
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55345928%2fhow-to-properly-loop-through-sms-sending-via-twilio-functions%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown