使用 Twilio 发送数千条短信 [英] Send thousands of SMS with Twilio

查看:39
本文介绍了使用 Twilio 发送数千条短信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 Twilio 发送约 50,000 条短信,我只是想知道如果我循环访问这种大小的电话号码数组,我的请求是否会崩溃.事实上,Twilio 只允许每个请求 1 条消息,所以我必须制作 50,000 条消息.

I would like to send ~50,000 SMS with Twilio, and I was just wondering if my requests are going to crash if I loop through a phone number array of this size. The fact is that Twilio only allows 1 message for each request, so I have to make 50,000 of them.

是否可以这样做,或者我必须找到另一种方式?50,000 似乎太多了,但我不知道我可以做多少请求.

Is it possible to do it this way or do I have to find another way? 50,000 seems too much but I have no idea of how many requests I can do.

phoneNumbers.forEach(function(phNb)
{
    client.messages.create({
        body: msgCt,
        to: phNb,
        from: ourPhone
    })
    .then((msg) => {
        console.log(msg.sid);
    });
})

提前致谢

推荐答案

这里有两个因素.

  • 您需要考虑Twilio Api 使用限制.
  • 执行 50.000 个并行 http 请求(实际上是您的代码执行)不是一个好主意:您会遇到内存问题.

Twilio 短信根据源和目标限制更改.

Twilio sms limits change based on source and destination.

您有两个解决方案:

按顺序执行 50k 个 http 请求

    phoneNumbers.forEach(async function(phNb){
      try {
        let m = await client.messages.create({
          body: msgCt,
          to: phNb,
          from: ourPhone
        })
        console.log(a)
      } catch(e) {
        console.log(e)
      } 
    })

以并发级别并发执行 50k http 请求

使用令人敬畏的蓝鸟糖函数可以很容易地做到这一点.无论如何,twilio 包使用本机承诺.为此,您可以使用带有 mapLimit 方法的异步模块

This is quite easy to do with the awesome bluebird sugar functions. Anyway, the twilio package uses native promise. You can use async module with mapLimit method for this purpose

这篇关于使用 Twilio 发送数千条短信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆