-
Notifications
You must be signed in to change notification settings - Fork 245
/
lazy_aws_lambda.py
57 lines (37 loc) · 1.45 KB
/
lazy_aws_lambda.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# ------------------------------------------------
# instead of slack_bolt in requirements.txt
import sys
import time
sys.path.insert(1, "vendor")
# ------------------------------------------------
import logging
from slack_bolt import App
from slack_bolt.adapter.aws_lambda import SlackRequestHandler
# process_before_response must be True when running on FaaS
app = App(process_before_response=True)
@app.middleware # or app.use(log_request)
def log_request(logger, body, next):
logger.debug(body)
return next()
command = "/hello-bolt-python-lambda"
def respond_to_slack_within_3_seconds(body, ack):
if body.get("text", None) is None:
ack(f":x: Usage: {command} (description here)")
else:
title = body["text"]
ack(f"Accepted! (task: {title})")
def process_request(respond, body):
time.sleep(5)
title = body["text"]
respond(f"Completed! (task: {title})")
app.command(command)(ack=respond_to_slack_within_3_seconds, lazy=[process_request])
SlackRequestHandler.clear_all_log_handlers()
logging.basicConfig(format="%(asctime)s %(message)s", level=logging.DEBUG)
def handler(event, context):
slack_handler = SlackRequestHandler(app=app)
return slack_handler.handle(event, context)
# export SLACK_SIGNING_SECRET=***
# export SLACK_BOT_TOKEN=xoxb-***
# rm -rf vendor && cp -pr ../../src/* vendor/
# pip install python-lambda
# lambda deploy --config-file aws_lambda_config.yaml --requirements requirements.txt