<jupyter_start><jupyter_text>Let's post a message to SlackIn this session, we're going to use Python to post a message to Slack. I set up [a team for us](https://ire-cfj-2017.slack.com/) so we can mess around with the [Slack API](https://api.slack.com/).We're going to use a simple [_incoming webhook_](https://api.slack.com/incoming-webhooks) to accomplish this.  Hello APIAPI stands for "Application Programming Interface." An API is a way to interact programmatically with a software application.If you want to post a message to Slack, you could open a browser and navigate to your URL and sign in with your username and password (or open the app), click on the channel you want, and start typing.OR ... you could post your Slack message with a Python script.  Hello environmental variablesThe code for this boot camp [is on the public internet](https://github.com/ireapps/cfj-2017). We don't want anyone on the internet to be able to post messages to our Slack channels, so we're going to use an [environmental variable](https://en.wikipedia.org/wiki/Environment_variable) to store our webhook.The environmental variable we're going to use -- `IRE_CFJ_2017_SLACK_HOOK` -- should already be stored on your computer.Python has a standard library module for working with the operating system called [`os`](https://docs.python.org/3/library/os.html). The `os` module has a data attribute called `environ`, a dictionary of environmental variables stored on your computer.(Here is a new thing: Instead of using brackets to access items in a dictionary, you can use the `get()` method. The advantage to doing it this way: If the item you're trying to get doesn't exist in your dictionary, it'll return `None` instead of throwing an exception, which is sometimes a desired behavior.)<jupyter_code>from os import environ

slack_hook = environ.get('IRE_CFJ_2017_SLACK_HOOK', None)<jupyter_output><empty_output><jupyter_text>Hello JSONSo far we've been working with tabular data -- CSVs with columns and rows. Most modern web APIs prefer to shake hands with a data structure called [JSON](http://www.json.org/) (**J**ava**S**cript **O**bject **N**otation), which is more like a matryoshka doll.![](https://media.giphy.com/media/Ud5r7tzmG4De0/giphy.gif "russian nesting dolls")Python has a standard library module for working with JSON data called [`json`](https://docs.python.org/3/library/json.html). Let's import it.<jupyter_code>import json<jupyter_output><empty_output><jupyter_text>Using `requests` to post dataWe're also going to use the `requests` library again, except this time, instead of using the `get()` method to get something off the web, we're going to use the `post()` method to send data _to_ the web.<jupyter_code>import requests<jupyter_output><empty_output><jupyter_text>Formatting the data correctlyThe JSON data we're going to send to the Slack webhook will start its life as a Python dictionary. Then we'll use the `json` module's `dumps()` method to turn it into a string of JSON.<jupyter_code># build a dictionary of payload data
payload = {
    'channel': '#general',
    'username': 'IRE Python Bot',
    'icon_emoji': ':ire:',
    'text': 'helllllllo!'
}

# turn it into a string of JSON
payload_as_json = json.dumps(payload)<jupyter_output><empty_output><jupyter_text>Send it off to Slack<jupyter_code># check to see if you have the webhook URL
if slack_hook:

    # send it to slack!
    requests.post(slack_hook, data=payload_as_json)

else:

    # if you don't have the webhook env var, print a message to the terminal
    print("You don't have the IRE_CFJ_2017_SLACK_HOOK"
          " environmental variable")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09028914348063284, 'fraction_numerical': 0.0070921985815602835, 'mean_word_length': 4.952922077922078, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 8, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '5469', 'license': 'MIT', 'n_tokens_mistral': 1100, 'n_tokens_neox': 1042, 'n_words': 462, 'path': 'completed/08. Working with APIs (Part 1).ipynb', 'repo_name': 'cjwinchester/cfj-2017', 'size': '3666'}