Twilio 不通过 AWS Lambda 实时发送 WhatsApp 消息

Twilio is not sending WhatsApp messages in real time through AWS Lambda

抱歉这么长post,但这只是为了更好地解释,因为英语不是我的母语。

我正在使用 API 网关 + Lambda 通过 POST 请求发送 WhatsApp 消息。

我遇到过这种奇怪的行为。看起来 Twilio 需要在 2 次请求或一段时间后“唤醒”。 我已经输入了从“1000”、“1001”、“1002”到“1018”的输入来测试和更好地理解。

会发生什么?

这是我的 Lambda 代码

///date is 1000, 1001, 1002, 1003, etc. The numbers I have sent manually to test.
'use strict';
console.log('Trying to send a WhatsApp message...');

exports.handler = async (event) => {
    
const accountSid = 'ACa-------------'; 
const authToken = '7e5--------------'; 

const client = require('twilio')("ACa48----------", "7e5d820---------"); 

 let date = "today";
 if (event.queryStringParameters && event.queryStringParameters.date) {
        console.log("Input received is: " + event.queryStringParameters.date);
        fecha = event.queryStringParameters.date;
    }
client.messages 
      .create({ 
         body: date, 
         from: 'whatsapp:+1---------',       
         to: 'whatsapp:+1---------' 
       }) 
      .then(message => console.log("TWILIO RESPONSE SHOULD BE HERE: " + JSON.stringify(message)))
      .done();

  
    let rspbody = date + " is the input";
    
 let responseBody = {
        message: rspbody,
        input: event
    };
let response = {
        statusCode: 200,
        headers: {
            "x-custom-header" : "my custom header value"
        },
        body: JSON.stringify(responseBody),
        "isBase64Encoded": false
    };
    console.log("response LAMBDA: " + JSON.stringify(response));
    return response;
      
};

这是1012请求(上次连续请求后几分钟后发送的)的相关API网关CloudWatch日志(实际上是与成功请求不同的部分)。所以它永远不会通过 API 网关,所以它不会出现在 Lambda 日志中(我稍后附上)。

2021-12-05T20:15:50.171-05:00   (93f6905d-0346-4eff-8b4c-2b03294c6202) Endpoint response headers: {Date=Mon, 06 Dec 2021 01:15:50 GMT, Content-Type=application/json, Content-Length=148, Connection=keep-alive, x-amzn-RequestId=ece04ac8-267b-42fb-ada0-e54e9b6d36c1, X-Amz-Function-Error=Unhandled, x-amzn-Remapped-Content-Length=0, X-Amz-Executed-Version=$LATEST, X-Amzn-Trace-Id=root=1-61ad6446-24dddd3d3b3f9d6c77fdbe4c;sampled=0}

2021-12-05T20:15:50.171-05:00   (93f6905d-0346-4eff-8b4c-2b03294c6202) Endpoint response body before transformations: {"errorMessage":"RequestId: ece04ac8-267b-42fb-ada0-e54e9b6d36c1 Error: Runtime exited with error: exit status 129","errorType":"Runtime.ExitError"}

2021-12-05T20:15:50.171-05:00   (93f6905d-0346-4eff-8b4c-2b03294c6202) Lambda execution failed with status 200 due to customer function error: RequestId: ece04ac8-267b-42fb-ada0-e54e9b6d36c1 Error: Runtime exited with error: exit status 129. Lambda request id: ece04ac8-267b-42fb-ada0-e54e9b6d36c1

2021-12-05T20:15:50.171-05:00   (93f6905d-0346-4eff-8b4c-2b03294c6202) Method completed with status: 502

2021-12-05T20:15:50.171-05:00   (93f6905d-0346-4eff-8b4c-2b03294c6202) AWS Integration Endpoint RequestId : ece04ac8-267b-42fb-ada0-e54e9b6d36c1

这是相关的 Lambda CloudWatch 日志。

2021-12-05T20:13:07.975-05:00   START RequestId: 184f018a-3ca1-46aa-981e-942b1340446c Version: $LATEST

2021-12-05T20:13:08.044-05:00   2021-12-06T01:13:08.025Z 184f018a-3ca1-46aa-981e-942b1340446c INFO Input received is: 1002

2021-12-05T20:13:08.064-05:00   2021-12-06T01:13:08.064Z 184f018a-3ca1-46aa-981e-942b1340446c INFO response LAMBDA: {"statusCode":200,"headers":{"x-custom-header":"my custom header value"},"body":"{\"message\":\"1002 is the input\",\"input\":{\"resource\":\"/helloworld\",\"path\":\"/helloworld\",\"httpMethod\":\"POST\",\"headers\":{\"Accept\":\"*/*\",\"Accept-Encoding\":\"gzip, deflate, br\",\"Authorization\":\"Bearer 3fa2c3d0-e110-11eb-b383-0b0630a03147\",\"Host\":\"ob9nw3nywj.execute-api.us-east-1.amazonaws.com\",\"Postman-Token\":\"8f8ca586-0b46-4882-a633-653a3a964a2b\",\"User-Agent\":\"PostmanRuntime/7.28.4\",\"X-Amzn-Trace-Id\":\"Root=1-61ad63a3-0782456908e458a6435d1ad5\",\"X-Forwarded-For\":\"190.42.19.71\",\"X-Forwarded-Port\":\"443\",\"X-Forwarded-Proto\":\"https\"},\"multiValueHeaders\":{\"Accept\":[\"*/*\"],\"Accept-Encoding\":[\"gzip, deflate, br\"],\"Authorization\":[\"Bearer 3fa2c3d0-e110-11eb-b383-0b0630a03147\"],\"Host\":[\"ob9nw3nywj.execute-api.us-east-1.amazonaws.com\"],\"Postman-Token\":[\"8f8ca586-0b46-4882-a633-653a3a964a2b\"],\"User-Agent\":[\"PostmanRuntime/7.28.4\"],\"X-Amzn-Trace-Id\":[\"Root=1-61ad63a3-0782456908e458a6435d1ad5\"],\"X-Forwarded-For\":[\"190.42.19.71\"],\"X-Forwarded-Port\":[\"443\"],\"X-Forwarded-Proto\":[\"https\"]},\"queryStringParameters\":{\"fecha\":\"1002\"},\"multiValueQueryStringParameters\":{\"fecha\":[\"1002\"]},\"pathParameters\":null,\"stageVariables\":null,\"requestContext\":{\"resourceId\":\"yiygu3\",\"resourcePath\":\"/helloworld\",\"httpMethod\":\"POST\",\"extendedRequestId\":\"J5yBpHOuIAMF_JA=\",\"requestTime\":\"06/Dec/2021:01:13:07 +0000\",\"path\":\"/test/helloworld\",\"accountId\":\"487474028649\",\"protocol\":\"HTTP/1.1\",\"stage\":\"test\",\"domainPrefix\":\"ob9nw3nywj\",\"requestTimeEpoch\":1638753187961,\"requestId\":\"157d7260-fb50-401d-9cd9-b593ddb8a320\",\"identity\":{\"cognitoIdentityPoolId\":null,\"accountId\":null,\"cognitoIdentityId\":null,\"caller\":null,\"sourceIp\":\"190.42.19.71\",\"principalOrgId\":null,\"accessKey\":null,\"cognitoAuthenticationType\":null,\"cognitoAuthenticationProvider\":null,\"userArn\":null,\"userAgent\":\"PostmanRuntime/7.28.4\",\"user\":null},\"domainName\":\"ob9nw3nywj.execute-api.us-east-1.amazonaws.com\",\"apiId\":\"ob9nw3nywj\"},\"body\":null,\"isBase64Encoded\":false}}","isBase64Encoded":false}

2021-12-05T20:13:08.084-05:00   END RequestId: 184f018a-3ca1-46aa-981e-942b1340446c

2021-12-05T20:13:08.084-05:00   REPORT RequestId: 184f018a-3ca1-46aa-981e-942b1340446c Duration: 105.32 ms Billed Duration: 106 ms Memory Size: 256 MB Max Memory Used: 116 MB

2021-12-05T20:13:09.705-05:00   START RequestId: c4e087b5-a26b-4753-9552-98a835658af5 Version: $LATEST

2021-12-05T20:13:09.745-05:00   2021-12-06T01:13:09.745Z 184f018a-3ca1-46aa-981e-942b1340446c INFO TWILIO RESPONSE SHOULD BE HERE: {"body":"1000","numSegments":"1","direction":"outbound-api","from":"whatsapp:+14155238886","to":"whatsapp:+1-------------","dateUpdated":"2021-12-06T01:13:08.000Z","price":null,"errorMessage":null,"uri":"/2010-04-01/Accounts/ACa48---------174df747a33beec1c3/Messages/SMadc43552455a49de88056d84bda32bd7.json","accountSid":"ACa48-------67ec1c3","numMedia":"0","status":"queued","messagingServiceSid":null,"sid":"SMadc43552455a49de88056d84bda32bd7","dateSent":null,"dateCreated":"2021-12-06T01:13:08.000Z","errorCode":null,"priceUnit":null,"apiVersion":"2010-04-01","subresourceUris":{"media":"/2010-04-01/Accounts/ACa48--------4df747a33beec1c3/Messages/SMadc43552455a49de88056d84bda32bd7/Media.json"}}

2021-12-05T20:13:09.746-05:00   2021-12-06T01:13:09.746Z c4e087b5-a26b-4753-9552-98a835658af5 INFO Input received is: 1003

2021-12-05T20:13:09.765-05:00   2021-12-06T01:13:09.765Z c4e087b5-a26b-4753-9552-98a835658af5 INFO response LAMBDA: {"statusCode":200,"headers":{"x-custom-header":"my custom header value"},"body":"{\"message\":\"1003 is the input\",\"input\":{\"resource\":\"/helloworld\",\"path\":\"/helloworld\",\"httpMethod\":\"POST\",\"headers\":{\"Accept\":\"*/*\",\"Accept-Encoding\":\"gzip, deflate, br\",\"Authorization\":\"Bearer 3fa2c3d0-e110-11eb-b383-0b0630a03147\",\"Host\":\"ob9nw3nywj.execute-api.us-east-1.amazonaws.com\",\"Postman-Token\":\"1ec116a0-e65a-4ecf-8dba-aa9f3ae2d1a0\",\"User-Agent\":\"PostmanRuntime/7.28.4\",\"X-Amzn-Trace-Id\":\"Root=1-61ad63a5-160d7e0c02c8cf7f20f35731\",\"X-Forwarded-For\":\"190.42.19.71\",\"X-Forwarded-Port\":\"443\",\"X-Forwarded-Proto\":\"https\"},\"multiValueHeaders\":{\"Accept\":[\"*/*\"],\"Accept-Encoding\":[\"gzip, deflate, br\"],\"Authorization\":[\"Bearer 3fa2c3d0-e110-11eb-b383-0b0630a03147\"],\"Host\":[\"ob9nw3nywj.execute-api.us-east-1.amazonaws.com\"],\"Postman-Token\":[\"1ec116a0-e65a-4ecf-8dba-aa9f3ae2d1a0\"],\"User-Agent\":[\"PostmanRuntime/7.28.4\"],\"X-Amzn-Trace-Id\":[\"Root=1-61ad63a5-160d7e0c02c8cf7f20f35731\"],\"X-Forwarded-For\":[\"190.42.19.71\"],\"X-Forwarded-Port\":[\"443\"],\"X-Forwarded-Proto\":[\"https\"]},\"queryStringParameters\":{\"fecha\":\"1003\"},\"multiValueQueryStringParameters\":{\"fecha\":[\"1003\"]},\"pathParameters\":null,\"stageVariables\":null,\"requestContext\":{\"resourceId\":\"yiygu3\",\"resourcePath\":\"/helloworld\",\"httpMethod\":\"POST\",\"extendedRequestId\":\"J5yB6EtBoAMF96Q=\",\"requestTime\":\"06/Dec/2021:01:13:09 +0000\",\"path\":\"/test/helloworld\",\"accountId\":\"487474028649\",\"protocol\":\"HTTP/1.1\",\"stage\":\"test\",\"domainPrefix\":\"ob9nw3nywj\",\"requestTimeEpoch\":1638753189693,\"requestId\":\"e2d9d7ec-56d3-4685-8590-aa1d5fb2038b\",\"identity\":{\"cognitoIdentityPoolId\":null,\"accountId\":null,\"cognitoIdentityId\":null,\"caller\":null,\"sourceIp\":\"190.42.19.71\",\"principalOrgId\":null,\"accessKey\":null,\"cognitoAuthenticationType\":null,\"cognitoAuthenticationProvider\":null,\"userArn\":null,\"userAgent\":\"PostmanRuntime/7.28.4\",\"user\":null},\"domainName\":\"ob9nw3nywj.execute-api.us-east-1.amazonaws.com\",\"apiId\":\"ob9nw3nywj\"},\"body\":null,\"isBase64Encoded\":false}}","isBase64Encoded":false}

2021-12-05T20:13:09.766-05:00   END RequestId: c4e087b5-a26b-4753-9552-98a835658af5

2021-12-05T20:13:09.766-05:00   REPORT RequestId: c4e087b5-a26b-4753-9552-98a835658af5 Duration: 59.03 ms Billed Duration: 60 ms Memory Size: 256 MB Max Memory Used: 117 MB

如您所见,“1000”(TWILIO 响应应在此处)消息是在发送 1002 请求后创建的。

这是1012请求后的Lambdla Log。

2021-12-05T20:15:50.118-05:00   START RequestId: ece04ac8-267b-42fb-ada0-e54e9b6d36c1 Version: $LATEST

2021-12-05T20:15:50.121-05:00   2021-12-06T01:15:50.121Z fc58a37e-33bd-47d0-bade-d311d26aad86 ERROR Uncaught Exception {"message":"timeout of 30000ms exceeded","name":"Error","stack":"Error: timeout of 30000ms exceeded\n at createError (/opt/nodejs/node_modules/axios/lib/core/createError.js:16:15)\n at ClientRequest.handleRequestTimeout (/opt/nodejs/node_modules/axios/lib/adapters/http.js:303:16)\n at Object.onceWrapper (events.js:519:28)\n at ClientRequest.emit (events.js:400:28)\n at TLSSocket.emitRequestTimeout (_http_client.js:790:9)\n at Object.onceWrapper (events.js:519:28)\n at TLSSocket.emit (events.js:412:35)\n at TLSSocket.Socket._onTimeout (net.js:495:8)\n at listOnTimeout (internal/timers.js:557:17)\n at processTimers (internal/timers.js:500:7)","config":{"url":"https://api.twilio.com/2010-04-01/Accounts/ACa------4df747a33beec1c3/Messages.json","method":"post","data":"To=whatsapp%3A%2B51-----273&From=whatsapp%3A%2B14155238886&Body=1009","headers":{"Accept":"application/json","Content-Type":"application/x-www-form-urlencoded","User-Agent":"twilio-node/3.71.3 (linux x64) node/v14.18.1","Accept-Charset":"utf-8","Connection":"close","Authorization":"Basic QUNhNDgxOGQ4MmE0ZDY3YTE3NGRmNzQ3YTMzYmVlYzFjMzo3ZTVkODIwNTk2OGFmMTFkNTM5M2NlMzBhNjYzNjk4OQ==","Content-Length":69},"proxy":false,"transformRequest":[null],"transformResponse":[null],"timeout":30000,"xsrfCookieName":"XSRF-TOKEN","xsrfHeaderName":"X-XSRF-TOKEN","maxContentLength":-1,"maxBodyLength":-1,"maxRedirects":0,"transitional":{"silentJSONParsing":true,"forcedJSONParsing":true,"clarifyTimeoutError":false}},"code":"ECONNABORTED"}

2021-12-05T20:15:50.125-05:00   [ERROR] [1638753350125] LAMBDA_RUNTIME Failed to post handler success response. Http response code: 400.

2021-12-05T20:15:50.169-05:00   END RequestId: ece04ac8-267b-42fb-ada0-e54e9b6d36c1

2021-12-05T20:15:50.169-05:00   REPORT RequestId: ece04ac8-267b-42fb-ada0-e54e9b6d36c1 Duration: 47.66 ms Billed Duration: 48 ms Memory Size: 256 MB Max Memory Used: 118 MB

2021-12-05T20:15:50.169-05:00   RequestId: ece04ac8-267b-42fb-ada0-e54e9b6d36c1 Error: Runtime exited with error: exit status 129 Runtime.ExitError

2021-12-05T20:15:50.400-05:00   2021-12-06T01:15:50.400Z undefined INFO Trying to send a WhatsApp message...

2021-12-05T20:15:51.756-05:00   START RequestId: 89f858ae-e5ae-45dc-af37-e911458ac0d0 Version: $LATEST

2021-12-05T20:15:56.829-05:00   2021-12-06T01:15:56.829Z 89f858ae-e5ae-45dc-af37-e911458ac0d0 INFO Input received is: 1013

2021-12-05T20:15:57.007-05:00   2021-12-06T01:15:57.007Z 89f858ae-e5ae-45dc-af37-e911458ac0d0 INFO response LAMBDA: {"statusCode":200,"headers":{"x-custom-header":"my custom header value"},"body":"{\"message\":\"1013 is the input\",\"input\":{\"resource\":\"/helloworld\",\"path\":\"/helloworld\",\"httpMethod\":\"POST\",\"headers\":{\"Accept\":\"*/*\",\"Accept-Encoding\":\"gzip, deflate, br\",\"Authorization\":\"Bearer 3fa2c3d0-e110-11eb-b383-0b0630a03147\",\"Host\":\"ob9nw3nywj.execute-api.us-east-1.amazonaws.com\",\"Postman-Token\":\"2bcec7b9-d27a-4003-b2f3-1c69902877d3\",\"User-Agent\":\"PostmanRuntime/7.28.4\",\"X-Amzn-Trace-Id\":\"Root=1-61ad6447-2f4899486209116835f244bc\",\"X-Forwarded-For\":\"190.42.19.71\",\"X-Forwarded-Port\":\"443\",\"X-Forwarded-Proto\":\"https\"},\"multiValueHeaders\":{\"Accept\":[\"*/*\"],\"Accept-Encoding\":[\"gzip, deflate, br\"],\"Authorization\":[\"Bearer 3fa2c3d0-e110-11eb-b383-0b0630a03147\"],\"Host\":[\"ob9nw3nywj.execute-api.us-east-1.amazonaws.com\"],\"Postman-Token\":[\"2bcec7b9-d27a-4003-b2f3-1c69902877d3\"],\"User-Agent\":[\"PostmanRuntime/7.28.4\"],\"X-Amzn-Trace-Id\":[\"Root=1-61ad6447-2f4899486209116835f244bc\"],\"X-Forwarded-For\":[\"190.42.19.71\"],\"X-Forwarded-Port\":[\"443\"],\"X-Forwarded-Proto\":[\"https\"]},\"queryStringParameters\":{\"fecha\":\"1013\"},\"multiValueQueryStringParameters\":{\"fecha\":[\"1013\"]},\"pathParameters\":null,\"stageVariables\":null,\"requestContext\":{\"resourceId\":\"yiygu3\",\"resourcePath\":\"/helloworld\",\"httpMethod\":\"POST\",\"extendedRequestId\":\"J5ybPH8IoAMFkWQ=\",\"requestTime\":\"06/Dec/2021:01:15:51 +0000\",\"path\":\"/test/helloworld\",\"accountId\":\"487474028649\",\"protocol\":\"HTTP/1.1\",\"stage\":\"test\",\"domainPrefix\":\"ob9nw3nywj\",\"requestTimeEpoch\":1638753351743,\"requestId\":\"32a656fb-4ae3-4b18-8d8b-68e5880562e7\",\"identity\":{\"cognitoIdentityPoolId\":null,\"accountId\":null,\"cognitoIdentityId\":null,\"caller\":null,\"sourceIp\":\"190.42.19.71\",\"principalOrgId\":null,\"accessKey\":null,\"cognitoAuthenticationType\":null,\"cognitoAuthenticationProvider\":null,\"userArn\":null,\"userAgent\":\"PostmanRuntime/7.28.4\",\"user\":null},\"domainName\":\"ob9nw3nywj.execute-api.us-east-1.amazonaws.com\",\"apiId\":\"ob9nw3nywj\"},\"body\":null,\"isBase64Encoded\":false}}","isBase64Encoded":false}

2021-12-05T20:15:57.166-05:00   END RequestId: 89f858ae-e5ae-45dc-af37-e911458ac0d0

2021-12-05T20:15:57.166-05:00   REPORT RequestId: 89f858ae-e5ae-45dc-af37-e911458ac0d0 Duration: 5407.73 ms Billed Duration: 5408 ms Memory Size: 256 MB Max Memory Used: 65 MB

这些是 WhatsApp 消息,可以更好地理解故障排除。

[20:13, 12/5/2021] +1 (415) 523-8886: 1000
[20:13, 12/5/2021] +1 (415) 523-8886: 1001
[20:13, 12/5/2021] +1 (415) 523-8886: 1002
[20:13, 12/5/2021] +1 (415) 523-8886: 1003
[20:13, 12/5/2021] +1 (415) 523-8886: 1004
[20:13, 12/5/2021] +1 (415) 523-8886: 1005
[20:13, 12/5/2021] +1 (415) 523-8886: 1006
[20:13, 12/5/2021] +1 (415) 523-8886: 1007
[20:13, 12/5/2021] +1 (415) 523-8886: 1008
[20:13, 12/5/2021] +1 (415) 523-8886: 1009
[20:15, 12/5/2021] +1 (415) 523-8886: 1013
[20:16, 12/5/2021] +1 (415) 523-8886: 1014
[20:16, 12/5/2021] +1 (415) 523-8886: 1015

请帮忙。如果您需要更多信息,请告诉我。

提前致谢。

此处为 Twilio 开发人员布道师。

我认为这里的问题是您在对 Twilio API 的异步请求完成之前从 Lambda 返回。您最终会收到原始请求,因为 AWS 似乎会在 Lambda 完成时暂停事件循环,然后在新的 Lambda 再次启动时恢复它。

我相信你可以通过在调用 Twilio API 之前添加 await 关键字来解决这个问题 API。

  await client.messages
    .create({
      body: date,
      from: "whatsapp:+1---------",
      to: "whatsapp:+1---------",
    })
    .then((message) =>
      console.log("TWILIO RESPONSE SHOULD BE HERE: " + JSON.stringify(message))
    )

您也应该在 API 调用周围添加一个 try/catch,这样您就可以处理 API 错误。

这应该可以解决