代码之家  ›  专栏  ›  技术社区  ›  Koko

使用Twilio发送数千条短信

  •  0
  • Koko  · 技术社区  · 7 年前

    我想用Twilio发送大约50000条短信,我只是想知道如果我循环通过这样大小的电话号码数组,我的请求是否会崩溃。事实上,Twilio只允许每个请求发送一条消息,所以我必须发送50000条消息。

    有可能这样做吗?还是我必须找到另一种方法?

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

    3 回复  |  直到 7 年前
        1
  •  2
  •   philnash    7 年前

    Twilio开发者布道者。

    API限制

    A short code can send 100 messages per second. .

    We also recommend that you don't send more than 200 messages on any one long code per day.

    无论哪种方式,我都建议使用 messaging service 发送这样的消息。

    最后,您还被限制为100个并发API请求。很高兴看到其他关于按顺序而不是异步发出请求的答案,因为这会消耗服务器上的内存,并且开始查找请求会被Twilio拒绝。

    直通API

    我们现在有了一个API,允许您通过单个API调用发送多条消息。它被称为 passthrough API ,因为它可以将许多数字传递给 Notify service . 您需要将数字转换为“绑定”,并通过Notify服务发送,Notify服务还使用消息服务进行数字池。

    The code looks a bit like this:

    const Twilio = require('twilio');    
    const client = new Twilio(accountSid, authToken);
    const service = client.notify.services('ISXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');
    
    service.notifications
      .create({
        toBinding: [
          JSON.stringify({
            binding_type: 'sms',
            address: '+15555555555',
          }),
          JSON.stringify({
            binding_type: 'facebook-messenger',
            address: '123456789123',
          }),
        ],
        body: 'Hello Bob',
      })
      .then(notification => {
        console.log(notification);
      })
      .catch(error => {
        console.log(error);
      })
    

    在您的情况下,唯一的缺点是每个消息都需要相同,请求的大小需要小于1MB。我们发现,这通常意味着大约10000个数字,因此您可能需要将列表分解为5个API调用。

    如果有任何帮助,请告诉我。

        2
  •  1
  •   radar155    7 年前

    • 你需要考虑 Twilio Api usage Limits.
    • 执行50.000个并行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)
          } 
        })
    

    这是很容易做到的可怕的蓝鸟糖功能。无论如何,twilio包使用原生promise。您可以将异步模块与 mapLimit 为此目的的方法

        3
  •  1
  •   dhilt    7 年前

    function sendSync(index = 0) {
      if(index === phoneNumbers.length) {
        return;
      }
      client.messages.create({
          body: msgCt,
          to: phoneNumbers[index],
          from: ourPhone
      })
      .then(function(msg) {
          console.log(msg.sid);
          sendSync(index + 1);
      })
      .catch(function(err) {
          console.log(err);
      });
    }
    
    sendSync();
    

    或者如果您喜欢异步/等待

    async function sendSync() {
      for (let phNb of phoneNumbers) {
        try {
          let msg = await client.messages.create({
            body: msgCt,
            to: phNb,
            from: ourPhone
          });
          console.log(msg);
        } catch(err) {
          console.log(err);
        } 
      })
    }
    
    sendSync();