Created
October 2, 2018 15:53
-
-
Save chloegrace94/feee1e31fbd8198e83ec3ba9f5dde022 to your computer and use it in GitHub Desktop.
Post to Slack using response URL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Post to Slack | |
def post_to_slack(channel_id, message_ts, original_message, message_response, response_url): | |
try: | |
slack_data = { | |
"channel":channel_id, | |
"ts":message_ts, | |
"text":original_message, | |
#"attachment_type": "default", | |
"attachments": [ | |
{ | |
"name": "action_decision", | |
"text": message_response, | |
"fallback": "Sorry, I'm unable to do that for you at the moment" | |
} | |
] | |
} | |
logger.info("\nResponse Message: " + str(slack_data)) | |
response = requests.post( | |
response_url, data=json.dumps(slack_data), | |
headers={'Content-Type': 'application/json'} | |
) | |
if response.status_code != 200: | |
raise ValueError( | |
'Request to slack returned an error %s, the response is:\n%s' | |
% (response.status_code, response.text) | |
) | |
except Exception as err: | |
logger.error('Error: %s' % str(err)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment