<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Let's post a message to Slack
In this session, we're going to use Python to post a message to Slack. I set up a team for us so we can mess around with the Slack API.
We're going to use a simple incoming webhook to accomplish this.
Hello API
API 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 variables
The code for this boot camp is on the public internet. 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 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. The os module has a data attribute called environ, a dictionary of environmental variables stored on your computer.
(Here is a new thing
Step1:  Hello JSON
So 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 (JavaScript Object Notation), which is more like a matryoshka doll.
Python has a standard library module for working with JSON data called json. Let's import it.
Step2:  Using requests to post data
We'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.
Step3:  Formatting the data correctly
The 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.
Step4:  Send it off to Slack
<ASSISTANT_TASK:>
Python Code:
from os import environ
slack_hook = environ.get('IRE_CFJ_2017_SLACK_HOOK', None)
Explanation: Let's post a message to Slack
In this session, we're going to use Python to post a message to Slack. I set up a team for us so we can mess around with the Slack API.
We're going to use a simple incoming webhook to accomplish this.
Hello API
API 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 variables
The code for this boot camp is on the public internet. 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 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. 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.)
End of explanation
import json
Explanation: Hello JSON
So 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 (JavaScript Object Notation), which is more like a matryoshka doll.
Python has a standard library module for working with JSON data called json. Let's import it.
End of explanation
import requests
Explanation: Using requests to post data
We'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.
End of explanation
# 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)
Explanation: Formatting the data correctly
The 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.
End of explanation
# 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")
Explanation: Send it off to Slack
End of explanation
<END_TASK>