Skip to content

Instantly share code, notes, and snippets.

@pmbaumgartner
Last active December 15, 2015 17:51
Show Gist options
  • Save pmbaumgartner/644ed4c56a17c4a4aa2b to your computer and use it in GitHub Desktop.
Save pmbaumgartner/644ed4c56a17c4a4aa2b to your computer and use it in GitHub Desktop.
Creating a JSON Response to a Slack Slash Request
from flask import Flask, request, make_response
import json
import os
app = Flask(__name__)
@app.route("/hello", methods=['POST'])
def hello():
# grab the post request
data = request.form
# this is what a user types after the slash command
query_text = data['text']
# craft JSON payload response
# these variables are not declared, need to replace
# for more details: https://api.slack.com/slash-commands
payload = {
"response_type":response_type,
"text":response_text
"attachments":[
{
"text":attachment_text,
"title":title,
"title_link":title_link,
"color":"#005DA2",
"fields":[
{
'title':"Field 1",
'value':value1,
'short':True
},
{
'title':"Field 2",
'value':value2,
'short':True
}
]
}
]
}
# dump that dictionary to json
json_payload = json.dumps(payload)
# JSON response must have json header for Slack
# update this to use flask.jsonify()
# http://stackoverflow.com/questions/7907596/json-dumps-vs-flask-jsonify
response = make_response(json_payload)
response.headers['content-type'] = 'application/json'
return response
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port, debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment