|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +"""A simple bot script, built on Flask. |
| 4 | +
|
| 5 | +This sample script leverages the Flask web service micro-framework |
| 6 | +(see http://flask.pocoo.org/). By default the web server will be reachable at |
| 7 | +port 5000 you can change this default if desired (see `flask_app.run(...)`). |
| 8 | +
|
| 9 | +ngrok (https://ngrok.com/) can be used to tunnel traffic back to your server |
| 10 | +if your machine sits behind a firewall. |
| 11 | +
|
| 12 | +You must create a Spark webhook that points to the URL where this script is |
| 13 | +hosted. You can do this via the CiscoSparkAPI.webhooks.create() method. |
| 14 | +
|
| 15 | +Additional Spark webhook details can be found here: |
| 16 | +https://developer.ciscospark.com/webhooks-explained.html |
| 17 | +
|
| 18 | +A bot must be created and pointed to this server in the My Apps section of |
| 19 | +https://developer.ciscospark.com. The bot's Access Token should be added as a |
| 20 | +'SPARK_ACCESS_TOKEN' environment variable on the web server hosting this |
| 21 | +script. |
| 22 | +
|
| 23 | +This script supports Python versions 2 and 3. |
| 24 | +
|
| 25 | +""" |
| 26 | + |
| 27 | + |
| 28 | +from __future__ import (absolute_import, division, |
| 29 | + print_function, unicode_literals) |
| 30 | +from builtins import * |
| 31 | + |
| 32 | +import json |
| 33 | + |
| 34 | +import requests |
| 35 | + |
| 36 | +from flask import Flask, request |
| 37 | + |
| 38 | +from ciscosparkapi import CiscoSparkAPI, Webhook |
| 39 | + |
| 40 | + |
| 41 | +# Module constants |
| 42 | +CAT_FACTS_URL = 'http://catfacts-api.appspot.com/api/facts?number=1' |
| 43 | + |
| 44 | + |
| 45 | +# Initialize the environment |
| 46 | +flask_app = Flask(__name__) # Create the web application instance |
| 47 | +spark_api = CiscoSparkAPI() # Create the Cisco Spark API connection object |
| 48 | + |
| 49 | + |
| 50 | +urls = ('/sparkwebhook', 'webhook') |
| 51 | + |
| 52 | + |
| 53 | +# Helper functions |
| 54 | +def get_catfact(): |
| 55 | + """Get a cat fact from appspot.com and return it as a string. |
| 56 | +
|
| 57 | + Functions for Soundhound, Google, IBM Watson, or other APIs can be added |
| 58 | + to create the desired functionality into this bot. |
| 59 | +
|
| 60 | + """ |
| 61 | + response = requests.get(CAT_FACTS_URL, verify=False) |
| 62 | + response_dict = json.loads(response.text) |
| 63 | + return response_dict['facts'][0] |
| 64 | + |
| 65 | + |
| 66 | +# Core bot functionality |
| 67 | +@flask_app.route('/sparkwebhook', methods=['GET', 'POST']) # Your Spark webhook should point to http://<serverip>:5000/sparkwebhook |
| 68 | +def sparkwebhook(): |
| 69 | + """Processes incoming requests to the '/sparkwebhook' URI.""" |
| 70 | + if request.method == 'GET': |
| 71 | + return (""" <!DOCTYPE html> |
| 72 | + <html lang="en"> |
| 73 | + <head> |
| 74 | + <meta charset="UTF-8"> |
| 75 | + <title>Spark Bot served via Flask</title> |
| 76 | + </head> |
| 77 | + <body> |
| 78 | + <p> |
| 79 | + <strong>Your Flask web server is up and running!</strong> |
| 80 | + </p> |
| 81 | + <p> |
| 82 | + Here is a nice Cat Fact for you: |
| 83 | + </p> |
| 84 | + <blockquote> {} </blockquote> |
| 85 | + </body> |
| 86 | + </html> |
| 87 | + """.format(get_catfact())) |
| 88 | + elif request.method == 'POST': |
| 89 | + """Respond to inbound webhook JSON HTTP POST from Cisco Spark.""" |
| 90 | + |
| 91 | + json_data = request.data # Get the POST data sent from Cisco Spark |
| 92 | + print("\n") |
| 93 | + print("WEBHOOK POST RECEIVED:") |
| 94 | + print(json_data) |
| 95 | + print("\n") |
| 96 | + |
| 97 | + webhook_obj = Webhook(json_data) # Create a Webhook object from the JSON data |
| 98 | + room = spark_api.rooms.get(webhook_obj.data.roomId) # Get the room details |
| 99 | + message = spark_api.messages.get(webhook_obj.data.id) # Get the message details |
| 100 | + person = spark_api.people.get(message.personId) # Get the sender's details |
| 101 | + |
| 102 | + print("NEW MESSAGE IN ROOM '{}'".format(room.title)) |
| 103 | + print("FROM '{}'".format(person.displayName)) |
| 104 | + print("MESSAGE '{}'\n".format(message.text)) |
| 105 | + |
| 106 | + # This is a VERY IMPORTANT loop prevention control step. |
| 107 | + # If you respond to all messages... You will respond to the messages |
| 108 | + # that the bot posts and thereby create a loop condition. |
| 109 | + me = spark_api.people.me() |
| 110 | + if message.personId == me.id: |
| 111 | + # Message was sent by me (bot); do not respond. |
| 112 | + return 'OK' |
| 113 | + |
| 114 | + else: |
| 115 | + # Message was sent by someone else; parse message and respond. |
| 116 | + if "/CAT" in message.text: |
| 117 | + print("FOUND '/CAT'") |
| 118 | + cat_fact = get_catfact() # Get a cat fact |
| 119 | + print("SENDING CAT FACT '{}'".format(cat_fact)) |
| 120 | + spark_api.messages.create(room.id, text=cat_fact) # Post the fact to the room where the request was received |
| 121 | + return 'OK' |
| 122 | + |
| 123 | + |
| 124 | +if __name__ == '__main__': |
| 125 | + # Start the Flask web server |
| 126 | + flask_app.run(host='0.0.0.0', port=5000) |
0 commit comments