【问题标题】:Twilio Functions Error 20429 - Too many requests multiple sms messagesTwilio 函数错误 20429 - 请求多条短信
【发布时间】:2019-07-14 19:55:48
【问题描述】:

我正在使用 Twilio 功能和可编程 SMS 将 SMS 消息发送到我的 iOS 应用程序中的号码列表。列表中只有 100 多个手机号码(出现此错误时为 113 个)。大多数这些消息都会发送,但随后该函数说它在 502 毫秒后超时。

我正在使用来自 Twilio 的示例代码发送到群组消息(我已在下面复制)并从我的 iOS 应用发出 URLSession 请求。

有没有办法可以解决这个问题,以便我可以发送到这个相当大的电话号码列表或使功能运行更长时间?

非常感谢您的帮助。 汤姆

请求:

let messagesPhoneNumberString = [+447987654321abc,+447123789456def,+447123456789ghi]
"https://myfunction.twil.io/send-messages?phoneAndID=\(messagesPhoneNumberString)&globalID=\(current globalID)"

我的 Twilio 功能代码:

exports.handler = function(context, event, callback) {

    let phoneAndIDString = event['phoneAndID'];
    let globalID String = event['globalID'];
    let numbersArray = phoneAndIDString.split(",");

Promise.all(
  numbersArray(number => {

      let phoneNumberSplit = "+" + number.slice(1, 13);
      let idSplit = number.slice(13);

      console.log('Send to number: ' + phoneNumberSplit + ' - with ID: ' + idSplit);

    return context.getTwilioClient().messages.create({
      to: phoneNumberSplit,
      from: 'Example',
      body: 'Hello World: ' + idSplit
    });
  })
)
  .then(messages => {
    console.log('Messages sent!');
    callback(null, response);
  })
  .catch(err => console.error(err));
    };

【问题讨论】:

    标签: ios swift sms twilio twilio-functions


    【解决方案1】:

    这里是 Twilio 开发者宣传员。

    Twilio Functions has a timeout of 5 seconds,因此使用 Twilio 函数一次性发送这么多消息可能不是最好的主意。

    不过你有一些选择。

    如果您向所有这些号码发送相同的消息,那么您可以使用 Twilio Notify 直通 API。查看这篇博文中关于 sending mass messages with Node.js 的详细信息。

    否则,如果您必须发送不同的消息,那么您可以将数字分成批次并多次使用相同的功能。

    最后,您可以使用不同的平台来发送没有 5 秒限制的消息。

    【讨论】:

    • 您好,我尝试发送的消息对于每个号码都略有不同。如果我使用分批的方法,是否需要等待一定时间才能调用该函数两次,还是可以一个接一个地发出请求?谢谢
    • 我刚刚尝试了“将数字分成批次并多次使用相同功能”的建议,成功率较高,但仍然遇到某些请求的超时消息(不是全部) 一次有 15 个数字的批次。有什么建议么?一次发送一个号码和消息并发出 113 次请求(每个电话号码 1 次)会更好吗?谢谢
    • 是的,每个函数一个是成功的最高机会。 Alex 的回答可能值得一试。
    【解决方案2】:

    除了 Phil 的回答中提供的选项之外,您还可以使用递归。

    您可以像现在一样从您的应用程序触发该过程并在初始函数调用中传递所有数字。

    然后,想法是每次函数调用只发送一条消息,并让 Twilio 函数在收到来自 .create() 的响应后调用自身。这意味着没有并发调用来发送消息,消息是一个接一个地发送,尽管接收它们的顺序不一定是查询字符串中传递数字的顺序。

    您需要在函数依赖配置中添加axios (https://www.twilio.com/console/runtime/functions/configure)。

    Axios 用于从函数内部向函数发出 HTTP 请求。

    每个函数都会运行,测试当电话号码查询字符串长度为零时发生的停止条件。然后,使用 .shift() 从 numbers 数组中删除第一个元素以使用它。剩余的数组被传递给下一个函数调用。

    这是我尝试过的代码,它对我有用,但是您必须为 +44更改(.slice() 方法上的 11 长度) > 因为我测试过长度较短的美国号码 +1


    
    exports.handler = function(context, event, callback) {
      const axios = require("axios");
    
      let phoneAndIDString = event["phoneAndID"].trim();
      console.log(phoneAndIDString);
    
      let globalIDString = event["globalID"].trim();
    
      // stop condition for recursive call
      if (phoneAndIDString.length === 0) {
        return callback(null, true);
      }
    
      let numbersArray = phoneAndIDString.split(",");
      console.log(numbersArray);
    
      // take the first item of array
      let number = numbersArray.shift();
      console.log(number);
    
      // the remaining array will be passed to the next function call
      phoneAndIDString = numbersArray.join();
      console.log(phoneAndIDString);
    
      let phoneNumberSplit = "+" + number.slice(0, 11);
      let idSplit = number.slice(11);
      console.log("Send to number: " + phoneNumberSplit + " - with ID: " + idSplit);
    
      context
        .getTwilioClient()
        .messages.create({
          to: phoneNumberSplit,
          from: "+17775553333",
          body: "Hello World: " + idSplit
        })
        .then(msg => {
          console.log("Message sent: " + msg.sid);
          axios
            .get(
              "https://foo-bar-1234.twil.io/send-messages?globalID=baz&phoneAndID=" +
                phoneAndIDString
            )
            .then(function(response) {
              // handle success
              console.log(response.status);
              return callback(null, true);
            })
            .catch(function(err) {
              // handle error
              console.error(err);
            });
        })
        .catch(function(err) {
          console.error(err);
        });
    };
    


    尝试在测试时逐步使用它,控制台记录事物,然后在从上到下使用return callback(null, true) 尽早返回,以确保不会陷入循环。

    【讨论】:

      猜你喜欢
      • 2022-01-01
      • 1970-01-01
      • 2014-08-13
      • 2022-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多