Skip to content

Instantly share code, notes, and snippets.

@bjyurkovich
Created October 19, 2019 04:20
Show Gist options
  • Select an option

  • Save bjyurkovich/9ed60f01f3a2770df3ea37a442670899 to your computer and use it in GitHub Desktop.

Select an option

Save bjyurkovich/9ed60f01f3a2770df3ea37a442670899 to your computer and use it in GitHub Desktop.
from flask import Flask, jsonify, request
import requests
import os
# Create my flask app to run on Port 7500
app = Flask(__name__)
PORT = 7500
# Handler for Fred's data (receiving the iotery Post Data Webhook)
@app.route("/light-status", methods=["POST"])
def handle_data_webhook():
body = request.json
# Get the status out of the webhook body
status = body["in"]["packets"][0]["data"]["light_state"]
# Post to slack webhook we created
requests.post(
"https://hooks.slack.com/services/TKQ7F5C3A/BPMKH0Z54/...",
json={"text": "Fred says that he is currently " + ("on" if status == 1 else "off")}
)
return jsonify({"status": "OK"})
# Handle wehhook from slack to turn light on
@app.route("/turn-on", methods=["POST"])
def handle_light_on():
# Create a command instance on Iotery for Fred to turn light on
iotery.createDeviceCommandInstance(
deviceUuid="78c204ce-f216-11e9-b548-d283610663ec", data={
"commandTypeUuid": "58b22c78-f21d-11e9-b548-d283610663ec"})
return jsonify("Turned on!")
# Handle wehhook from slack to turn light off
@app.route("/turn-off", methods=["POST"])
def handle_light_off():
# Create a command instance on Iotery for Fred to turn light off
iotery.createDeviceCommandInstance(
deviceUuid="78c204ce-f216-11e9-b548-d283610663ec", data={
"commandTypeUuid": "6369b165-f21d-11e9-b548-d283610663ec"})
return jsonify("Turned off!")
if __name__ == "__main__":
app.run(debug=True, port=PORT, host="0.0.0.0")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment