6
6
client_id = os .environ ["SLACK_CLIENT_ID" ]
7
7
client_secret = os .environ ["SLACK_CLIENT_SECRET" ]
8
8
signing_secret = os .environ ["SLACK_SIGNING_SECRET" ]
9
- state = ' super-secret-state'
9
+ state = " super-secret-state"
10
10
# Scopes needed for this app
11
- oauth_scope = ' chat:write, channels:read, channels:join, channels:manage'
11
+ oauth_scope = "" . join ([ " chat:write" , " channels:read" , " channels:join" , " channels:manage" ])
12
12
13
13
app = Flask (__name__ )
14
+
14
15
# Route to kick off Oauth flow
15
16
@app .route ("/begin_auth" , methods = ["GET" ])
16
17
def pre_install ():
@@ -20,14 +21,14 @@ def pre_install():
20
21
@app .route ("/finish_auth" , methods = ["GET" , "POST" ])
21
22
def post_install ():
22
23
# Retrieve the auth code and state from the request params
23
- auth_code = request .args [' code' ]
24
- received_state = request .args [' state' ]
24
+ auth_code = request .args [" code" ]
25
+ received_state = request .args [" state" ]
25
26
26
- # An empty string is a valid token for this request
27
- client = slack .WebClient (token = "" )
27
+ # Token is not required to call the oauth.v2.access method
28
+ client = slack .WebClient ()
28
29
29
30
if received_state == state :
30
- # Request the auth tokens from Slack
31
+ # Exchange the authorization code for an access token with Slack
31
32
response = client .oauth_v2_access (
32
33
client_id = client_id ,
33
34
client_secret = client_secret ,
@@ -37,7 +38,7 @@ def post_install():
37
38
return "Invalid State"
38
39
39
40
# Save the bot token to an environmental variable or to your data store
40
- os .environ ["SLACK_BOT_TOKEN" ] = response [' access_token' ]
41
+ os .environ ["SLACK_BOT_TOKEN" ] = response [" access_token" ]
41
42
42
43
# See if "the-welcome-channel" exists. Create it if it doesn't.
43
44
channel_exists ()
@@ -55,7 +56,7 @@ def channel_exists():
55
56
exists = False
56
57
for k in clist ["channels" ]:
57
58
# look for the channel in the list of existing channels
58
- if k [' name' ] == ' the-welcome-channel' :
59
+ if k [" name" ] == " the-welcome-channel" :
59
60
exists = True
60
61
break
61
62
if exists == False :
@@ -76,13 +77,13 @@ def create_channel():
76
77
# Sends a DM to the user who joined the channel
77
78
@slack_events_adapter .on ("member_joined_channel" )
78
79
def member_joined_channel (event_data ):
79
- user = event_data [' event' ][ ' user' ]
80
- channelid = event_data [' event' ][ ' channel' ]
80
+ user = event_data [" event" ][ " user" ]
81
+ channelid = event_data [" event" ][ " channel" ]
81
82
token = os .environ ["SLACK_BOT_TOKEN" ]
82
83
83
84
# In case the app doesn't have access to the oAuth Token
84
85
if token is None :
85
- print (' ERROR: Autenticate the App!' )
86
+ print (" ERROR: Autenticate the App!" )
86
87
return
87
88
client = slack .WebClient (token = token )
88
89
0 commit comments