|
| 1 | +# -*- coding: utf-8 -*- |
1 | 2 | """A simple bot script, built on Pyramid using Cornice |
2 | 3 |
|
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`). |
| 4 | +This sample script leverages the Pyramid web framework https://trypyramid.com/ |
| 5 | +with Cornice https://cornice.readthedocs.io. By default the web server will be |
| 6 | +reachable at port 6543 you can change this default if desired |
| 7 | +(see `pyramidSparkBot.ini`). |
6 | 8 |
|
7 | 9 | ngrok (https://ngrok.com/) can be used to tunnel traffic back to your server |
8 | 10 | if your machine sits behind a firewall. |
|
19 | 21 | script. |
20 | 22 |
|
21 | 23 | This script supports Python versions 2 and 3. |
| 24 | +
|
22 | 25 | """ |
23 | | -from __future__ import (absolute_import, division, |
24 | | - print_function, unicode_literals) |
25 | | -from cornice import Service |
26 | 26 |
|
| 27 | + |
| 28 | +# Use future for Python v2 and v3 compatibility |
| 29 | +from __future__ import ( |
| 30 | + absolute_import, |
| 31 | + division, |
| 32 | + print_function, |
| 33 | + unicode_literals, |
| 34 | +) |
27 | 35 | from builtins import * |
28 | 36 |
|
29 | | -import json |
30 | 37 |
|
31 | | -import requests |
| 38 | +__author__ = "Jose Bogarín Solano" |
| 39 | +__author_email__ = "jose@bogarin.co.cr" |
| 40 | +__contributors__ = [ |
| 41 | + "Brad Bester <brbester@cisco.com>", |
| 42 | + "Chris Lunsford <chrlunsf@cisco.com>", |
| 43 | +] |
| 44 | +__copyright__ = "Copyright (c) 2016-2018 Cisco and/or its affiliates." |
| 45 | +__license__ = "MIT" |
| 46 | + |
32 | 47 |
|
33 | 48 | from ciscosparkapi import CiscoSparkAPI, Webhook |
| 49 | +from cornice import Service |
| 50 | +import requests |
| 51 | + |
34 | 52 |
|
35 | 53 | import logging |
36 | 54 | log = logging.getLogger(__name__) |
37 | 55 |
|
38 | 56 |
|
39 | 57 | # Module constants |
40 | | -CAT_FACT_URL = 'http://catfacts-api.appspot.com/api/facts?number=1' |
| 58 | +CAT_FACTS_URL = 'https://catfact.ninja/fact' |
41 | 59 |
|
42 | 60 |
|
43 | 61 | # Initialize the environment |
|
46 | 64 |
|
47 | 65 | # Helper functions |
48 | 66 | def get_catfact(): |
49 | | - """Get a cat fact from catfacts-api.appspot.com and return it as a string. |
| 67 | + """Get a cat fact from catfact.ninja and return it as a string. |
| 68 | +
|
50 | 69 | Functions for Soundhound, Google, IBM Watson, or other APIs can be added |
51 | 70 | to create the desired functionality into this bot. |
| 71 | +
|
52 | 72 | """ |
53 | | - response = requests.get(CAT_FACT_URL, verify=False) |
54 | | - response_dict = json.loads(response.text) |
55 | | - return response_dict['facts'][0] |
| 73 | + response = requests.get(CAT_FACTS_URL, verify=False) |
| 74 | + response.raise_for_status() |
| 75 | + json_data = response.json() |
| 76 | + return json_data['fact'] |
56 | 77 |
|
57 | 78 |
|
58 | | -sparkwebhook = Service(name='sparkwebhook', path='/sparkwebhook', description="Spark Webhook") |
| 79 | +sparkwebhook = Service( |
| 80 | + name='sparkwebhook', |
| 81 | + path='/sparkwebhook', |
| 82 | + description="Spark Webhook", |
| 83 | +) |
59 | 84 |
|
60 | 85 |
|
61 | 86 | @sparkwebhook.get() |
62 | 87 | def get_sparkwebhook(request): |
63 | 88 | log.info(get_catfact()) |
64 | 89 | return {"fact": get_catfact()} |
65 | 90 |
|
66 | | -@sparkwebhook.post() |
| 91 | + |
67 | 92 | # Your Spark webhook should point to http://<serverip>:6543/sparkwebhook |
| 93 | +@sparkwebhook.post() |
68 | 94 | def post_sparkwebhook(request): |
69 | 95 | """Respond to inbound webhook JSON HTTP POST from Cisco Spark.""" |
70 | | - |
71 | 96 | json_data = request.json # Get the POST data sent from Cisco Spark |
72 | 97 | log.info("\n") |
73 | 98 | log.info("WEBHOOK POST RECEIVED:") |
@@ -99,4 +124,3 @@ def post_sparkwebhook(request): |
99 | 124 | log.info("SENDING CAT FACT'{}'".format(catfact)) |
100 | 125 | spark_api.messages.create(room.id, text=catfact) # Post the fact to the room where the request was received |
101 | 126 | return {'Message': 'OK'} |
102 | | - |
|
0 commit comments