Skip to content

Instantly share code, notes, and snippets.

@Smerity
Created January 10, 2019 23:18
Show Gist options
  • Save Smerity/77d0d5edfdeb2304393f9d253c705bf5 to your computer and use it in GitHub Desktop.
Save Smerity/77d0d5edfdeb2304393f9d253c705bf5 to your computer and use it in GitHub Desktop.
Send a message using the Slack postMessage API
import requests
AUTH_TOKEN = 'xoxb-...'
channel = '#locked-out'
USER_ID = 'UDAS0J04S' # A user ID obtained by `list_members.py`
text = f'<@{USER_ID}>, Smerity locked himself out'
params = {
'token': AUTH_TOKEN,
'channel': channel,
'text': text
}
r = requests.post('https://slack.com/api/chat.postMessage', params=params)
# You can print the response to see if it succeeded - expecting a 200
print(r)
# The Slack response itself may actually be an error however
# 'ok' will be true or false depending on whether an error was triggered
print(r.json())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment