Skip to content
This repository has been archived by the owner on Oct 19, 2023. It is now read-only.

Commit

Permalink
Merge pull request #3 from edonosotti/master
Browse files Browse the repository at this point in the history
Improve Message() and MessageSet() objects to better match the API specs and fix imports
  • Loading branch information
cristiancavalli authored Jan 4, 2018
2 parents a433761 + 442927a commit eca2b29
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 16 deletions.
13 changes: 11 additions & 2 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Generic:
```PYTHON
from chatbase import Message

msg = Message(api_key="x",
msg = Message(api_key="x",
platform="kik",
version="0.1",
user_id="unique-str",
Expand Down Expand Up @@ -56,6 +56,7 @@ resp = usrMsg.send()

Generic:


```PYTHON
from chatbase import MessageSet

Expand All @@ -66,6 +67,14 @@ msg = set.new_message(intent="impress", content="goes to 11")
# one can still edit the message normally and these changes will be reflected
# in the containing set
msg.user_id = "shark-sandwich"
# Message type objects can be appended:
msg2 = Message(api_key="x",
platform="my_platform",
version="0.1",
user_id="unique-str",
message="this is a test",
intent="test")
set.append_message(msg2)
# Sending the set will send all contained messages to the batch endpoint
resp = set.send()
```
Expand Down Expand Up @@ -98,5 +107,5 @@ Please place tests in `tests` directory. To run tests, from the repository
root run the following command:

```
$ python -m unittest discover ./tests/
$ python -m unittest discover ./chatbase/tests/
```
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Generic:
```PYTHON
from chatbase import Message

msg = Message(api_key="x",
msg = Message(api_key="x",
platform="kik",
version="0.1",
user_id="unique-str",
Expand Down Expand Up @@ -66,6 +66,14 @@ msg = set.new_message(intent="impress", content="goes to 11")
# one can still edit the message normally and these changes will be reflected
# in the containing set
msg.user_id = "shark-sandwich"
# Message type objects can be appended:
msg2 = Message(api_key="x",
platform="my_platform",
version="0.1",
user_id="unique-str",
message="this is a test",
intent="test")
set.append_message(msg2)
# Sending the set will send all contained messages to the batch endpoint
resp = set.send()
```
Expand Down Expand Up @@ -98,5 +106,5 @@ Please place tests in `tests` directory. To run tests, from the repository
root run the following command:

```
$ python -m unittest discover ./tests/
$ python -m unittest discover ./chatbase/tests/
```
27 changes: 21 additions & 6 deletions chatbase/base_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,20 @@ def __init__(self,
message="",
intent="",
version="",
user_id=""):
user_id="",
type=None,
not_handled=False,
time_stamp=None):
self.api_key = api_key
self.platform = platform
self.message = message
self.intent = intent
self.version = version
self.user_id = user_id
self.not_handled = False
self.not_handled = not_handled
self.feedback = False
self.time_stamp = Message.get_current_timestamp()
self.type = MessageTypes.USER
self.time_stamp = time_stamp or Message.get_current_timestamp()
self.type = type or MessageTypes.USER

@staticmethod
def get_current_timestamp():
Expand Down Expand Up @@ -134,14 +137,26 @@ def __init__(self,
self.user_id = user_id
self.messages = []

def new_message(self, intent="", message=""):
def append_message(self, message_object):
"""Append a Message object to the set."""
self.messages.append(message_object)

def new_message(self,
intent="",
message="",
type=None,
not_handled=False,
time_stamp=None):
"""Add a message to the internal messages list and return it"""
self.messages.append(Message(api_key=self.api_key,
platform=self.platform,
version=self.version,
user_id=self.user_id,
intent=intent,
message=message))
message=message,
type=type,
not_handled=not_handled,
time_stamp=time_stamp))
return self.messages[-1]

def to_json(self):
Expand Down
4 changes: 2 additions & 2 deletions chatbase/facebook_user_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def set_sender_id(self, snd_id):
def set_message_id(self, msg_id):
"""Set the message id."""
self.fb_message.mid = msg_id

def set_chatbase_fields(self):
"""Extract chatbase fields from instance and format for transmission."""
self.chatbase_fields.intent = self.intent
Expand Down Expand Up @@ -76,7 +76,7 @@ def to_set_format(self):
'message': self.fb_message,
'chatbase_fields': self.chatbase_fields
}

def send(self):
"""Send the message to the Chatbase API."""
url = ("https://chatbase.com/api/facebook/send_message?api_key=%s" %
Expand Down
98 changes: 94 additions & 4 deletions chatbase/tests/test_base_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import json
import time
import os
import unittest
from chatbase import Message, MessageSet, MessageTypes, InvalidMessageTypeError
Expand Down Expand Up @@ -67,9 +68,11 @@ def test_to_json(self):
intent = '3'
version = '4'
user_id = '5'
time_stamp = int(round(time.time() * 1e3))
i = Message(api_key=api_key, platform=platform, message=message,
intent=intent, version=version, user_id=user_id)
i.set_as_not_handled()
intent=intent, version=version, user_id=user_id,
type=MessageTypes.USER, not_handled=True,
time_stamp=time_stamp)
i.set_as_feedback()
self.assertEqual(json.loads(i.to_json()), {
'api_key': api_key,
Expand All @@ -78,11 +81,98 @@ def test_to_json(self):
'intent': intent,
'version': version,
'user_id': user_id,
'time_stamp': i.time_stamp,
'time_stamp': time_stamp,
'type': MessageTypes.USER, # since we did not set as type agent
'not_handled': True,
'feedback': True
})

def test_message_set_append_message(self):
api_key = '1234'
platform = '1'
message = '2'
intent = '3'
version = '4'
user_id = '5'
time_stamp = int(round(time.time() * 1e3))
msg1 = Message(api_key=api_key, platform=platform, message=message,
intent=intent, version=version, user_id=user_id,
type=MessageTypes.USER, not_handled=True,
time_stamp=time_stamp)
msg1.set_as_feedback()
msg2 = Message(api_key=api_key, platform=platform, message=message,
version=version, user_id=user_id,
type=MessageTypes.AGENT)
message_set = MessageSet(api_key=api_key, platform=platform,
version=version, user_id=user_id)
message_set.append_message(msg1)
message_set.append_message(msg2)
msg1 = message_set.messages[0]
self.assertEqual(json.loads(msg1.to_json()), {
'api_key': api_key,
'platform': platform,
'message': message,
'intent': intent,
'version': version,
'user_id': user_id,
'time_stamp': time_stamp,
'type': MessageTypes.USER, # since we did not set as type agent
'not_handled': True,
'feedback': True
})
msg2 = message_set.messages[1]
self.assertEqual(json.loads(msg2.to_json()), {
'api_key': api_key,
'platform': platform,
'message': message,
'intent': msg2.intent,
'version': version,
'user_id': user_id,
'time_stamp': msg2.time_stamp,
'type': MessageTypes.AGENT, # since we did set as type agent
'not_handled': False,
'feedback': False
})

def test_message_set_new_message(self):
api_key = '1234'
platform = '1'
message = '2'
intent = '3'
version = '4'
user_id = '5'
time_stamp = int(round(time.time() * 1e3))
message_set = MessageSet(api_key=api_key, platform=platform,
version=version, user_id=user_id)
msg1 = message_set.new_message(intent=intent, message=message,
type=MessageTypes.USER,
not_handled=True, time_stamp=time_stamp)
msg1.set_as_feedback()
msg2 = message_set.new_message(message=message, type=MessageTypes.AGENT)
self.assertEqual(json.loads(msg1.to_json()), {
'api_key': api_key,
'platform': platform,
'message': message,
'intent': intent,
'version': version,
'user_id': user_id,
'time_stamp': time_stamp,
'type': MessageTypes.USER, # since we did not set as type agent
'not_handled': True,
'feedback': True
})
self.assertEqual(json.loads(msg2.to_json()), {
'api_key': api_key,
'platform': platform,
'message': message,
'intent': msg2.intent,
'version': version,
'user_id': user_id,
'time_stamp': msg2.time_stamp,
'type': MessageTypes.AGENT, # since we did not set as type agent
'not_handled': False,
'feedback': False
})

def test_live_send(self):
test_api_key = os.environ.get('CB_TEST_API_KEY')
Expand All @@ -97,7 +187,7 @@ def test_live_send(self):
user_id="12345")
resp = i.send()
self.assertEqual(resp.status_code, 200)

def test_live_set_send(self):
test_api_key = os.environ.get('CB_TEST_API_KEY')
if test_api_key is None:
Expand Down

0 comments on commit eca2b29

Please sign in to comment.