I have an end-point for a Slack slash command that uses the Slack Python sdk to call Slack’s API.
@app.route('/route', methods=['POST', 'GET'])
def foo():
slack_web_client = WebClient(token=os.environ['SLACK_ORG_ADMIN_USER_TOKEN'])
start = time()
data = request.form
user_id = data.get('user_id')
text = data.get('text')
response_url = data.get('response_url')
@copy_current_request_context
def execute_foo(user_id, text, response_url):
print("######## Execute foo #########")
# check if text has one parameter: new password
print("###### Checking parameters ########")
params = text.split(" ")
if len(params) != 1:
print("######## Error: Invalid params #########")
requests.post(response_url, data=json.dumps({'text':':negative_squared_cross_mark: Error'}))
return make_response("", 404)
new_pass = params[0]
# check if new_pass hits requirements:
# 1. at least 12 characters
# 2. at least 1 uppercase letter
# 3. at least 1 lowercase letter
# 4. at least 1 number
# 5. at least 1 special character
print("######## Checking password criteria ########")
if len(new_pass) < 12 or not re.search("[a-z]", new_pass) or not re.search("[A-Z]", new_pass) or not re.search("[0-9]", new_pass):
print("######## Error: Invalid password #########")
requests.post(response_url, data=json.dumps({'text': ':negative_squared_cross_mark: Your new password must contain the following:\n1. At least 12 characters\n2. At least 1 uppercase letter\n3. At least 1 lowercase letter\n4. At least 1 number\n5. At least 1 special character'}))
return make_response("", 404)
# get user's email from slack
print("######## Getting email from Slack #########")
user_email = None
try:
print("#### Calling `users_info` API #######")
user_email = slack_web_client.users_info(user=user_id)['user']['profile']['email']
except SlackApiError as e:
print("######## Error: Could not retrieve user email from Slack #########")
requests.post(response_url, data=json.dumps({'text': ':negative_squared_cross_mark: Could not retrieve user email from Slack'}))
return make_response("", 404)
print("##### Got user email ####")
requests.post(response_url, data=json.dumps({'text': 'Success'}))
return make_response("", 200)
process = Thread(target=execute_foo, args=[user_id, text, response_url])
process.start()
return ':hourglass_flowing_sand: Your request is being serviced...'
However, every time the slash command is used, the function only logs until print("#### Calling
users_info API #######")
before it is stopped. I’ve increased the timeout to 10 minutes. Why can’t I call Slack API?
This is the serverless.yaml
file:
org: org-name
app: app-name
service: service-name
frameworkVersion: '3'
custom:
wsgi:
app: app.app
pythonRequirements:
dockerizePip: true
provider:
name: aws
runtime: python3.9
functions:
api:
handler: wsgi_handler.handler
events:
- httpApi: '*'
plugins:
- serverless-wsgi
- serverless-python-requirements