Twilio 可编程 SMS 未在已部署的 Lambda 函数中发送
Twilio programmable SMS not sending in deployed Lambda function
我正在开发 Serverless AWS service that uses Twilio's programmable SMS 来发送短信。
当我在本地 运行 堆栈(例如 sls offline start
)时,我的设置始终成功地传递消息,但在已部署的环境中,我似乎甚至无法调用 Twilio client.
消息传递的设置方式如下:
const twilio = require('twilio');
const twilioClient = twilio(
process.env.TWILIO_SID,
process.env.TWILIO_TOKEN,
{
lazyLoading: true,
}
);
export function sendMessage(user, message) {
twilioClient.messages.create({
from: process.env.TWILIO_NUMBER,
to: user.phone,
body: message,
}, function(err, message) {
console.log('error', err);
console.log('message', message);
});
}
// And then usage in a Serverless Function Handler
function example(event, context, callback) {
context.callbackWaitsForEmptyEventLoop = false;
// user is also determined here
sendMessage(user, 'This is a message');
return {
body: JSON.stringify({}),
statusCode: 200
};
}
在本地,运行这行得通,我能够看到 message
日志的输出,error
日志中没有任何内容。然而,在部署时,运行ning 这不会产生任何结果——该方法似乎甚至没有被调用(我可以在 Twilio 日志中验证没有进行 API 调用),因此没有 error
或 message
日志在回调中生成。
在调试中我尝试了以下方法:
- 我已经记录了所有环境变量(Twilio SSID、身份验证令牌、phone 编号)以及函数参数,它们似乎都已就位。我还检查了 Lambda 函数本身以确保环境变量存在。
- 我检查了我的 CloudWatch 日志;没有错误或异常被记录。除了未调用 Twilio 方法外,Lambda 函数的执行没有问题。
- 我已经尝试记录
twilio
和 twilioClient.messages.create
之类的东西,以确保客户端和函数定义不会以某种方式被删除。
- 我认为这可能与
context.callbackWaitsForEmptyEventLoop
有关,所以我将其从 false
更改为 true
。
我一无所获,我不明白为什么这会在本地工作,但在部署时却不行。
编辑:根据 Twilio client example,如果您省略回调函数,该方法将 return 一个 Promise。我继续尝试等待方法的响应:
export function sendMessage(user, message) {
return twilioClient.messages.create({
from: process.env.TWILIO_NUMBER!,
to: user.phone,
body: message,
});
}
// Usage...
async function example(event, context, callback) {
context.callbackWaitsForEmptyEventLoop = false;
try {
const message = await sendMessage(user, 'This is a message');
console.log('message', message)
} catch (error) {
console.log('error', error);
}
return {
body: JSON.stringify({}),
statusCode: 200
};
}
在此示例中,Lambda 函数成功,但未记录消息和错误。
我试过了,很管用。我试图使我的代码与使用类似,并进行了一些更改。
const twilio = require('twilio');
const twilioClient = twilio(
process.env.TWILIO_SID,
process.env.TWILIO_TOKEN
);
let user = '+14075551212';
function sendMessage(user, message) {
return twilioClient.messages.create({
from: process.env.TWILIO_NUMBER,
to: user,
body: message,
});
}
exports.handler = async function(event, context, callback) {
try {
const message = await sendMessage(user, 'This is a message');
console.log('message', message);
callback(null, {result: 'success'});
} catch (error) {
console.log('error', error);
callback("error");
}
};
我正在开发 Serverless AWS service that uses Twilio's programmable SMS 来发送短信。
当我在本地 运行 堆栈(例如 sls offline start
)时,我的设置始终成功地传递消息,但在已部署的环境中,我似乎甚至无法调用 Twilio client.
消息传递的设置方式如下:
const twilio = require('twilio');
const twilioClient = twilio(
process.env.TWILIO_SID,
process.env.TWILIO_TOKEN,
{
lazyLoading: true,
}
);
export function sendMessage(user, message) {
twilioClient.messages.create({
from: process.env.TWILIO_NUMBER,
to: user.phone,
body: message,
}, function(err, message) {
console.log('error', err);
console.log('message', message);
});
}
// And then usage in a Serverless Function Handler
function example(event, context, callback) {
context.callbackWaitsForEmptyEventLoop = false;
// user is also determined here
sendMessage(user, 'This is a message');
return {
body: JSON.stringify({}),
statusCode: 200
};
}
在本地,运行这行得通,我能够看到 message
日志的输出,error
日志中没有任何内容。然而,在部署时,运行ning 这不会产生任何结果——该方法似乎甚至没有被调用(我可以在 Twilio 日志中验证没有进行 API 调用),因此没有 error
或 message
日志在回调中生成。
在调试中我尝试了以下方法:
- 我已经记录了所有环境变量(Twilio SSID、身份验证令牌、phone 编号)以及函数参数,它们似乎都已就位。我还检查了 Lambda 函数本身以确保环境变量存在。
- 我检查了我的 CloudWatch 日志;没有错误或异常被记录。除了未调用 Twilio 方法外,Lambda 函数的执行没有问题。
- 我已经尝试记录
twilio
和twilioClient.messages.create
之类的东西,以确保客户端和函数定义不会以某种方式被删除。 - 我认为这可能与
context.callbackWaitsForEmptyEventLoop
有关,所以我将其从false
更改为true
。
我一无所获,我不明白为什么这会在本地工作,但在部署时却不行。
编辑:根据 Twilio client example,如果您省略回调函数,该方法将 return 一个 Promise。我继续尝试等待方法的响应:
export function sendMessage(user, message) {
return twilioClient.messages.create({
from: process.env.TWILIO_NUMBER!,
to: user.phone,
body: message,
});
}
// Usage...
async function example(event, context, callback) {
context.callbackWaitsForEmptyEventLoop = false;
try {
const message = await sendMessage(user, 'This is a message');
console.log('message', message)
} catch (error) {
console.log('error', error);
}
return {
body: JSON.stringify({}),
statusCode: 200
};
}
在此示例中,Lambda 函数成功,但未记录消息和错误。
我试过了,很管用。我试图使我的代码与使用类似,并进行了一些更改。
const twilio = require('twilio');
const twilioClient = twilio(
process.env.TWILIO_SID,
process.env.TWILIO_TOKEN
);
let user = '+14075551212';
function sendMessage(user, message) {
return twilioClient.messages.create({
from: process.env.TWILIO_NUMBER,
to: user,
body: message,
});
}
exports.handler = async function(event, context, callback) {
try {
const message = await sendMessage(user, 'This is a message');
console.log('message', message);
callback(null, {result: 'success'});
} catch (error) {
console.log('error', error);
callback("error");
}
};