提问人:Tails 提问时间:11/11/2023 更新时间:11/11/2023 访问量:38
获取 无法使用通过 SAM 部署的 AWS Lambda 将 undefined 或 null 转换为 @slack/bolt 上的对象
Getting Cannot convert undefined or null to object on @slack/bolt using AWS Lambda deployed with SAM
问:
我正在尝试使用 AWS Lambda 上的 @slack/bolt node.js 库构建 Slack Bot 我按照 Slack 文档上的说明进行操作,但我在使用 AWSLambdaReceiver 时遇到了问题(我猜)
我使用带有 Hello-World 模板的 SAM 创建了项目
这是我的app.mjs
import pkg from '@slack/bolt';
const { App, AwsLambdaReceiver } = pkg;
const awsLambdaReceiver = new AwsLambdaReceiver({
signingSecret: MY_SIGNING_SECRET
});
const app = new App({
token: MY_TOKEN,
receiver: awsLambdaReceiver,
});
// Listens to incoming messages that contain "hello"
app.message('hello', async ({ message, say }) => {
// say() sends a message to the channel where the event was triggered
await say({
blocks: [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": `Hey there <@${message.user}>!`
},
"accessory": {
"type": "button",
"text": {
"type": "plain_text",
"text": "Click Me"
},
"action_id": "button_click"
}
}
],
text: `Hey there <@${message.user}>!`
});
});
app.action('button_click', async ({ body, ack, say }) => {
// Acknowledge the action
await ack();
await say(`<@${body.user.id}> clicked the button`);
});
export const lambdaHandler = async (event, context, callback) => {
const handler = await awsLambdaReceiver.start();
return handler(event, context, callback);
}
这是我的文件template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
slack-bot-lambda
Sample SAM Template for slack-bot-lambda
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
Function:
Timeout: 3
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: hello-world/
Handler: app.lambdaHandler
Runtime: nodejs18.x
Architectures:
- x86_64
Events:
HelloWorld:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /hello/slack/events
Method: post
Outputs:
# ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
# Find out more about other implicit resources you can reference within SAM
# https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
HelloWorldApi:
Description: "API Gateway endpoint URL for Prod stage for Hello World function"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
HelloWorldFunction:
Description: "Hello World Lambda Function ARN"
Value: !GetAtt HelloWorldFunction.Arn
HelloWorldFunctionIamRole:
Description: "Implicit IAM Role created for Hello World function"
Value: !GetAtt HelloWorldFunctionRole.Ar
当我尝试运行时,我在 AWS Lambda 上遇到的错误是:
{
"errorType": "TypeError",
"errorMessage": "Wrong arguments",
"trace": [
"TypeError: Wrong arguments",
" at RAPIDClient.postInvocationResponse (file:///var/runtime/index.mjs:434:27)",
" at complete (file:///var/runtime/index.mjs:811:16)",
" at done (file:///var/runtime/index.mjs:835:11)",
" at succeed (file:///var/runtime/index.mjs:839:9)",
" at file:///var/runtime/index.mjs:872:20"
]
}
和
{"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"TypeError: Cannot convert undefined or null to object","reason":{"errorType":"TypeError","errorMessage":"Cannot convert undefined or null to object","stack":["TypeError: Cannot convert undefined or null to object"," at Function.keys (<anonymous>)"," at AwsLambdaReceiver.getHeaderValue (/var/task/node_modules/@slack/bolt/dist/receivers/AwsLambdaReceiver.js:186:43)"
我假设错误在上,但我不知道我能做多少。return handler(event, context, callback)
感谢您的帮助。谢谢!
我尝试检查是否有任何处理程序参数为 null,但显然它们都不是 null
答: 暂无答案
评论