-
Notifications
You must be signed in to change notification settings - Fork 263
/
Copy pathoauth_app.py
91 lines (68 loc) · 2.29 KB
/
oauth_app.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import logging
logging.basicConfig(level=logging.DEBUG)
logging.getLogger("sqlalchemy.engine").setLevel(logging.INFO)
import os
from slack_bolt import App
from slack_bolt.adapter.flask import SlackRequestHandler
from slack_bolt.oauth.oauth_settings import OAuthSettings
from slack_sdk.oauth.installation_store.sqlalchemy import SQLAlchemyInstallationStore
from slack_sdk.oauth.state_store.sqlalchemy import SQLAlchemyOAuthStateStore
import sqlalchemy
from sqlalchemy.engine import Engine
database_url = "sqlite:///slackapp.db"
# database_url = "postgresql://localhost/slackapp" # pip install psycopg2
logger = logging.getLogger(__name__)
client_id, client_secret, signing_secret = (
os.environ["SLACK_CLIENT_ID"],
os.environ["SLACK_CLIENT_SECRET"],
os.environ["SLACK_SIGNING_SECRET"],
)
engine: Engine = sqlalchemy.create_engine(database_url)
installation_store = SQLAlchemyInstallationStore(
client_id=client_id,
engine=engine,
logger=logger,
)
oauth_state_store = SQLAlchemyOAuthStateStore(
expiration_seconds=120,
engine=engine,
logger=logger,
)
try:
engine.execute("select count(*) from slack_bots")
except Exception as e:
installation_store.metadata.create_all(engine)
oauth_state_store.metadata.create_all(engine)
app = App(
logger=logger,
signing_secret=signing_secret,
installation_store=installation_store,
oauth_settings=OAuthSettings(
client_id=client_id,
client_secret=client_secret,
state_store=oauth_state_store,
),
)
@app.event("app_mention")
def handle_command(say):
say("Hi!")
from flask import Flask, request
flask_app = Flask(__name__)
handler = SlackRequestHandler(app)
@flask_app.route("/slack/events", methods=["POST"])
def slack_events():
return handler.handle(request)
@flask_app.route("/slack/install", methods=["GET"])
def install():
return handler.handle(request)
@flask_app.route("/slack/oauth_redirect", methods=["GET"])
def oauth_redirect():
return handler.handle(request)
# pip install -r requirements.txt
# # -- OAuth flow -- #
# export SLACK_SIGNING_SECRET=***
# export SLACK_BOT_TOKEN=xoxb-***
# export SLACK_CLIENT_ID=111.111
# export SLACK_CLIENT_SECRET=***
# export SLACK_SCOPES=app_mentions:read,chat:write
# FLASK_APP=oauth_app.py FLASK_ENV=development flask run -p 3000