Skip to content

Instantly share code, notes, and snippets.

@kstohr
Created April 13, 2017 22:05
Show Gist options
  • Save kstohr/901740d1a97cc7707b5fe13b581a717d to your computer and use it in GitHub Desktop.
Save kstohr/901740d1a97cc7707b5fe13b581a717d to your computer and use it in GitHub Desktop.
Python Slackbot for sending data to Slack
from slackclient import SlackClient
import json
import requests
# Store your data output/analysis as a variable named 'attachment'
# See: https://api.slack.com/docs/messages
attachment = {"text": "And, here is an attachment"}
# Output to json, if in other format
attachment = json.dumps(attachment)
# Set your credentials
# Go to https://api.slack.com/
# Create a custom app on Slack and generate api credentials
# To add a bot user: https://api.slack.com/bot-users
# See:https://api.slack.com/docs/oauth
# Post to slack
slack_token = os.environ["SLACK_API_TOKEN"]
sc = SlackClient(slack_token)
channel = '@User.Name' #test by posting to your own slack channel, can be changed to any channel
sc.api_call(
"chat.postMessage",
channel=channel,
text: "I am a test message http://slack.com",
attachments=attachment,
as_user = True,
username='YOUR_SLACK_BOTS_NAME_HERE',
unfurl_links = 'true',
unfurl_media = 'true'
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment