簡體   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