繁体   English   中英

Twilio:使用 Node.js 从特定数字中计算日志中 SMS 消息的数量

[英]Twilio: count number of SMS messages in the log from a specific number using Node.js

我正在尝试在 Node.js 中编写一个 Twilio 服务器端函数( https://console.twilio.com/us1/develop/functions/classic )来计算来自任何发件人的传入消息的数量。

如果数字 = 0,则执行 X。

如果数字 > 0,则做 Y。

显然,下面的代码不起作用,甚至可能会非常糟糕。 产生的错误消息是:"{"message":"username is required","name":"Error","stack":"Error: username is required\\n at new Twilio (/var/task/node_modules/twilio /lib/rest/Twilio.js:141:11)"

任何帮助表示赞赏。 谢谢!

exports.handler = function(context, event, callback) {
    // 
    const accountSid = process.env.TWILIO_ACCOUNT_SID;
    const authToken = process.env.TWILIO_AUTH_TOKEN;
    const client = require('twilio')(accountSid, authToken);
    //
    //const Phone = event.Phone;
    
    client.messages
          .list({
             from: event.Phone
           })
          .then(messages => messages.forEach(m => console.log(m.sid)));
    //
    if(messages.length === 0)
    {
        return callback(null,"Continue");
    } else {
        //
        return callback("Error","Error");
    }
};

Twilio 开发人员布道者在这里。

首先,我可以推荐您使用Twilio Functions Service ,而不是经典的 Functions。

您的错误消息向我表明您的accoundSid为空。 在 Twilio Functions 中,您的 Account Sid 和 Auth Token 被保存为环境变量ACCOUNT_SIDAUTH_TOKEN并且应该在context对象而不是process.env上访问。 为了使事情更容易,您可以使用context.getTwilioClient()创建一个客户端。

一旦你解决了这个问题,还有更多的事情要做,因为对client.messages.list的调用是异步的,所以你不应该在 API 请求完成之前callback

试试这样的:

exports.handler = function (context, event, callback) {
  const client = context.getTwilioClient();

  client.messages
    .list({
      from: event.Phone,
    })
    .then((messages) => {
      messages.forEach((m) => console.log(m.sid));
      if (messages.length === 0) {
        return callback(null, "Continue");
      } else {
        return callback(null, "Error");
      }
    })
    .catch(error => callback(error));
};

来自 Twilio 支持,与 Phil 的回答一致:

exports.handler = function(context, event, callback) {
 // Make sure under the Functions Global Config tab:
 // "Add my Twilio Credentials (ACCOUNT_SID) and (AUTH_TOKEN) to ENV" is CHECKED
 const client = context.getTwilioClient();
 
 const phoneNumber = event.From_Number;

 client.messages
    .list({ from: phoneNumber, limit:20 })
    .then((result) => {
    console.log(result.length);
    return callback(null, result);
 })
    .catch((error) => {
    console.error(error);
    return callback(error, null);
 });
};

我实际使用的答案版本:

exports.handler = function (context, event, callback) {
  //
  const client = context.getTwilioClient();
  //
  client.messages
    .list({
      from: event.Phone,
    })
    .then((messages) => {
      // messages.forEach((m) => console.log(m.sid));
      if (messages.length <= 1) {
        console.log("The number of previous messages is " + messages.length);
        return callback(null, "Continue");
      } else {
        return callback("The number of previous messages is " + messages.length);
      }
    })
    .catch(error => callback(error));
};

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM