Skip to content

Commit fa8a52f

Browse files
committed
Add OAuth Flow support slackapi#1
1 parent 464590f commit fa8a52f

44 files changed

Lines changed: 1521 additions & 163 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ pip
3434
tmp.txt
3535
.DS_Store
3636
logs/
37+
38+
.env

samples/aws_lambda/.env.sample

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export SLACK_SIGNING_SECRET=
2+
export SLACK_CLIENT_ID=
3+
export SLACK_CLIENT_SECRET=
4+
export SLACK_SCOPES=app_mentions:read,channels:history,im:history,chat:write
5+
export SLACK_INSTALLATION_S3_BUCKET_NAME=
6+
export SLACK_STATE_S3_BUCKET_NAME=
7+
export SLACK_LAMBDA_PATH=/default/bolt_py_function

samples/aws_lambda/aws_lambda.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,17 @@
77

88
import logging
99
from slack_bolt import App
10-
from slack_bolt.adapter.aws_lambda import SlackRequestHandler
10+
from slack_bolt.adapter.aws_lambda import SlackRequestHandler, LambdaS3OAuthFlow
1111

1212
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG)
13-
app = App(process_before_response=True)
1413

14+
app = App(
15+
process_before_response=True,
16+
oauth_flow=LambdaS3OAuthFlow(),
17+
)
1518

16-
@app.command("/bolt-py-proto")
19+
20+
@app.shortcut("bolt-python-on-aws-lambda")
1721
def command_handler(logger, payload, client, ack):
1822
res = client.views_open(
1923
trigger_id=payload["trigger_id"],
@@ -55,11 +59,37 @@ def view_submission(ack, payload, logger):
5559
return ack()
5660

5761

62+
@app.event("app_mention")
63+
def handle_app_mentions(payload, say, logger):
64+
logger.info(payload)
65+
say("What's up?")
66+
67+
slack_handler = SlackRequestHandler(app=app)
68+
69+
5870
def handler(event, context):
59-
return SlackRequestHandler(app).handle(event, context)
71+
return slack_handler.handle(event, context)
72+
73+
# export SLACK_SIGNING_SECRET=***
74+
# export SLACK_BOT_TOKEN=xoxb-***
6075

76+
# rm -rf src
77+
# cp -pr ../../src src
78+
# pip install python-lambda
79+
# lambda deploy --config-file aws_lambda_config.yaml --requirements requirements.txt
80+
81+
# # -- OAuth flow -- #
6182
# export SLACK_SIGNING_SECRET=***
6283
# export SLACK_BOT_TOKEN=xoxb-***
84+
# export SLACK_CLIENT_ID=111.111
85+
# export SLACK_CLIENT_SECRET=***
86+
# export SLACK_SCOPES=app_mentions:read,channels:history,im:history,chat:write
87+
88+
# AWS IAM Role: bolt_python_s3_storage
89+
# - AmazonS3FullAccess
90+
# - AWSLambdaBasicExecutionRole
91+
92+
# rm -rf src
6393
# cp -pr ../../src src
6494
# pip install python-lambda
6595
# lambda deploy --config-file aws_lambda_config.yaml --requirements requirements.txt

samples/aws_lambda/aws_lambda_config.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ handler: aws_lambda.handler
55
description: My first lambda function
66
runtime: python3.8
77
# role: lambda_basic_execution
8+
role: bolt_python_s3_storage
89

910
# S3 upload requires appropriate role with s3:PutObject permission
1011
# (ex. basic_s3_upload), a destination bucket, and the key prefix
@@ -26,6 +27,13 @@ aws_secret_access_key:
2627
environment_variables:
2728
SLACK_BOT_TOKEN: ${SLACK_BOT_TOKEN}
2829
SLACK_SIGNING_SECRET: ${SLACK_SIGNING_SECRET}
30+
SLACK_CLIENT_ID: ${SLACK_CLIENT_ID}
31+
SLACK_CLIENT_SECRET: ${SLACK_CLIENT_SECRET}
32+
SLACK_SCOPES: ${SLACK_SCOPES}
33+
SLACK_INSTALLATION_S3_BUCKET_NAME: ${SLACK_INSTALLATION_S3_BUCKET_NAME}
34+
SLACK_STATE_S3_BUCKET_NAME: ${SLACK_STATE_S3_BUCKET_NAME}
35+
SLACK_LAMBDA_PATH: ${SLACK_LAMBDA_PATH}
36+
2937

3038
# If `tags` is uncommented then tags will be set at creation or update
3139
# time. During an update all other tags will be removed except the tags

samples/aws_lambda/deploy.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/bash
2+
rm -rf ./src
3+
cp -pr ../../src src
4+
pip install python-lambda -U
5+
lambda deploy \
6+
--config-file aws_lambda_config.yaml \
7+
--requirements requirements.txt
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
slackclient==2.6.2
1+
slackclient==2.7.3
2+
boto3==1.4.4

samples/falcon/app.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,25 @@ def button_click(logger, payload, say, ack, respond):
9494
# say(text="say!")
9595
return ack()
9696

97+
@app.event("app_mention")
98+
def handle_app_mentions(payload, say, logger):
99+
logger.info(payload)
100+
say("What's up?")
97101

98102
api = falcon.API()
99-
api.add_route("/slack/events", SlackAppResource(app))
103+
resource = SlackAppResource(app)
104+
api.add_route("/slack/events", resource)
100105

101106
# pip install -r requirements.txt
102107
# export SLACK_SIGNING_SECRET=***
103108
# export SLACK_BOT_TOKEN=xoxb-***
104109
# gunicorn app:api --reload -b 0.0.0.0:3000
110+
111+
# # -- OAuth flow -- #
112+
# export SLACK_SIGNING_SECRET=***
113+
# export SLACK_BOT_TOKEN=xoxb-***
114+
# export SLACK_CLIENT_ID=111.111
115+
# export SLACK_CLIENT_SECRET=***
116+
# export SLACK_SCOPES=app_mentions:read,channels:history,im:history,chat:write
117+
api.add_route("/slack/install", resource)
118+
api.add_route("/slack/oauth_redirect", resource)

samples/fastapi/app.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ def button_click(logger, payload, say, ack, respond):
9595
return ack()
9696

9797

98+
@app.event("app_mention")
99+
def handle_app_mentions(payload, say, logger):
100+
logger.info(payload)
101+
say("What's up?")
102+
103+
98104
from fastapi import FastAPI, Request
99105

100106
api = FastAPI()
@@ -104,7 +110,25 @@ def button_click(logger, payload, say, ack, respond):
104110
async def endpoint(req: Request):
105111
return await app_handler.handle(req)
106112

113+
107114
# pip install -r requirements.txt
108115
# export SLACK_SIGNING_SECRET=***
109116
# export SLACK_BOT_TOKEN=xoxb-***
110117
# uvicorn app:api --reload --port 3000
118+
119+
120+
# # -- OAuth flow -- #
121+
# export SLACK_SIGNING_SECRET=***
122+
# export SLACK_BOT_TOKEN=xoxb-***
123+
# export SLACK_CLIENT_ID=111.111
124+
# export SLACK_CLIENT_SECRET=***
125+
# export SLACK_SCOPES=app_mentions:read,channels:history,im:history,chat:write
126+
127+
@api.get("/slack/install")
128+
async def install(req: Request):
129+
return await app_handler.handle(req)
130+
131+
132+
@api.get("/slack/oauth_redirect")
133+
async def install(req: Request):
134+
return await app_handler.handle(req)

samples/flask/app.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,6 @@ def button_click(logger, payload, ack, respond):
103103
def event_test(ack, payload, say, logger):
104104
logger.info(payload)
105105
say("What's up?")
106-
return ack()
107106

108107

109108
@app.event({"type": "message", "subtype": "message_deleted"})
@@ -117,7 +116,6 @@ def deleted(ack, payload, say):
117116
def new_message(ack, logger, payload, say):
118117
logger.info(payload)
119118
# say(f"I've noticed you deleted {payload}")
120-
return ack()
121119

122120

123121
from flask import Flask, request
@@ -130,7 +128,25 @@ def new_message(ack, logger, payload, say):
130128
def slack_events():
131129
return handler.handle(request)
132130

131+
132+
@flask_app.route("/slack/install", methods=["GET"])
133+
def install():
134+
return handler.handle(request)
135+
136+
137+
@flask_app.route("/slack/oauth_redirect", methods=["GET"])
138+
def oauth_redirect():
139+
return handler.handle(request)
140+
133141
# pip install -r requirements.txt
134142
# export SLACK_SIGNING_SECRET=***
135143
# export SLACK_BOT_TOKEN=xoxb-***
144+
145+
# # -- OAuth flow -- #
146+
# export SLACK_SIGNING_SECRET=***
147+
# export SLACK_BOT_TOKEN=xoxb-***
148+
# export SLACK_CLIENT_ID=111.111
149+
# export SLACK_CLIENT_SECRET=***
150+
# export SLACK_SCOPES=app_mentions:read,channels:history,im:history,chat:write
151+
136152
# FLASK_APP=app.py FLASK_ENV=development flask run -p 3000

samples/oauth_app.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# ------------------------------------------------
2+
# instead of slack_bolt in requirements.txt
3+
import sys
4+
5+
sys.path.insert(1, "../src")
6+
# ------------------------------------------------
7+
8+
import logging
9+
from slack_bolt import App
10+
11+
logging.basicConfig(level=logging.DEBUG)
12+
app = App()
13+
14+
def log_request(logger, payload, next):
15+
logger.debug(payload)
16+
return next()
17+
18+
app.use(log_request)
19+
20+
@app.event("app_mention")
21+
def handle_app_mentions(payload, say, logger, context):
22+
logger.info(payload)
23+
say("What's up?")
24+
25+
@app.event("message")
26+
def handle_messages():
27+
pass
28+
29+
if __name__ == '__main__':
30+
app.start(3000)
31+
32+
# pip install slackclient
33+
# pip install -i https://test.pypi.org/simple/ slack_bolt
34+
# export SLACK_SIGNING_SECRET=***
35+
# export SLACK_BOT_TOKEN=xoxb-***
36+
# export SLACK_CLIENT_ID=111.111
37+
# export SLACK_CLIENT_SECRET=***
38+
# export SLACK_SCOPES=app_mentions:read,channels:history,im:history,chat:write
39+
# python oauth_app.py

0 commit comments

Comments
 (0)