Repository: imapex/ciscosparkbot

Issue:
Allow callbacks to return `ciscosparkapi.Message` objects
This will allow extended functionality such as files, html, and mentions

Comments:

Patch:
diff --git a/ciscosparkbot/Spark.py b/ciscosparkbot/Spark.py
index d91685a..f9ca3f8 100644
--- a/ciscosparkbot/Spark.py
+++ b/ciscosparkbot/Spark.py
@@ -9,6 +9,7 @@ Classes:
 
 from flask import Flask, request
 from ciscosparkapi import CiscoSparkAPI
+from ciscosparkbot.models import Response
 import sys
 import json
 
@@ -257,8 +258,13 @@ class SparkBot(Flask):
         else:
             pass
 
-        # send_message_to_room(room_id, reply)
-        if reply:
+        # allow command handlers to craft their own Spark message
+        if reply and isinstance(reply, Response):
+            reply.roomId = room_id
+            reply = reply.as_dict()
+            self.spark.messages.create(**reply)
+            reply = "ok"
+        elif reply:
             self.spark.messages.create(roomId=room_id, markdown=reply)
         return reply
 
diff --git a/ciscosparkbot/__about__.py b/ciscosparkbot/__about__.py
index f14e5b2..7c50a86 100644
--- a/ciscosparkbot/__about__.py
+++ b/ciscosparkbot/__about__.py
@@ -7,7 +7,7 @@ __title__ = "ciscosparkbot"
 __summary__ = "Python Bot for Cisco Spark"
 __uri__ = "http://github.com/imapex/ciscosparkbot"
 
-__version__ = "0.5.5"
+__version__ = "0.6.2"
 
 __author__ = "Cisco Systems, Inc."
 __email__ = "imapex-admin@cisco.com"
diff --git a/ciscosparkbot/models.py b/ciscosparkbot/models.py
new file mode 100644
index 0000000..979670b
--- /dev/null
+++ b/ciscosparkbot/models.py
@@ -0,0 +1,64 @@
+import json
+
+
+class Response(object):
+    def __init__(self, attributes=None):
+        if attributes:
+            self.attributes = attributes
+        else:
+            self.attributes = dict()
+            self.attributes['text'] = None
+            self.attributes['roomId'] = None
+            self.attributes['markdown'] = None
+            self.attributes['html'] = None
+            self.attributes['files'] = list()
+
+    @property
+    def text(self):
+        return self.attributes['text']
+
+    @text.setter
+    def text(self, val):
+        self.attributes['text'] = val
+
+    @property
+    def files(self):
+        return self.attributes['files']
+
+    @files.setter
+    def files(self, val):
+        self.attributes['files'].append(val)
+
+    @property
+    def roomId(self):
+        return self.attributes['roomId']
+
+    @roomId.setter
+    def roomId(self, val):
+        self.attributes['roomId'] = val
+
+    @property
+    def markdown(self):
+        return self.attributes['markdown']
+
+    @markdown.setter
+    def markdown(self, val):
+        self.attributes['markdown'] = val
+
+    @property
+    def html(self):
+        return self.attributes['html']
+
+    @html.setter
+    def html(self, val):
+        self.attributes['html'] = val
+
+    def as_dict(self):
+        ret = dict()
+        for k, v in self.attributes.items():
+            if v:
+                ret[k] = v
+        return ret
+
+    def json(self):
+        return json.dumps(self.attributes)
diff --git a/requirements.txt b/requirements.txt
index a1cb3da..7faef01 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,2 +1,2 @@
 Flask==0.12.1
-ciscosparkapi==0.5.5
\ No newline at end of file
+ciscosparkapi==0.10
diff --git a/sample.py b/sample.py
index 54165ce..7e620c6 100644
--- a/sample.py
+++ b/sample.py
@@ -2,9 +2,9 @@
 """
 Sample code for using ciscosparkbot
 """
-
 import os
 from ciscosparkbot import SparkBot
+from ciscosparkbot.models import Response
 
 __author__ = "imapex"
 __author_email__ = "CiscoSparkBot@imapex.io"
@@ -27,12 +27,23 @@ def do_something(incoming_msg):
     return "i did what you said - {}".format(incoming_msg.text)
 
 
+def ret_message(incoming_msg):
+    m = Response()
+    u = 'https://sayingimages.com/wp-content/uploads/'
+    u = u + 'aaaaaalll-righty-then-alrighty-meme.jpg'
+    m.files = u
+    return m
+
+
 # Create a new bot
 bot = SparkBot(bot_app_name, spark_bot_token=spark_token,
                spark_bot_url=bot_url, spark_bot_email=bot_email, debug=True)
 
+
 # Add new command
 bot.add_command('/dosomething', 'help for do something', do_something)
+bot.add_command('/demo', 'sampel that allows spark message to be returned',
+                ret_message)
 
 # Run Bot
 bot.run(host='0.0.0.0', port=5000)


Test patch:
diff --git a/tests/models.py b/tests/models.py
new file mode 100644
index 0000000..fa4a022
--- /dev/null
+++ b/tests/models.py
@@ -0,0 +1,40 @@
+import unittest
+from ciscosparkbot.models import Response
+
+
+class ModelTests(unittest.TestCase):
+
+    def test_response_text(self):
+        r = Response()
+        r.text = "hello"
+        self.assertEqual(r.text, "hello")
+
+    def test_response_files(self):
+        r = Response()
+        r.files = "someurl"
+        self.assertEqual(r.files[0], "someurl")
+
+    def test_response_roomid(self):
+        r = Response()
+        r.roomId = "someid"
+        self.assertEqual(r.roomId, "someid")
+
+    def test_response_markdown(self):
+        r = Response()
+        r.markdown = "**some markdown**"
+        self.assertEqual(r.markdown, "**some markdown**")
+
+    def test_response_html(self):
+        r = Response()
+        r.html = "<h1>some html</h1>"
+        self.assertEqual(r.html, "<h1>some html</h1>")
+
+    def test_response_json(self):
+        r = Response()
+        r.text = "foo"
+        self.assertIn('text', r.json())
+
+    def test_response_as_dict(self):
+        r = Response()
+        r.text = "foo"
+        self.assertIn('text', r.as_dict())