X Tutup
Skip to content

Commit 5a702cd

Browse files
authored
Implement Lazy Listener Functions slackapi#34 (slackapi#35)
* Implement Lazy Listener Functions slackapi#34 * Add unit tests * Remove test.pypi.org from instructions * Fix errors in Python 3.6 * Apply formtter to samples * Fix type hints
1 parent dd855e1 commit 5a702cd

34 files changed

+1216
-242
lines changed

samples/aws_chalice/app.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
from slack_bolt.adapter.aws_lambda.chalice_handler import ChaliceSlackRequestHandler
77

88
# process_before_response must be True when running on FaaS
9-
bolt_app = App(
10-
process_before_response=True,
11-
authorization_test_enabled=False,
12-
)
9+
bolt_app = App(process_before_response=True, authorization_test_enabled=False,)
1310

1411

1512
@bolt_app.event("app_mention")

samples/aws_chalice/simple_app.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
from slack_bolt.adapter.aws_lambda.chalice_handler import ChaliceSlackRequestHandler
77

88
# process_before_response must be True when running on FaaS
9-
bolt_app = App(
10-
process_before_response=True,
11-
authorization_test_enabled=False,
12-
)
9+
bolt_app = App(process_before_response=True, authorization_test_enabled=False,)
1310

1411

1512
@bolt_app.event("app_mention")

samples/aws_lambda/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
vendor/
1+
vendor/
2+
.env

samples/aws_lambda/deploy_lazy.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
rm -rf vendor && mkdir -p vendor/slack_bolt && cp -pr ../../slack_bolt/* vendor/slack_bolt/
3+
pip install python-lambda -U
4+
lambda deploy \
5+
--config-file lazy_aws_lambda_config.yaml \
6+
--requirements requirements.txt
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# ------------------------------------------------
2+
# instead of slack_bolt in requirements.txt
3+
import sys
4+
import time
5+
6+
sys.path.insert(1, "vendor")
7+
# ------------------------------------------------
8+
9+
import logging
10+
11+
from slack_bolt import App
12+
from slack_bolt.adapter.aws_lambda import SlackRequestHandler
13+
14+
# process_before_response must be True when running on FaaS
15+
app = App(process_before_response=True)
16+
17+
18+
@app.middleware # or app.use(log_request)
19+
def log_request(logger, payload, next):
20+
logger.debug(payload)
21+
return next()
22+
23+
24+
command = "/hello-bolt-python-lambda"
25+
26+
27+
def respond_to_slack_within_3_seconds(payload, ack):
28+
if payload.get("text", None) is None:
29+
ack(f":x: Usage: {command} (description here)")
30+
else:
31+
title = payload["text"]
32+
ack(f"Accepted! (task: {title})")
33+
34+
35+
def process_request(respond, payload):
36+
time.sleep(5)
37+
title = payload["text"]
38+
respond(f"Completed! (task: {title})")
39+
40+
41+
app.command(command)(ack=respond_to_slack_within_3_seconds, lazy=[process_request])
42+
43+
SlackRequestHandler.clear_all_log_handlers()
44+
logging.basicConfig(format="%(asctime)s %(message)s", level=logging.DEBUG)
45+
46+
47+
def handler(event, context):
48+
slack_handler = SlackRequestHandler(app=app)
49+
return slack_handler.handle(event, context)
50+
51+
52+
# export SLACK_SIGNING_SECRET=***
53+
# export SLACK_BOT_TOKEN=xoxb-***
54+
55+
# rm -rf vendor && cp -pr ../../src/* vendor/
56+
# pip install python-lambda
57+
# lambda deploy --config-file aws_lambda_config.yaml --requirements requirements.txt
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
region: us-east-1
2+
3+
function_name: bolt_py_function
4+
handler: lazy_aws_lambda.handler
5+
description: My first lambda function
6+
runtime: python3.8
7+
# role: lambda_basic_execution
8+
role: bolt_python_lambda_invocation # AWSLambdaFullAccess
9+
10+
# S3 upload requires appropriate role with s3:PutObject permission
11+
# (ex. basic_s3_upload), a destination bucket, and the key prefix
12+
# bucket_name: 'example-bucket'
13+
# s3_key_prefix: 'path/to/file/'
14+
15+
# if access key and secret are left blank, boto will use the credentials
16+
# defined in the [default] section of ~/.aws/credentials.
17+
aws_access_key_id:
18+
aws_secret_access_key:
19+
20+
# dist_directory: dist
21+
# timeout: 15
22+
# memory_size: 512
23+
# concurrency: 500
24+
#
25+
26+
# Experimental Environment variables
27+
environment_variables:
28+
SLACK_BOT_TOKEN: ${SLACK_BOT_TOKEN}
29+
SLACK_SIGNING_SECRET: ${SLACK_SIGNING_SECRET}
30+
31+
# If `tags` is uncommented then tags will be set at creation or update
32+
# time. During an update all other tags will be removed except the tags
33+
# listed here.
34+
#tags:
35+
# tag_1: foo
36+
# tag_2: bar
37+
38+
# Build options
39+
build:
40+
source_directories: vendor # a comma delimited list of directories in your project root that contains source to package.

samples/django/slackapp/slackapp/settings.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535

3636
# SECURITY WARNING: keep the secret key used in production secret!
3737
# TODO: CHANGE THIS IF YOU REUSE THIS APP
38-
SECRET_KEY = "This is just a example. You should not expose your secret key in real apps"
38+
SECRET_KEY = (
39+
"This is just a example. You should not expose your secret key in real apps"
40+
)
3941

4042
# SECURITY WARNING: don't run with debug turned on in production!
4143
DEBUG = True
Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
# ------------------------------------------------
22
# instead of slack_bolt in requirements.txt
3-
import asyncio
43
import sys
54

65
sys.path.insert(1, "..")
76
# ------------------------------------------------
87

8+
import asyncio
99
import logging
10+
from slack_bolt.async_app import AsyncApp
1011

1112
logging.basicConfig(level=logging.DEBUG)
1213

13-
from slack_bolt.async_app import AsyncApp
14-
1514
app = AsyncApp()
1615

1716

@@ -21,11 +20,12 @@ async def log_request(logger, payload, next):
2120
return await next()
2221

2322

24-
@app.command("/hello-bolt-python")
25-
async def handle_command(payload, ack, respond, client, logger):
23+
async def ack_command(payload, ack, logger):
2624
logger.info(payload)
27-
await ack("Accepted!")
25+
await ack("Thanks!")
26+
2827

28+
async def post_button_message(respond):
2929
await respond(
3030
blocks=[
3131
{
@@ -45,6 +45,8 @@ async def handle_command(payload, ack, respond, client, logger):
4545
]
4646
)
4747

48+
49+
async def open_modal(payload, client, logger):
4850
res = await client.views_open(
4951
trigger_id=payload["trigger_id"],
5052
view={
@@ -66,6 +68,7 @@ async def handle_command(payload, ack, respond, client, logger):
6668
"type": "external_select",
6769
"action_id": "es_a",
6870
"placeholder": {"type": "plain_text", "text": "Select an item"},
71+
"min_query_length": 0,
6972
},
7073
"label": {"type": "plain_text", "text": "Search"},
7174
},
@@ -76,6 +79,7 @@ async def handle_command(payload, ack, respond, client, logger):
7679
"type": "multi_external_select",
7780
"action_id": "mes_a",
7881
"placeholder": {"type": "plain_text", "text": "Select an item"},
82+
"min_query_length": 0,
7983
},
8084
"label": {"type": "plain_text", "text": "Search (multi)"},
8185
},
@@ -85,6 +89,11 @@ async def handle_command(payload, ack, respond, client, logger):
8589
logger.info(res)
8690

8791

92+
app.command("/hello-bolt-python")(
93+
ack=ack_command, lazy=[post_button_message, open_modal],
94+
)
95+
96+
8897
@app.options("es_a")
8998
async def show_options(ack):
9099
await ack(
@@ -125,19 +134,22 @@ async def show_multi_options(ack):
125134

126135

127136
@app.view("view-id")
128-
async def view_submission(ack, payload, logger):
137+
async def handle_view_submission(ack, payload, logger):
129138
await ack()
130139
logger.info(payload["view"]["state"]["values"])
131140

132141

133-
@app.action("a")
134-
async def button_click(ack, respond):
142+
async def ack_button_click(ack, respond):
135143
await ack()
144+
await respond("Loading ...")
145+
146+
147+
async def respond_5_seconds_later(respond):
136148
await asyncio.sleep(5)
137-
await respond(
138-
{"response_type": "in_channel", "text": "Clicked!",}
139-
)
149+
await respond("Completed!")
150+
140151

152+
app.action("a")(ack=ack_button_click, lazy=[respond_5_seconds_later])
141153

142154
if __name__ == "__main__":
143155
app.start(3000)

0 commit comments

Comments
 (0)
X Tutup