[英]Twilio: count number of SMS messages in the log from a specific number using Node.js
I'm trying to write a Twilio server-side function ( https://console.twilio.com/us1/develop/functions/classic ) in Node.js to count the number of incoming messages from any sender.我正在尝试在 Node.js 中编写一个 Twilio 服务器端函数( https://console.twilio.com/us1/develop/functions/classic )来计算来自任何发件人的传入消息的数量。
If the number is = 0, do X.如果数字 = 0,则执行 X。
If the number is > 0, do Y.如果数字 > 0,则做 Y。
Obviously the code below doesn't work, and may even be wildly off.显然,下面的代码不起作用,甚至可能会非常糟糕。 The resulting error message is: "{"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)"
产生的错误消息是:"{"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)"
Any assistance is appreciate.任何帮助表示赞赏。 Thanks!
谢谢!
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 developer evangelist here. Twilio 开发人员布道者在这里。
First up, can I recommend you use a Twilio Functions Service , not classic Functions.首先,我可以推荐您使用Twilio Functions Service ,而不是经典的 Functions。
Your error message suggests to me that your accoundSid
is blank.您的错误消息向我表明您的
accoundSid
为空。 Within Twilio Functions, your Account Sid and Auth Token are saved as the environment variables ACCOUNT_SID
and AUTH_TOKEN
and should be accessed on the context
object rather than process.env
.在 Twilio Functions 中,您的 Account Sid 和 Auth Token 被保存为环境变量
ACCOUNT_SID
和AUTH_TOKEN
并且应该在context
对象而不是process.env
上访问。 To make things easier, you can create a client using context.getTwilioClient()
instead.为了使事情更容易,您可以使用
context.getTwilioClient()
创建一个客户端。
Once you've fixed that, there will still be more to do as the call to client.messages.list
is asynchronous, so you should not callback
until that API request is complete.一旦你解决了这个问题,还有更多的事情要做,因为对
client.messages.list
的调用是异步的,所以你不应该在 API 请求完成之前callback
。
Try something like this instead:试试这样的:
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));
};
From Twilio support, which aligns with Phil's answer:来自 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);
});
};
The version of the answer I'm actually using:我实际使用的答案版本:
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.