1
+ import json
2
+ import os
3
+ import boto3
4
+
5
+ # Initialize AWS Lambda client
6
+ lambda_client = boto3 .client ('lambda' )
7
+
8
+ def lambda_handler (event , context ):
9
+ """
10
+ Receives Slack events, performs basic validation, and asynchronously invokes the processor Lambda
11
+ """
12
+ print ("Received event: %s" , json .dumps (event ))
13
+
14
+ try :
15
+ # Parse the event body
16
+ body = json .loads (event ['body' ])
17
+
18
+ # Handle Slack URL verification challenge
19
+ if 'challenge' in body :
20
+ return {
21
+ 'statusCode' : 200 ,
22
+ 'body' : json .dumps ({'challenge' : body ['challenge' ]})
23
+ }
24
+
25
+ # Get the event type
26
+ event_type = body .get ('type' , '' )
27
+
28
+ # Check for edited messages
29
+ if (
30
+ event_type == 'event_callback' and
31
+ 'edited' in body .get ('event' , {})
32
+ ):
33
+ print ('Detected edited message, discarding' )
34
+ return {
35
+ 'statusCode' : 200 ,
36
+ 'body' : json .dumps ({'message' : 'Edited message discarded' })
37
+ }
38
+
39
+ # Only process events we care about
40
+ if event_type == 'event_callback' :
41
+ # Asynchronously invoke the processor Lambda
42
+ lambda_client .invoke (
43
+ FunctionName = os .environ ['PROCESSOR_FUNCTION_NAME' ],
44
+ InvocationType = 'Event' , # Async invocation
45
+ Payload = json .dumps (event )
46
+ )
47
+
48
+ # Always return 200 OK to Slack quickly
49
+ return {
50
+ 'statusCode' : 200 ,
51
+ 'body' : json .dumps ({'message' : 'Event received' })
52
+ }
53
+
54
+ except Exception as e :
55
+ print ("Error processing event: %s" , str (e ))
56
+ # Still return 200 to Slack to prevent retries
57
+ return {
58
+ 'statusCode' : 200 ,
59
+ 'body' : json .dumps ({'message' : 'Error processing event' })
60
+ }
0 commit comments