NodeJS Twilio没有通过AWS Lambda真实的发送WhatsApp消息

inb24sb2  于 2023-08-04  发布在  Node.js
关注(0)|答案(1)|浏览(162)

很抱歉写了这么长的帖子,但这只是为了更好地解释,因为英语不是我的第一语言。
我使用API Gateway + Lambda通过POST请求发送WhatsApp消息。
我遇到过这种奇怪的行为。看起来Twilio需要在2个请求或一段时间后“唤醒”。我已经输入了从“1000”,“1001”,“1002”,直到“1018”来测试和更好地理解。

  • 当我进行1000和1001的请求时,我在WhatsApp上什么也没有收到。
  • 当我发出请求1002时,我收到了1000消息。所以我总是落后两条信息。
  • 我一直持续到1011,比方说(我收到消息直到1009)。然后我等了大约3分钟或更少。
  • 在请求1012中,我在Postman中得到“内部服务器错误”。(有时我会收到这个错误,有时我会收到一个身体响应,但WhatsApp从未将1012作为消息发送(阅读下一行以更好地理解))。
  • 但我应该得到消息1010(因为我是2消息落后),但什么也没发生。循环重新开始,我从未得到响应1010、1011或1012。我应该等到请求1014才开始接收消息。

会发生什么事?
这是我的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 RESPONSE SHOULD BE HERE)消息是在发送1002 resuest之后创建的。
这是1012请求之后的Lambdla日志。

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的消息,以更好地了解trobuleshoot。

[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


求你了救命告诉我如果你需要更多的信息。
先谢了。

ux6nzvsh

ux6nzvsh1#

我认为这里的问题是,在对Twilio API的异步请求完成之前,您正在从Lambda返回。您最终会收到原始请求,因为AWS似乎会在Lambda完成时暂停事件循环,然后在新的Lambda再次启动时恢复事件循环。
我相信你可以通过在调用Twilio API之前添加await关键字来解决这个问题。

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错误。
这应该解决your previous question too

相关问题