Twilio消息未在Cron Lamba上触发

时间:2019-10-04 09:40:55

标签: node.js serverless-framework twilio-api aws-serverless

我已经在这个问题上工作了几个小时,没有成功。我触发以下功能,每15分钟运行一次。从CloudWatch中可以看到该功能正在触发。从我的日志中,我知道该函数已成功获取需要发送通知的约会。我正在使用2个try / catch块尝试获取一些错误消息,但是没有错误记录。

当我使用sls invoke local运行此功能时,它可以正常工作并将期望的文本消息发送到正确的数字。但是,当在基于cron的基础上部署并运行时,它既不起作用,也不会出错-所以我很茫然。

我认为这是一个异步/等待问题。基本上,控制台日志显示在Cloudwatch日志中,但是twilioClient.messages.create函数内部没有任何内容显示在日志中。

任何帮助将不胜感激-我相信这很简单,但是盯着这个看了几个小时却没有成功!

function sendNotifications(appointments) {
  console.log('require notifications', appointments.length);
  appointments.forEach(function(appointment) {
    // Create options to send the message
    // Appointments show in the logs
    console.log('appt', appointment);
    console.log('from', process.env.TWILIO_PHONE_NUMBER);

    try {
      const options = {
        to: `${appointment.meta.contact.number}`,
        from: process.env.TWILIO_PHONE_NUMBER,
        /* eslint-disable max-len */
        body: `Hi ${appointment.meta.contact.name}. Just a reminder that you have an property viewing for ${appointment.meta.property.name} at ${appointment.date}, ${appointment.time}. Please reply with CONFIRM to confirm that you'll be attending this viewing or CANCEL BOOKING to cancel this viewing.`
        /* eslint-enable max-len */
      };

      // Send the message! - this log displays
      console.log('about to send message');
      twilioClient.messages.create(options, function(err, response, callback) {
        // Nothing in this block gets printed to the logs
        if (err) {
          // Just log it for now
          console.log('ERROR', err);
        } else {
          // Log the last few digits of a phone number
          let masked = appointment.meta.contact.number.substr(
            0,
            appointment.meta.contact.number.length - 5
          );
          masked += '*****';
          console.log(`Message sent to ${masked}`);

          try {
            updateBooking({ booking: appointment, message: response });
          } catch (e) {
            console.log(e.message);
          }
        }

        // Don't wait on success/failure, just indicate all messages have been
        // queued for delivery
        if (callback) {
          callback.call();
        }
      });
    } catch (e) {
      console.log('ERR', e.message, appointment);
    }
  });
}

1 个答案:

答案 0 :(得分:0)

来自文档airflow/api/common/experimental/pool.py

twilioClient.messages.create没有回调参数,但返回Promise

尝试使用以下命令执行Lambda:

twilioClient.messages
  .create({
     body: 'just for test',
     from: 'a verified number',
     to: 'a verified number'
   })
  .then(message => console.log(message.sid));

如果这行得通,那就是问题所在。

否则可能是process.env.TWILIO_PHONE_NUMBER设置不正确,或者${appointment.meta.contact.number}可能丢失或无效。也许也打印那些。

也可能没有正确设置Twilio。确保设置了accountSidauthToken