X Tutup
Skip to content

Commit 119ae11

Browse files
committed
Add OAuth flow sample app using Chalice
1 parent 85502a4 commit 119ae11

File tree

7 files changed

+127
-6
lines changed

7 files changed

+127
-6
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"version": "2.0",
3+
"app_name": "bolt-python-chalice",
4+
"stages": {
5+
"dev": {
6+
"api_gateway_stage": "api",
7+
"environment_variables": {
8+
"SLACK_SIGNING_SECRET": "xxx",
9+
"SLACK_CLIENT_ID": "111.111",
10+
"SLACK_CLIENT_SECRET": "xxx",
11+
"SLACK_SCOPES": "app_mentions:read,channels:history,im:history,chat:write",
12+
"SLACK_INSTALLATION_S3_BUCKET_NAME": "",
13+
"SLACK_STATE_S3_BUCKET_NAME": ""
14+
},
15+
"manage_iam_role": false,
16+
"iam_role_arn": "arn:aws:iam::1111111111111:role/bolt_python_s3_storage"
17+
}
18+
}
19+
}
File renamed without changes.

samples/aws_chalice/app.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@
1717
@bolt_app.event("app_mention")
1818
def handle_app_mentions(payload, say, logger):
1919
logger.info(payload)
20-
say("What's up?")
20+
say("What's up? I'm a Chalice app :wave:")
2121

2222

2323
ChaliceSlackRequestHandler.clear_all_log_handlers()
2424
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG)
2525

2626
# Don't change this variable name "app"
2727
app = Chalice(app_name="bolt-python-chalice")
28+
slack_handler = ChaliceSlackRequestHandler(app=bolt_app)
2829

2930

3031
@app.route("/slack/events", methods=["POST"])
31-
def index() -> Response:
32-
slack_handler = ChaliceSlackRequestHandler(app=bolt_app)
32+
def events() -> Response:
3333
return slack_handler.handle(app.current_request)
3434

3535
# configure aws credentials properly
@@ -39,4 +39,4 @@ def index() -> Response:
3939
# chalice deploy
4040

4141
# for local dev
42-
# chalice local --stage dev --port 3000
42+
# chalice local --stage dev --port 3000

samples/aws_chalice/oauth_app.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# ------------------------------------------------
2+
# instead of slack_bolt in requirements.txt
3+
import sys
4+
5+
sys.path.insert(1, "latest_slack_bolt")
6+
# ------------------------------------------------
7+
8+
import logging
9+
from chalice import Chalice, Response
10+
from slack_bolt import App
11+
from slack_bolt.adapter.aws_lambda.chalice_handler import ChaliceSlackRequestHandler
12+
from slack_bolt.adapter.aws_lambda.lambda_s3_oauth_flow import LambdaS3OAuthFlow
13+
14+
# process_before_response must be True when running on FaaS
15+
bolt_app = App(
16+
process_before_response=True,
17+
oauth_flow=LambdaS3OAuthFlow(
18+
install_path="/api/slack/install",
19+
redirect_uri_path="/api/slack/oauth_redirect",
20+
),
21+
)
22+
23+
24+
@bolt_app.event("app_mention")
25+
def handle_app_mentions(payload, say, logger):
26+
logger.info(payload)
27+
say("What's up? I'm a Chalice app :wave:")
28+
29+
30+
ChaliceSlackRequestHandler.clear_all_log_handlers()
31+
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG)
32+
33+
# Don't change this variable name "app"
34+
app = Chalice(app_name="bolt-python-chalice")
35+
slack_handler = ChaliceSlackRequestHandler(app=bolt_app)
36+
37+
38+
@app.route("/slack/events", methods=["POST"])
39+
def events() -> Response:
40+
return slack_handler.handle(app.current_request)
41+
42+
43+
@app.route("/slack/install", methods=["GET"])
44+
def install() -> Response:
45+
return slack_handler.handle(app.current_request)
46+
47+
48+
@app.route("/slack/oauth_redirect", methods=["GET"])
49+
def oauth_redirect() -> Response:
50+
return slack_handler.handle(app.current_request)
51+
52+
# configure aws credentials properly
53+
# pip install -r requirements.txt
54+
# edit .chalice/config.json
55+
# rm -rf vendor/latest_slack_bolt && cp -pr ../../src vendor/latest_slack_bolt
56+
# chalice deploy
57+
58+
# for local dev
59+
# chalice local --stage dev --port 3000
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
chalice>=1,<2
22
click>=7,<8
33
slackclient>=2,<3
4+
boto3==1.4.4

samples/aws_chalice/simple_app.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# ------------------------------------------------
2+
# instead of slack_bolt in requirements.txt
3+
import sys
4+
5+
sys.path.insert(1, "latest_slack_bolt")
6+
# ------------------------------------------------
7+
8+
import logging
9+
from chalice import Chalice, Response
10+
from slack_bolt import App
11+
from slack_bolt.adapter.aws_lambda.chalice_handler import ChaliceSlackRequestHandler
12+
13+
# process_before_response must be True when running on FaaS
14+
bolt_app = App(process_before_response=True)
15+
16+
17+
@bolt_app.event("app_mention")
18+
def handle_app_mentions(payload, say, logger):
19+
logger.info(payload)
20+
say("What's up? I'm a Chalice app :wave:")
21+
22+
23+
ChaliceSlackRequestHandler.clear_all_log_handlers()
24+
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG)
25+
26+
# Don't change this variable name "app"
27+
app = Chalice(app_name="bolt-python-chalice")
28+
slack_handler = ChaliceSlackRequestHandler(app=bolt_app)
29+
30+
31+
@app.route("/slack/events", methods=["POST"])
32+
def events() -> Response:
33+
return slack_handler.handle(app.current_request)
34+
35+
# configure aws credentials properly
36+
# pip install -r requirements.txt
37+
# edit .chalice/config.json
38+
# rm -rf vendor/latest_slack_bolt && cp -pr ../../src vendor/latest_slack_bolt
39+
# chalice deploy
40+
41+
# for local dev
42+
# chalice local --stage dev --port 3000

src/slack_bolt/adapter/aws_lambda/lambda_s3_oauth_flow.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ def __init__(
3030
user_scopes: Optional[str] = os.environ.get("SLACK_USER_SCOPES", None),
3131
redirect_uri: Optional[str] = os.environ.get("SLACK_REDIRECT_URI", None),
3232

33-
install_path: str = os.environ["SLACK_LAMBDA_PATH"],
34-
redirect_uri_path: str = os.environ["SLACK_LAMBDA_PATH"],
33+
install_path: str = os.environ.get("SLACK_LAMBDA_PATH", "/slack/install"),
34+
redirect_uri_path: str = os.environ.get("SLACK_LAMBDA_PATH", "/slack/oauth_redirect"),
3535

3636
success_url: Optional[str] = None,
3737
failure_url: Optional[str] = None,

0 commit comments

Comments
 (0)
X Tutup