|
| 1 | +"""A simple bot script, built on Pyramid using Cornice |
| 2 | +
|
| 3 | +This sample script leverages the Pyramid web framework (https://trypyramid.com/) with |
| 4 | +Cornice (https://cornice.readthedocs.io). By default the web server will be reachable at |
| 5 | +port 6543 you can change this default if desired (see `pyramidSparkBot.ini`). |
| 6 | +
|
| 7 | +ngrok (https://ngrok.com/) can be used to tunnel traffic back to your server |
| 8 | +if your machine sits behind a firewall. |
| 9 | +
|
| 10 | +You must create a Spark webhook that points to the URL where this script is |
| 11 | +hosted. You can do this via the CiscoSparkAPI.webhooks.create() method. |
| 12 | +
|
| 13 | +Additional Spark webhook details can be found here: |
| 14 | +https://developer.ciscospark.com/webhooks-explained.html |
| 15 | +
|
| 16 | +A bot must be created and pointed to this server in the My Apps section of |
| 17 | +https://developer.ciscospark.com. The bot's Access Token should be added as a |
| 18 | +'SPARK_ACCESS_TOKEN' environment variable on the web server hosting this |
| 19 | +script. |
| 20 | +
|
| 21 | +This script supports Python versions 2 and 3. |
| 22 | +""" |
| 23 | +from __future__ import (absolute_import, division, |
| 24 | + print_function, unicode_literals) |
| 25 | +from cornice import Service |
| 26 | + |
| 27 | +from builtins import * |
| 28 | + |
| 29 | +import json |
| 30 | + |
| 31 | +import requests |
| 32 | + |
| 33 | +from ciscosparkapi import CiscoSparkAPI, Webhook |
| 34 | + |
| 35 | +import logging |
| 36 | +log = logging.getLogger(__name__) |
| 37 | + |
| 38 | + |
| 39 | +# Module constants |
| 40 | +CHUCK_NORRIS_JOKES_URL = 'http://api.icndb.com/jokes/random' |
| 41 | + |
| 42 | + |
| 43 | +# Initialize the environment |
| 44 | +spark_api = CiscoSparkAPI() # Create the Cisco Spark API connection object |
| 45 | + |
| 46 | + |
| 47 | +# Helper functions |
| 48 | +def get_chuck_norris_joke(): |
| 49 | + """Get a Chuck Norris Joke from api.icndb.com and return it as a string. |
| 50 | + Functions for Soundhound, Google, IBM Watson, or other APIs can be added |
| 51 | + to create the desired functionality into this bot. |
| 52 | + """ |
| 53 | + response = requests.get(CHUCK_NORRIS_JOKES_URL, verify=False) |
| 54 | + response_dict = json.loads(response.text) |
| 55 | + return response_dict['value']['joke'] |
| 56 | + |
| 57 | + |
| 58 | +sparkwebhook = Service(name='sparkwebhook', path='/sparkwebhook', description="Spark Webhook") |
| 59 | + |
| 60 | + |
| 61 | +@sparkwebhook.get() |
| 62 | +def get_sparkwebhook(request): |
| 63 | + log.info(get_chuck_norris_joke()) |
| 64 | + return {"joke": get_chuck_norris_joke()} |
| 65 | + |
| 66 | +@sparkwebhook.post() |
| 67 | +# Your Spark webhook should point to http://<serverip>:5000/sparkwebhook |
| 68 | +def post_sparkwebhook(request): |
| 69 | + """Respond to inbound webhook JSON HTTP POST from Cisco Spark.""" |
| 70 | + |
| 71 | + json_data = request.json # Get the POST data sent from Cisco Spark |
| 72 | + log.info("\n") |
| 73 | + log.info("WEBHOOK POST RECEIVED:") |
| 74 | + log.info(json_data) |
| 75 | + log.info("\n") |
| 76 | + |
| 77 | + webhook_obj = Webhook(json_data) # Create a Webhook object from the JSON data |
| 78 | + room = spark_api.rooms.get(webhook_obj.data.roomId) # Get the room details |
| 79 | + message = spark_api.messages.get(webhook_obj.data.id) # Get the message details |
| 80 | + person = spark_api.people.get(message.personId) # Get the sender's details |
| 81 | + |
| 82 | + log.info("NEW MESSAGE IN ROOM '{}'".format(room.title)) |
| 83 | + log.info("FROM '{}'".format(person.displayName)) |
| 84 | + log.info("MESSAGE '{}'\n".format(message.text)) |
| 85 | + |
| 86 | + # This is a VERY IMPORTANT loop prevention control step. |
| 87 | + # If you respond to all messages... You will respond to the messages |
| 88 | + # that the bot posts and thereby create a loop condition. |
| 89 | + me = spark_api.people.me() |
| 90 | + if message.personId == me.id: |
| 91 | + # Message was sent by me (bot); do not respond. |
| 92 | + return {'Message': 'OK'} |
| 93 | + |
| 94 | + else: |
| 95 | + # Message was sent by someone else; parse message and respond. |
| 96 | + if "/CHUCKNORRIS" in message.text: |
| 97 | + log.info("FOUND '/CHUCKNORRIS'") |
| 98 | + chuck_norris_joke = get_chuck_norris_joke() # Get a Chuck Norris Joke |
| 99 | + log.info("SENDING CHUCK NORRIS JOKE '{}'".format(chuck_norris_joke)) |
| 100 | + spark_api.messages.create(room.id, text=chuck_norris_joke) # Post the fact to the room where the request was received |
| 101 | + return {'Message': 'OK'} |
| 102 | + |
0 commit comments