The application of requests – publishing events to the Internet

In the previous example, we retrieved information from the Internet. Let's consider an example where we have to publish a sensor event somewhere on the Internet. This could be either a cat door opening while you are away from home or someone at your doorstep stepping on the doormat. Because we discussed interfacing sensors to the Raspberry Pi Zero in the previous chapter, let's discuss a scenario where we could post these events to Slacka workplace communication tool, Twitter, or cloud services such as Phant (https://data.sparkfun.com/).

In this example, we will post these events to Slack using requests. Let's send a direct message to ourselves on Slack whenever a sensor event such as a cat door opening occurs. We need a URL to post these sensor events to Slack. Let's review generating a URL in order to post sensor events to Slack:

  1. The first step in generating a URL is creating an incoming webhook. A webhook is a type request that can post messages that are carried as a payload to applications such as Slack.
  1. If you are a member of a Slack team named TeamX, launch your team's application directory, namely teamx.slack.com/apps in a browser:
Launch your team's app directory
  1. Search for incoming webhooks in your app directory and select the first option, Incoming WebHooks (as shown in the following screenshot):
Select incoming webhooks
  1. Click on Add Configuration:
Add Configuration
  1. Let's send a private message to ourselves when an event occurs. Select Privately to           (you) as the option and create a webhook by clicking on Add Incoming WebHooks integration:
Select Privately to you
  1. We have generated a URL to send direct messages about sensor events (URL partially concealed):
Generated URL
  1. Now, we can send direct message to ourselves on Slack using the previously-mentioned URL. The sensor event can be published to Slack as a JSON payload. Let's review posting a sensor event to Slack.
  2. For example, let's consider posting a message when a cat door opens. The first step is preparing the JSON payload for the message. According to the Slack API documentation (https://api.slack.com/custom-integrations), the message payload needs to be in the following format:
       payload = {"text": "The cat door was just opened!"}
  1. In order to publish this event, we will make use of the post() method from the requests module. The data payload needs to be encoded in JSON format while posting it:
       response = requests.post(URL, json.dumps(payload))
  1. Putting it all together, we have this:
       #!/usr/bin/python3

import requests
import json

# generate your own URL
URL = 'https://hooks.slack.com/services/'

if __name__ == "__main__":
payload = {"text": "The cat door was just opened!"}
try:
# encode data payload and post it
response = requests.post(URL, json.dumps(payload))
print(response.text)
except requests.exceptions.ConnectionError as error:
print("The error is %s" % error)
  1. On posting the message, the request returns ok as a response. This indicates that the post was successful.
  2. Generate your own URL and execute the preceding example (available for download along with this chapter as slack_post.py). You will receive a direct message on Slack:
Direct message on Slack

Now, try interfacing a sensor to the Raspberry Pi Zero (discussed in previous chapters) and post the sensor events to Slack.

It is also possible to post sensor events to Twitter and have your Raspberry Pi Zero check for new e-mails and so on. Check this book's website for more examples.