question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JS Bolt: Receiving multiple requests from a single app_mention event on AWS Lambda

See original GitHub issue

Description

Hi! So my application is listening for an app_mention event, which it then passes on to a function that sends a message in the channel as a response. However, when I send a message to my bot, I usually receive more than one response from it in the chat. I have read that this can be caused by timeouts and retries, but it looks as if (according to my logs) the handling of the event is happening in well under 3 seconds. Any ideas? Do I need to manually return a 200 OK with the bolt framework?

Thank you!!

What type of issue is this? (place an x in one of the [ ])

  • bug
  • enhancement (feature request)
  • question
  • documentation related
  • example code related
  • testing related
  • discussion

Requirements (place an x in each of the [ ])

  • I’ve read and understood the Contributing guidelines and have done my best effort to follow them.
  • I’ve read and agree to the Code of Conduct.
  • I’ve searched for any related issues and avoided creating a duplicate issue.

Bug Report

Filling out the following details about bugs will help us solve your issue sooner.

Reproducible in:

package version:

node version:

OS version(s):

Steps to reproduce:

Expected result:

A single response

Actual result:

Multiple responses

Attachments:

Here is a picture of the issue (Note that the bot responses arrived within >1 second of one another):

Screen Shot 2021-05-11 at 4 59 56 PM

Here are the logs:

START RequestId: 78572cf9-b2b8-457f-a167-c64feead09f8 Version: $LATEST
--
2021-05-11T21:46:25.356Z	78572cf9-b2b8-457f-a167-c64feead09f8	INFO	event { resource: '/slack/events',  path: '/slack/events',  httpMethod: 'POST',  headers:   { Accept: '*/*',     'Accept-Encoding': 'gzip,deflate',     'CloudFront-Forwarded-Proto': 'https',     'CloudFront-Is-Desktop-Viewer': 'true',     'CloudFront-Is-Mobile-Viewer': 'false',     'CloudFront-Is-SmartTV-Viewer': 'false',     'CloudFront-Is-Tablet-Viewer': 'false',     'CloudFront-Viewer-Country': 'US',     'Content-Type': 'application/json',     Host: 'p5kspcf2i4.execute-api.us-east-1.amazonaws.com',     'User-Agent': 'Slackbot 1.0 (+https://api.slack.com/robots)',     Via:      '1.1 5d9ef69d47a63c80a41bbcf6902f6312.cloudfront.net (CloudFront)',     'X-Amz-Cf-Id': 'LSHwIlEtnu8XRsiN04J1mFMvfClIUdhJaCLdcONxhYb2o84J2yYDrA==',     'X-Amzn-Trace-Id': 'Root=1-609afb30-17f8de1675b8f91038856b4e',     'X-Forwarded-For': '3.230.154.183, 70.132.32.69',     'X-Forwarded-Port': '443',     'X-Forwarded-Proto': 'https',     'X-Slack-Request-Timestamp': '1620769584',     'X-Slack-Signature':      'v0=5844681c76930b3a7a208b3e74a6910867a9bcb0e52f97bd9c7c8e4c0cada5c1' },  multiValueHeaders:   { Accept: [ '*/*' ],     'Accept-Encoding': [ 'gzip,deflate' ],     'CloudFront-Forwarded-Proto': [ 'https' ],     'CloudFront-Is-Desktop-Viewer': [ 'true' ],     'CloudFront-Is-Mobile-Viewer': [ 'false' ],     'CloudFront-Is-SmartTV-Viewer': [ 'false' ],     'CloudFront-Is-Tablet-Viewer': [ 'false' ],     'CloudFront-Viewer-Country': [ 'US' ],     'Content-Type': [ 'application/json' ],     Host: [ 'p5kspcf2i4.execute-api.us-east-1.amazonaws.com' ],     'User-Agent': [ 'Slackbot 1.0 (+https://api.slack.com/robots)' ],     Via:      [ '1.1 5d9ef69d47a63c80a41bbcf6902f6312.cloudfront.net (CloudFront)' ],     'X-Amz-Cf-Id':      [ 'LSHwIlEtnu8XRsiN04J1mFMvfClIUdhJaCLdcONxhYb2o84J2yYDrA==' ],     'X-Amzn-Trace-Id': [ 'Root=1-609afb30-17f8de1675b8f91038856b4e' ],     'X-Forwarded-For': [ '3.230.154.183, 70.132.32.69' ],     'X-Forwarded-Port': [ '443' ],     'X-Forwarded-Proto': [ 'https' ],     'X-Slack-Request-Timestamp': [ '1620769584' ],     'X-Slack-Signature':      [ 'v0=5844681c76930b3a7a208b3e74a6910867a9bcb0e52f97bd9c7c8e4c0cada5c1' ] },  queryStringParameters: null,  multiValueQueryStringParameters: null,  pathParameters: null,  stageVariables: null,  requestContext:   { resourceId: 'tbpie9',     resourcePath: '/slack/events',     httpMethod: 'POST',     extendedRequestId: 'fLwvjH8ToAMFQfw=',     requestTime: '11/May/2021:21:46:24 +0000',     path: '/dev/slack/events',     accountId: '777509510706',     protocol: 'HTTP/1.1',     stage: 'dev',     domainPrefix: 'p5kspcf2i4',     requestTimeEpoch: 1620769584218,     requestId: 'e5f91597-3c3e-47d6-b582-7b85d039637f',     identity:      { cognitoIdentityPoolId: null,        accountId: null,        cognitoIdentityId: null,        caller: null,        sourceIp: '3.230.154.183',        principalOrgId: null,        accessKey: null,        cognitoAuthenticationType: null,        cognitoAuthenticationProvider: null,        userArn: null,        userAgent: 'Slackbot 1.0 (+https://api.slack.com/robots)',        user: null },     domainName: 'p5kspcf2i4.execute-api.us-east-1.amazonaws.com',     apiId: 'p5kspcf2i4' },  body:   '{"token":"BVfjr7IiFj2CpDbCyIlivx9o","team_id":"T01RMJFCGTV","api_app_id":"A01RMCE7UGJ","event":{"client_msg_id":"e8856ee1-cfac-4f15-a7e7-6d29446f986c","type":"app_mention","text":"<@U01RJA6K3UM> schedules pause workflow","user":"U01SB6CHSTA","ts":"1620769583.007100","team":"T01RMJFCGTV","blocks":[{"type":"rich_text","block_id":"JEtp","elements":[{"type":"rich_text_section","elements":[{"type":"user","user_id":"U01RJA6K3UM"},{"type":"text","text":" schedules pause workflow"}]}]}],"channel":"C021LEAQNV8","event_ts":"1620769583.007100"},"type":"event_callback","event_id":"Ev02166LDHSB","event_time":1620769583,"authorizations":[{"enterprise_id":null,"team_id":"T01RMJFCGTV","user_id":"U01RJA6K3UM","is_bot":true,"is_enterprise_install":false}],"is_ext_shared_channel":false,"event_context":"2-app_mention-T01RMJFCGTV-A01RMCE7UGJ-C021LEAQNV8"}',  isBase64Encoded: false }
2021-05-11T21:46:26.117Z	78572cf9-b2b8-457f-a167-c64feead09f8	DEBUG	[DEBUG]  web-api:WebClient:0 initialized
2021-05-11T21:46:26.118Z	78572cf9-b2b8-457f-a167-c64feead09f8	DEBUG	[DEBUG]  web-api:WebClient:0 apiCall('auth.test') start
2021-05-11T21:46:26.176Z	78572cf9-b2b8-457f-a167-c64feead09f8	DEBUG	[DEBUG]  web-api:WebClient:0 will perform http request
2021-05-11T21:46:26.177Z	78572cf9-b2b8-457f-a167-c64feead09f8	DEBUG	[DEBUG]  web-api:WebClient:0 apiCall('auth.test') start
2021-05-11T21:46:26.177Z	78572cf9-b2b8-457f-a167-c64feead09f8	DEBUG	[DEBUG]  web-api:WebClient:0 will perform http request
2021-05-11T21:46:26.456Z	78572cf9-b2b8-457f-a167-c64feead09f8	DEBUG	[DEBUG]  web-api:WebClient:0 http response received
2021-05-11T21:46:26.457Z	78572cf9-b2b8-457f-a167-c64feead09f8	DEBUG	[DEBUG]  web-api:WebClient:1 initialized
2021-05-11T21:46:26.458Z	78572cf9-b2b8-457f-a167-c64feead09f8	DEBUG	[DEBUG]  bolt-app Conversation context failed loading for ID: C021LEAQNV8, error: Conversation not found
2021-05-11T21:46:26.536Z	78572cf9-b2b8-457f-a167-c64feead09f8	DEBUG	[DEBUG]  web-api:WebClient:0 http response received
2021-05-11T21:46:26.636Z	78572cf9-b2b8-457f-a167-c64feead09f8	INFO	inside user auth before context
2021-05-11T21:46:26.636Z	78572cf9-b2b8-457f-a167-c64feead09f8	INFO	inside app mention
2021-05-11T21:46:26.637Z	78572cf9-b2b8-457f-a167-c64feead09f8	INFO	inside pause workflow
2021-05-11T21:46:27.017Z	78572cf9-b2b8-457f-a167-c64feead09f8	DEBUG	[DEBUG]  web-api:WebClient:1 apiCall('chat.postEphemeral') start
2021-05-11T21:46:27.018Z	78572cf9-b2b8-457f-a167-c64feead09f8	DEBUG	[DEBUG]  web-api:WebClient:1 will perform http request
2021-05-11T21:46:27.208Z	78572cf9-b2b8-457f-a167-c64feead09f8	DEBUG	[DEBUG]  web-api:WebClient:1 http response received
2021-05-11T21:46:27.208Z	78572cf9-b2b8-457f-a167-c64feead09f8	INFO	finished with everything
2021-05-11T21:46:27.208Z	78572cf9-b2b8-457f-a167-c64feead09f8	INFO	app mention is finished
END RequestId: 78572cf9-b2b8-457f-a167-c64feead09f8
REPORT RequestId: 78572cf9-b2b8-457f-a167-c64feead09f8	Duration: 1915.52 ms	Billed Duration: 1916 ms	Memory Size: 128 MB	Max Memory Used: 105 MB	Init Duration: 743.28 ms
START RequestId: e6c2edba-3230-4eef-8fb9-47b2edeb56e4 Version: $LATEST
2021-05-11T21:46:27.324Z	e6c2edba-3230-4eef-8fb9-47b2edeb56e4	INFO	event { resource: '/slack/events',  path: '/slack/events',  httpMethod: 'POST',  headers:   { Accept: '*/*',     'Accept-Encoding': 'gzip,deflate',     'CloudFront-Forwarded-Proto': 'https',     'CloudFront-Is-Desktop-Viewer': 'true',     'CloudFront-Is-Mobile-Viewer': 'false',     'CloudFront-Is-SmartTV-Viewer': 'false',     'CloudFront-Is-Tablet-Viewer': 'false',     'CloudFront-Viewer-Country': 'US',     'Content-Type': 'application/json',     Host: 'p5kspcf2i4.execute-api.us-east-1.amazonaws.com',     'User-Agent': 'Slackbot 1.0 (+https://api.slack.com/robots)',     Via:      '1.1 f92491812e422470607f365e923929b5.cloudfront.net (CloudFront)',     'X-Amz-Cf-Id': 'JQuHCnQbetL5eUIp3nIaMEKudWURC5ucZj8vZuXoYuYVlyeGGABIxA==',     'X-Amzn-Trace-Id': 'Root=1-609afb33-6bf02ee521f42a765f53eab7',     'X-Forwarded-For': '54.196.88.72, 70.132.32.150',     'X-Forwarded-Port': '443',     'X-Forwarded-Proto': 'https',     'X-Slack-Request-Timestamp': '1620769587',     'X-Slack-Retry-Num': '1',     'X-Slack-Retry-Reason': 'http_timeout',     'X-Slack-Signature':      'v0=7bf9d3438f13308fad873a1695f9b1ec2767909f30ca56e1d0becf33acb9b6ab' },  multiValueHeaders:   { Accept: [ '*/*' ],     'Accept-Encoding': [ 'gzip,deflate' ],     'CloudFront-Forwarded-Proto': [ 'https' ],     'CloudFront-Is-Desktop-Viewer': [ 'true' ],     'CloudFront-Is-Mobile-Viewer': [ 'false' ],     'CloudFront-Is-SmartTV-Viewer': [ 'false' ],     'CloudFront-Is-Tablet-Viewer': [ 'false' ],     'CloudFront-Viewer-Country': [ 'US' ],     'Content-Type': [ 'application/json' ],     Host: [ 'p5kspcf2i4.execute-api.us-east-1.amazonaws.com' ],     'User-Agent': [ 'Slackbot 1.0 (+https://api.slack.com/robots)' ],     Via:      [ '1.1 f92491812e422470607f365e923929b5.cloudfront.net (CloudFront)' ],     'X-Amz-Cf-Id':      [ 'JQuHCnQbetL5eUIp3nIaMEKudWURC5ucZj8vZuXoYuYVlyeGGABIxA==' ],     'X-Amzn-Trace-Id': [ 'Root=1-609afb33-6bf02ee521f42a765f53eab7' ],     'X-Forwarded-For': [ '54.196.88.72, 70.132.32.150' ],     'X-Forwarded-Port': [ '443' ],     'X-Forwarded-Proto': [ 'https' ],     'X-Slack-Request-Timestamp': [ '1620769587' ],     'X-Slack-Retry-Num': [ '1' ],     'X-Slack-Retry-Reason': [ 'http_timeout' ],     'X-Slack-Signature':      [ 'v0=7bf9d3438f13308fad873a1695f9b1ec2767909f30ca56e1d0becf33acb9b6ab' ] },  queryStringParameters: null,  multiValueQueryStringParameters: null,  pathParameters: null,  stageVariables: null,  requestContext:   { resourceId: 'tbpie9',     resourcePath: '/slack/events',     httpMethod: 'POST',     extendedRequestId: 'fLwwCF40oAMFpbQ=',     requestTime: '11/May/2021:21:46:27 +0000',     path: '/dev/slack/events',     accountId: '777509510706',     protocol: 'HTTP/1.1',     stage: 'dev',     domainPrefix: 'p5kspcf2i4',     requestTimeEpoch: 1620769587313,     requestId: 'c3ca4c54-05f8-42ce-b433-3be44b6fa201',     identity:      { cognitoIdentityPoolId: null,        accountId: null,        cognitoIdentityId: null,        caller: null,        sourceIp: '54.196.88.72',        principalOrgId: null,        accessKey: null,        cognitoAuthenticationType: null,        cognitoAuthenticationProvider: null,        userArn: null,        userAgent: 'Slackbot 1.0 (+https://api.slack.com/robots)',        user: null },     domainName: 'p5kspcf2i4.execute-api.us-east-1.amazonaws.com',     apiId: 'p5kspcf2i4' },  body:   '{"token":"BVfjr7IiFj2CpDbCyIlivx9o","team_id":"T01RMJFCGTV","api_app_id":"A01RMCE7UGJ","event":{"client_msg_id":"e8856ee1-cfac-4f15-a7e7-6d29446f986c","type":"app_mention","text":"<@U01RJA6K3UM> schedules pause workflow","user":"U01SB6CHSTA","ts":"1620769583.007100","team":"T01RMJFCGTV","blocks":[{"type":"rich_text","block_id":"JEtp","elements":[{"type":"rich_text_section","elements":[{"type":"user","user_id":"U01RJA6K3UM"},{"type":"text","text":" schedules pause workflow"}]}]}],"channel":"C021LEAQNV8","event_ts":"1620769583.007100"},"type":"event_callback","event_id":"Ev02166LDHSB","event_time":1620769583,"authorizations":[{"enterprise_id":null,"team_id":"T01RMJFCGTV","user_id":"U01RJA6K3UM","is_bot":true,"is_enterprise_install":false}],"is_ext_shared_channel":false,"event_context":"2-app_mention-T01RMJFCGTV-A01RMCE7UGJ-C021LEAQNV8"}',  isBase64Encoded: false }
2021-05-11T21:46:27.377Z	e6c2edba-3230-4eef-8fb9-47b2edeb56e4	DEBUG	[DEBUG]  web-api:WebClient:2 initialized
2021-05-11T21:46:27.377Z	e6c2edba-3230-4eef-8fb9-47b2edeb56e4	DEBUG	[DEBUG]  web-api:WebClient:2 apiCall('auth.test') start
2021-05-11T21:46:27.377Z	e6c2edba-3230-4eef-8fb9-47b2edeb56e4	DEBUG	[DEBUG]  web-api:WebClient:2 will perform http request
2021-05-11T21:46:27.378Z	e6c2edba-3230-4eef-8fb9-47b2edeb56e4	DEBUG	[DEBUG]  web-api:WebClient:2 apiCall('auth.test') start
2021-05-11T21:46:27.394Z	e6c2edba-3230-4eef-8fb9-47b2edeb56e4	DEBUG	[DEBUG]  web-api:WebClient:2 will perform http request
2021-05-11T21:46:27.495Z	e6c2edba-3230-4eef-8fb9-47b2edeb56e4	DEBUG	[DEBUG]  web-api:WebClient:2 http response received
2021-05-11T21:46:27.515Z	e6c2edba-3230-4eef-8fb9-47b2edeb56e4	DEBUG	[DEBUG]  web-api:WebClient:2 http response received
2021-05-11T21:46:27.515Z	e6c2edba-3230-4eef-8fb9-47b2edeb56e4	DEBUG	[DEBUG]  web-api:WebClient:3 initialized
2021-05-11T21:46:27.515Z	e6c2edba-3230-4eef-8fb9-47b2edeb56e4	DEBUG	[DEBUG]  bolt-app Conversation context failed loading for ID: C021LEAQNV8, error: Conversation not found
2021-05-11T21:46:27.535Z	e6c2edba-3230-4eef-8fb9-47b2edeb56e4	INFO	inside user auth before context
2021-05-11T21:46:27.535Z	e6c2edba-3230-4eef-8fb9-47b2edeb56e4	INFO	inside app mention
2021-05-11T21:46:27.535Z	e6c2edba-3230-4eef-8fb9-47b2edeb56e4	INFO	inside pause workflow
2021-05-11T21:46:27.635Z	e6c2edba-3230-4eef-8fb9-47b2edeb56e4	DEBUG	[DEBUG]  web-api:WebClient:3 apiCall('chat.postEphemeral') start
2021-05-11T21:46:27.635Z	e6c2edba-3230-4eef-8fb9-47b2edeb56e4	DEBUG	[DEBUG]  web-api:WebClient:3 will perform http request
2021-05-11T21:46:27.778Z	e6c2edba-3230-4eef-8fb9-47b2edeb56e4	DEBUG	[DEBUG]  web-api:WebClient:3 http response received
2021-05-11T21:46:27.778Z	e6c2edba-3230-4eef-8fb9-47b2edeb56e4	INFO	finished with everything
2021-05-11T21:46:27.778Z	e6c2edba-3230-4eef-8fb9-47b2edeb56e4	INFO	app mention is finished
END RequestId: e6c2edba-3230-4eef-8fb9-47b2edeb56e4
REPORT RequestId: e6c2edba-3230-4eef-8fb9-47b2edeb56e4	Duration: 474.76 ms	Billed Duration: 475 ms	Memory Size: 128 MB	Max Memory Used: 106 MB

And here is the src (some info removed):


// Handle the Lambda function event
module.exports.handler = async (event) => {
 

  // Initialize your custom receiver
  const expressReceiver = new ExpressReceiver({
    signingSecret: secrets['SLACK_SIGNING_SECRET'],
    // The `processBeforeResponse` option is required for all FaaS environments.
    // It allows Bolt methods (e.g. `app.message`) to handle a Slack request
    // before the Bolt framework responds to the request (e.g. `ack()`). This is
    // important because FaaS immediately terminate handlers after the response.
    processBeforeResponse: true
  });

  // Initializes your app with your bot token and the AWS Lambda ready receiver
  const app = new App({
    processBeforeResponse: true,
    token: secrets['SLACK_BOT_TOKEN'],
    receiver: expressReceiver,
    logLevel: LogLevel.DEBUG,
  });


  app.event('app_mention', async ({ event, client, context , body}) => {
    console.log("inside app mention")
    let command = event.text.split(" ")

    if (command.length === 1){
      await handlers.invalid.invalid_handler({ event, client, context, body })
    } else if (command[1] in topic_event_handlers) {
      let operation = command[1]
      await topic_event_handlers[operation]({ event, client, context, command, body })
    } else {
      await handlers.invalid.invalid_handler({ event, client, context, body})
    }

    console.log("app mention is finished")

  });

  




 return serverlessExpress({
    app: expressReceiver.app
  })(event)

};


Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
seratchcommented, Jun 28, 2021

Is this already supported in Bolt JS? Or is this a recommendation that is not implemented.

Sorry, my comment there might be a bit confusing. I proposed the way in the issue but it’s not yet implemented.

I’m hoping you can recommend a solution to support these long-running requests.

Only the recommendation at the moment is to do something like this in your own code. Another way is to use a queue like SQS to start time-consuming operation as a separate AWS Lambda function.

app.command("/hello", ({ body, ack }) => {
  const lambda = new AWS.Lambda();
  const params: AWS.Lambda.InvocationRequest = {
    InvocationType: 'Event', // async invocation
    FunctionName: functionName,
    Payload: JSON.stringify(body)
  };
  const lambdaInvocation = await lambda.invoke(params).promise();
  // check the lambdaInvocation here
  ack();
});

I know this is not an ideal solution for you but bolt-js won’t come up with a new solution for AWS Lambda support at least in the short term.

1reaction
seratchcommented, Jun 28, 2021

@AdamInTheOculus Yes, this is feasible. I’ve created something similar before: https://github.com/seratch/deepl-for-slack#shortcut-to-run-deepl-translate-api

This example app displays loading modal view when acknowledging a view_submission request. And then, the app calls views.update API to show the translation result once it’s ready. The app does not run as-is on AWS Lambda but if you move this part to another Lambda function, it behaves as you mentioned.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I prevent duplicate Lambda function invocations? - AWS
My AWS Lambda function keeps receiving more than one invocation request for a single event. How do I prevent my Lambda function from ......
Read more >
Deploying to AWS Lambda - Slack | Bolt for JavaScript
This guide walks you through preparing and deploying a Slack app using Bolt for JavaScript, the Serverless Framework, and AWS Lambda. When you're...
Read more >
Responding to app mentions - Slack API
Use Socket Mode to listen for app mention events and respond to them with a ... with this tutorial, follow our Getting Started...
Read more >
Getting Started with Creating Slack Bots & Slack Apps
Consideration #1: Getting events via Slack Events API vs Socket Mode ... https://Slack.dev/bolt-js/deployments/aws-lambda.
Read more >
slack events api triggers multiple times by one message
I had the same issue receiving multiple events. Turned out I was receiving 1 event per bot user event I added. I don't...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found